2424from abc import ABCMeta , abstractmethod
2525from dataclasses import dataclass
2626from typing import (
27- TYPE_CHECKING ,
2827 Any ,
2928 Callable ,
3029 Collection ,
4544 overload ,
4645)
4746
48- try :
49- from typing import NoReturn
50- except ImportError :
51- from typing_extensions import NoReturn
52-
53- # This is a messy, type-wise, because we not only have two potentially conflicting imports here
54- # The easiest way to make mypy happy here is to tell it the versions from typing_extensions are
55- # canonical. Since this typing_extensions import is only for mypy it'll work even without
56- # typing_extensions actually installed so all's good.
57- if TYPE_CHECKING :
58- from typing_extensions import Annotated , _AnnotatedAlias , get_type_hints
59- else :
60- # Ignoring errors here as typing_extensions stub doesn't know about those things yet
61- try :
62- from typing import Annotated , _AnnotatedAlias , get_type_hints
63- except ImportError :
64- from typing_extensions import Annotated , _AnnotatedAlias , get_type_hints
47+ from typing import Annotated , NoReturn , _AnnotatedAlias , get_type_hints # type: ignore[attr-defined]
6548
6649
6750__author__ = 'Alec Thomas <alec@swapoff.org>'
@@ -108,7 +91,7 @@ def wrapper(*args: Any, **kwargs: Any) -> Any:
10891InjectT = TypeVar ('InjectT' )
10992Inject = Annotated [InjectT , _inject_marker ]
11093"""An experimental way to declare injectable dependencies utilizing a `PEP 593`_ implementation
111- in Python 3.9 and backported to Python 3.7+ in `typing_extensions` .
94+ in Python 3.9.
11295
11396Those two declarations are equivalent::
11497
@@ -136,19 +119,18 @@ def fun(t: Inject[SomeType], s: SomeOtherType) -> None:
136119 A way to inspect how various injection declarations interact with each other.
137120
138121.. versionadded:: 0.18.0
139- .. note:: Requires Python 3.7 +.
122+ .. note:: Requires Python 3.9 +.
140123.. note::
141124
142125 If you're using mypy you need the version 0.750 or newer to fully type-check code using this
143126 construct.
144127
145128.. _PEP 593: https://www.python.org/dev/peps/pep-0593/
146- .. _typing_extensions: https://pypi.org/project/typing-extensions/
147129"""
148130
149131NoInject = Annotated [InjectT , _noinject_marker ]
150132"""An experimental way to declare noninjectable dependencies utilizing a `PEP 593`_ implementation
151- in Python 3.9 and backported to Python 3.7+ in `typing_extensions` .
133+ in Python 3.9.
152134
153135Since :func:`inject` declares all function's parameters to be injectable there needs to be a way
154136to opt out of it. This has been provided by :func:`noninjectable` but `noninjectable` suffers from
@@ -175,14 +157,13 @@ def fun(a: TypeA, b: NoInject[TypeB]) -> None:
175157 A way to inspect how various injection declarations interact with each other.
176158
177159.. versionadded:: 0.18.0
178- .. note:: Requires Python 3.7 +.
160+ .. note:: Requires Python 3.9 +.
179161.. note::
180162
181163 If you're using mypy you need the version 0.750 or newer to fully type-check code using this
182164 construct.
183165
184166.. _PEP 593: https://www.python.org/dev/peps/pep-0593/
185- .. _typing_extensions: https://pypi.org/project/typing-extensions/
186167"""
187168
188169
@@ -791,13 +772,7 @@ def _ensure_iterable(item_or_list: Union[T, List[T]]) -> List[T]:
791772
792773
793774def _punch_through_alias (type_ : Any ) -> type :
794- if (
795- sys .version_info < (3 , 10 )
796- and getattr (type_ , '__qualname__' , '' ) == 'NewType.<locals>.new_type'
797- or sys .version_info >= (3 , 10 )
798- and type (type_ ).__module__ == 'typing'
799- and type (type_ ).__name__ == 'NewType'
800- ):
775+ if type (type_ ).__module__ == 'typing' and type (type_ ).__name__ == 'NewType' :
801776 return type_ .__supertype__
802777 elif isinstance (type_ , _AnnotatedAlias ) and getattr (type_ , '__metadata__' , None ) is not None :
803778 return type_ .__origin__
0 commit comments