14
14
import reprlib
15
15
import importlib
16
16
from collections .abc import Mapping , Callable , Iterable
17
- from typing import Any , TypeVar , Type , Generic , cast , TYPE_CHECKING
17
+ from typing import Any , TypeVar , Type , Generic , TYPE_CHECKING
18
18
19
19
_T = TypeVar ("_T" )
20
20
21
21
if TYPE_CHECKING :
22
22
from typing_extensions import Protocol
23
- from typing import (
24
- overload ,
25
- Collection ,
26
- Union ,
27
- Tuple ,
28
- Dict ,
29
- KeysView ,
30
- ValuesView ,
31
- ItemsView ,
32
- Iterator ,
33
- )
23
+ from typing import Union , Tuple , Dict
34
24
35
25
_KT = TypeVar ("_KT" )
36
- _VT = TypeVar ("_VT" )
37
26
_VT_co = TypeVar ("_VT_co" , covariant = True )
38
27
_ST1 = TypeVar ("_ST1" , bound = "LazyImporter[Any]" )
39
28
_ST2 = TypeVar ("_ST2" , bound = "MutableLazyImporter[Any]" )
@@ -42,26 +31,6 @@ class _SupportsKeysAndGetItem(Protocol[_KT, _VT_co]):
42
31
def __getitem__ (self , __key : _KT ) -> _VT_co : ...
43
32
def keys (self ) -> Iterable [_KT ]: ...
44
33
45
- # A `Protocol` version of `types.MappingProxyType`
46
- class _MappingProtocol (Protocol [_KT , _VT ], Collection [_KT ]):
47
- __hash__ : None # type: ignore
48
- def __getitem__ (self , k : _KT ) -> _VT : ...
49
- def __contains__ (self , o : object ) -> bool : ...
50
- @overload
51
- def get (self , key : _KT ) -> None | _VT : ...
52
- @overload
53
- def get (self , key : _KT , default : _T ) -> _T | _VT : ...
54
- def items (self ) -> ItemsView [_KT , _VT ]: ...
55
- def keys (self ) -> KeysView [_KT ]: ...
56
- def values (self ) -> ValuesView [_VT ]: ...
57
- def copy (self ) -> dict [_KT , _VT ]: ...
58
-
59
- # TODO: Update `__or__` once mypy > 0.910 hits
60
- if sys .version_info >= (3 , 9 ):
61
- def __reversed__ (self ) -> Iterator [_KT ]: ...
62
- def __or__ (self , __value : Mapping [_KT , _VT ]) -> dict [_KT , _VT ]: ...
63
- def __ror__ (self , __value : Mapping [_KT , _VT ]) -> dict [_KT , _VT ]: ...
64
-
65
34
_DictLike = Union [_SupportsKeysAndGetItem [str , str ], Iterable [Tuple [str , str ]]]
66
35
_ReduceTuple = Tuple [Callable [[str , Dict [str , str ]], _T ], Tuple [str , Dict [str , str ]]]
67
36
@@ -114,7 +83,7 @@ def module(self) -> types.ModuleType:
114
83
return self ._module
115
84
116
85
@property
117
- def imports (self ) -> _MappingProtocol [str , str ]:
86
+ def imports (self ) -> Mapping [str , str ]:
118
87
""":class:`types.MappingProxyType[str, str]<types.MappingProxyType>`: Get a mapping that maps object names to their module name.""" # noqa: E501
119
88
return self ._imports
120
89
@@ -127,7 +96,7 @@ def __init__(
127
96
if not isinstance (module , types .ModuleType ):
128
97
raise TypeError (f"Expected a module, not { type (module ).__name__ } " )
129
98
self ._module = module
130
- self ._imports = cast ( "_MappingProtocol [str, str]" , types .MappingProxyType (dict (imports ) ))
99
+ self ._imports : Mapping [str , str ] = types .MappingProxyType (dict (imports ))
131
100
132
101
@classmethod
133
102
def from_name (cls : Type [_ST1 ], name : str , imports : _DictLike ) -> _ST1 :
@@ -158,7 +127,7 @@ def from_name(cls: Type[_ST1], name: str, imports: _DictLike) -> _ST1:
158
127
def __reduce__ (self : _ST1 ) -> _ReduceTuple [_ST1 ]:
159
128
"""Helper for :mod:`pickle`."""
160
129
cls = type (self )
161
- args = (self .module .__name__ , self .imports .copy ())
130
+ args = (self .module .__name__ , self .imports .copy ()) # type: ignore[attr-defined]
162
131
return cls .from_name , args
163
132
164
133
def __copy__ (self : _ST1 ) -> _ST1 :
@@ -179,8 +148,7 @@ def __hash__(self) -> int:
179
148
180
149
def __eq__ (self , value : object ) -> bool :
181
150
"""Implement :meth:`self == value<object.__eq__>`."""
182
- cls = type (self )
183
- if not isinstance (value , cls ):
151
+ if not isinstance (value , LazyImporter ):
184
152
return NotImplemented
185
153
return self .module is value .module and self .imports == value .imports
186
154
0 commit comments