diff --git a/src/defer_imports/__init__.py b/src/defer_imports/__init__.py index ad72e9b..1497278 100644 --- a/src/defer_imports/__init__.py +++ b/src/defer_imports/__init__.py @@ -134,8 +134,11 @@ def _final(f: object) -> object: if sys.version_info >= (3, 10): # pragma: >=3.10 cover - _TypeAlias: typing.TypeAlias = "typing.TypeAlias" - _TypeGuard: typing.TypeAlias = "typing.TypeGuard" + if TYPE_CHECKING: + from typing import TypeAlias as _TypeAlias, TypeGuard as _TypeGuard + else: + _TypeAlias: typing.TypeAlias = "typing.TypeAlias" + _TypeGuard: typing.TypeAlias = "typing.TypeGuard" elif TYPE_CHECKING: from typing_extensions import TypeAlias as _TypeAlias, TypeGuard as _TypeGuard else: # pragma: <3.10 cover @@ -157,7 +160,10 @@ class _TypeGuard(metaclass=_PlaceholderMeta): if sys.version_info >= (3, 11): # pragma: >=3.11 cover - _Self: _TypeAlias = "typing.Self" + if TYPE_CHECKING: + from typing import Self as _Self + else: + _Self: _TypeAlias = "typing.Self" elif TYPE_CHECKING: from typing_extensions import Self as _Self else: # pragma: <3.11 cover