Skip to content

Commit e23091a

Browse files
committed
Assume that typing_extensions is always installed.
Because we strictly depend upon typing_extensions both explicitly and implicitly, we don't need the code which checks if it isn't installed.
1 parent 016f813 commit e23091a

1 file changed

Lines changed: 10 additions & 23 deletions

File tree

src/typeguard/_checkers.py

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@
3434
from unittest.mock import Mock
3535
from weakref import WeakKeyDictionary
3636

37-
try:
38-
import typing_extensions
39-
except ImportError:
40-
typing_extensions = None # type: ignore[assignment]
37+
import typing_extensions
4138

4239
# Must use this because typing.is_typeddict does not recognize
4340
# TypedDict from typing_extensions, and as of version 4.12.0
@@ -548,15 +545,8 @@ def check_typevar(
548545
)
549546

550547

551-
if typing_extensions is None:
552-
553-
def _is_literal_type(typ: object) -> bool:
554-
return typ is typing.Literal
555-
556-
else:
557-
558-
def _is_literal_type(typ: object) -> bool:
559-
return typ is typing.Literal or typ is typing_extensions.Literal
548+
def _is_literal_type(typ: object) -> bool:
549+
return typ is typing.Literal or typ is typing_extensions.Literal
560550

561551

562552
def check_literal(
@@ -905,6 +895,13 @@ def check_type_internal(
905895
type: check_class,
906896
Type: check_class,
907897
Union: check_union,
898+
# On some versions of Python, these may simply be re-exports from "typing",
899+
# but exactly which Python versions is subject to change.
900+
# It's best to err on the safe side and just always specify these.
901+
typing_extensions.Literal: check_literal,
902+
typing_extensions.LiteralString: check_literal_string,
903+
typing_extensions.Self: check_self,
904+
typing_extensions.TypeGuard: check_typeguard,
908905
}
909906
if sys.version_info >= (3, 10):
910907
origin_type_checkers[types.UnionType] = check_uniontype
@@ -913,16 +910,6 @@ def check_type_internal(
913910
origin_type_checkers.update(
914911
{typing.LiteralString: check_literal_string, typing.Self: check_self}
915912
)
916-
if typing_extensions is not None:
917-
# On some Python versions, these may simply be re-exports from typing,
918-
# but exactly which Python versions is subject to change,
919-
# so it's best to err on the safe side
920-
# and update the dictionary on all Python versions
921-
# if typing_extensions is installed
922-
origin_type_checkers[typing_extensions.Literal] = check_literal
923-
origin_type_checkers[typing_extensions.LiteralString] = check_literal_string
924-
origin_type_checkers[typing_extensions.Self] = check_self
925-
origin_type_checkers[typing_extensions.TypeGuard] = check_typeguard
926913

927914

928915
def builtin_checker_lookup(

0 commit comments

Comments
 (0)