3434from unittest .mock import Mock
3535from 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
562552def 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}
909906if 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
928915def builtin_checker_lookup (
0 commit comments