Skip to content

Commit 6ff778e

Browse files
author
Bas van Beek
committed
TYP: Simplify LazyImporter.imports annotations
1 parent 82d9b9c commit 6ff778e

File tree

1 file changed

+6
-38
lines changed

1 file changed

+6
-38
lines changed

nanoutils/_lazy_import.py

+6-38
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,15 @@
1414
import reprlib
1515
import importlib
1616
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
1818

1919
_T = TypeVar("_T")
2020

2121
if TYPE_CHECKING:
2222
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
3424

3525
_KT = TypeVar("_KT")
36-
_VT = TypeVar("_VT")
3726
_VT_co = TypeVar("_VT_co", covariant=True)
3827
_ST1 = TypeVar("_ST1", bound="LazyImporter[Any]")
3928
_ST2 = TypeVar("_ST2", bound="MutableLazyImporter[Any]")
@@ -42,26 +31,6 @@ class _SupportsKeysAndGetItem(Protocol[_KT, _VT_co]):
4231
def __getitem__(self, __key: _KT) -> _VT_co: ...
4332
def keys(self) -> Iterable[_KT]: ...
4433

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-
6534
_DictLike = Union[_SupportsKeysAndGetItem[str, str], Iterable[Tuple[str, str]]]
6635
_ReduceTuple = Tuple[Callable[[str, Dict[str, str]], _T], Tuple[str, Dict[str, str]]]
6736

@@ -114,7 +83,7 @@ def module(self) -> types.ModuleType:
11483
return self._module
11584

11685
@property
117-
def imports(self) -> _MappingProtocol[str, str]:
86+
def imports(self) -> Mapping[str, str]:
11887
""":class:`types.MappingProxyType[str, str]<types.MappingProxyType>`: Get a mapping that maps object names to their module name.""" # noqa: E501
11988
return self._imports
12089

@@ -127,7 +96,7 @@ def __init__(
12796
if not isinstance(module, types.ModuleType):
12897
raise TypeError(f"Expected a module, not {type(module).__name__}")
12998
self._module = module
130-
self._imports = cast("_MappingProtocol[str, str]", types.MappingProxyType(dict(imports)))
99+
self._imports: Mapping[str, str] = types.MappingProxyType(dict(imports))
131100

132101
@classmethod
133102
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:
158127
def __reduce__(self: _ST1) -> _ReduceTuple[_ST1]:
159128
"""Helper for :mod:`pickle`."""
160129
cls = type(self)
161-
args = (self.module.__name__, self.imports.copy())
130+
args = (self.module.__name__, self.imports.copy()) # type: ignore[attr-defined]
162131
return cls.from_name, args
163132

164133
def __copy__(self: _ST1) -> _ST1:
@@ -179,8 +148,7 @@ def __hash__(self) -> int:
179148

180149
def __eq__(self, value: object) -> bool:
181150
"""Implement :meth:`self == value<object.__eq__>`."""
182-
cls = type(self)
183-
if not isinstance(value, cls):
151+
if not isinstance(value, LazyImporter):
184152
return NotImplemented
185153
return self.module is value.module and self.imports == value.imports
186154

0 commit comments

Comments
 (0)