2323from typing_extensions import Awaitable , AsyncIterator , AsyncContextManager , Required , NotRequired
2424from typing_extensions import Protocol , runtime , runtime_checkable , Annotated , overload , final , is_typeddict
2525from typing_extensions import TypeVarTuple , Unpack , dataclass_transform , reveal_type , Never , assert_never , LiteralString
26- try :
27- from typing_extensions import get_type_hints
28- except ImportError :
29- from typing import get_type_hints
30-
31- PEP_560 = sys .version_info [:3 ] >= (3 , 7 , 0 )
32-
33- OLD_GENERICS = False
34- try :
35- from typing import _type_vars , _next_in_mro , _type_check # noqa
36- except ImportError :
37- OLD_GENERICS = True
26+ from typing_extensions import get_type_hints , get_origin , get_args
3827
3928# Flags used to mark tests that only apply after a specific
4029# version of the typing module.
41- TYPING_3_6_1 = sys .version_info [:3 ] >= (3 , 6 , 1 )
4230TYPING_3_8_0 = sys .version_info [:3 ] >= (3 , 8 , 0 )
4331TYPING_3_10_0 = sys .version_info [:3 ] >= (3 , 10 , 0 )
4432TYPING_3_11_0 = sys .version_info [:3 ] >= (3 , 11 , 0 )
4533
46- # For typing versions where instantiating collection
47- # types are allowed.
48- #
49- # See https://github.com/python/typing/issues/367
50- CAN_INSTANTIATE_COLLECTIONS = TYPING_3_6_1
51-
5234
5335class BaseTestCase (TestCase ):
5436 def assertIsSubclass (self , cls , class_or_tuple , msg = None ):
@@ -78,9 +60,7 @@ def test_equality(self):
7860 self .assertIs (self .bottom_type , self .bottom_type )
7961 self .assertNotEqual (self .bottom_type , None )
8062
81- @skipUnless (PEP_560 , "Python 3.7+ required" )
8263 def test_get_origin (self ):
83- from typing_extensions import get_origin
8464 self .assertIs (get_origin (self .bottom_type ), None )
8565
8666 def test_instance_type_error (self ):
@@ -621,11 +601,8 @@ def test_final_forward_ref(self):
621601 self .assertNotEqual (gth (Loop , globals ())['attr' ], Final )
622602
623603
624- @skipUnless (PEP_560 , "Python 3.7+ required" )
625604class GetUtilitiesTestCase (TestCase ):
626605 def test_get_origin (self ):
627- from typing_extensions import get_origin
628-
629606 T = TypeVar ('T' )
630607 P = ParamSpec ('P' )
631608 Ts = TypeVarTuple ('Ts' )
@@ -655,8 +632,6 @@ class C(Generic[T]): pass
655632 self .assertIs (get_origin (Unpack ), None )
656633
657634 def test_get_args (self ):
658- from typing_extensions import get_args
659-
660635 T = TypeVar ('T' )
661636 Ts = TypeVarTuple ('Ts' )
662637 class C (Generic [T ]): pass
@@ -767,7 +742,6 @@ class MyDeque(typing_extensions.Deque[int]): ...
767742 def test_counter (self ):
768743 self .assertIsSubclass (collections .Counter , typing_extensions .Counter )
769744
770- @skipUnless (CAN_INSTANTIATE_COLLECTIONS , "Behavior added in typing 3.6.1" )
771745 def test_defaultdict_instantiation (self ):
772746 self .assertIs (
773747 type (typing_extensions .DefaultDict ()),
@@ -790,7 +764,6 @@ class MyDefDict(typing_extensions.DefaultDict[str, int]):
790764 self .assertIsSubclass (MyDefDict , collections .defaultdict )
791765 self .assertNotIsSubclass (collections .defaultdict , MyDefDict )
792766
793- @skipUnless (CAN_INSTANTIATE_COLLECTIONS , "Behavior added in typing 3.6.1" )
794767 def test_ordereddict_instantiation (self ):
795768 self .assertIs (
796769 type (typing_extensions .OrderedDict ()),
@@ -844,10 +817,7 @@ def test_counter_instantiation(self):
844817 self .assertIs (type (typing_extensions .Counter [int ]()), collections .Counter )
845818 class C (typing_extensions .Counter [T ]): ...
846819 self .assertIs (type (C [int ]()), C )
847- if not PEP_560 :
848- self .assertEqual (C .__bases__ , (typing_extensions .Counter ,))
849- else :
850- self .assertEqual (C .__bases__ , (collections .Counter , typing .Generic ))
820+ self .assertEqual (C .__bases__ , (collections .Counter , typing .Generic ))
851821
852822 def test_counter_subclass_instantiation (self ):
853823
@@ -922,9 +892,8 @@ def manager():
922892 cm = manager ()
923893 self .assertNotIsInstance (cm , typing_extensions .AsyncContextManager )
924894 self .assertEqual (typing_extensions .AsyncContextManager [int ].__args__ , (int ,))
925- if TYPING_3_6_1 :
926- with self .assertRaises (TypeError ):
927- isinstance (42 , typing_extensions .AsyncContextManager [int ])
895+ with self .assertRaises (TypeError ):
896+ isinstance (42 , typing_extensions .AsyncContextManager [int ])
928897 with self .assertRaises (TypeError ):
929898 typing_extensions .AsyncContextManager [int , str ]
930899
@@ -1189,10 +1158,6 @@ def x(self): ...
11891158 self .assertIsSubclass (C , P )
11901159 self .assertIsSubclass (C , PG )
11911160 self .assertIsSubclass (BadP , PG )
1192- if not PEP_560 :
1193- self .assertIsSubclass (PG [int ], PG )
1194- self .assertIsSubclass (BadPG [int ], P )
1195- self .assertIsSubclass (BadPG [T ], PG )
11961161 with self .assertRaises (TypeError ):
11971162 issubclass (C , PG [T ])
11981163 with self .assertRaises (TypeError ):
@@ -1383,7 +1348,6 @@ class C: pass
13831348 with self .assertRaises (TypeError ):
13841349 issubclass (C (), P )
13851350
1386- @skipUnless (not OLD_GENERICS , "New style generics required" )
13871351 def test_defining_generic_protocols (self ):
13881352 T = TypeVar ('T' )
13891353 S = TypeVar ('S' )
@@ -1413,11 +1377,8 @@ class PR(Protocol, Generic[T, S]):
14131377 def meth (self ): pass
14141378 class P (PR [int , str ], Protocol ):
14151379 y = 1
1416- if not PEP_560 :
1380+ with self . assertRaises ( TypeError ) :
14171381 self .assertIsSubclass (PR [int , str ], PR )
1418- else :
1419- with self .assertRaises (TypeError ):
1420- self .assertIsSubclass (PR [int , str ], PR )
14211382 self .assertIsSubclass (P , PR )
14221383 with self .assertRaises (TypeError ):
14231384 PR [int ]
@@ -1448,7 +1409,6 @@ def __init__(self):
14481409 self .test = 'OK'
14491410 self .assertEqual (C [int ]().test , 'OK' )
14501411
1451- @skipUnless (not OLD_GENERICS , "New style generics required" )
14521412 def test_protocols_bad_subscripts (self ):
14531413 T = TypeVar ('T' )
14541414 S = TypeVar ('S' )
@@ -1465,9 +1425,6 @@ def test_generic_protocols_repr(self):
14651425 T = TypeVar ('T' )
14661426 S = TypeVar ('S' )
14671427 class P (Protocol [T , S ]): pass
1468- # After PEP 560 unsubscripted generics have a standard repr.
1469- if not PEP_560 :
1470- self .assertTrue (repr (P ).endswith ('P' ))
14711428 self .assertTrue (repr (P [T , S ]).endswith ('P[~T, ~S]' ))
14721429 self .assertTrue (repr (P [int , str ]).endswith ('P[int, str]' ))
14731430
@@ -1480,7 +1437,6 @@ class P(Protocol[T, S]): pass
14801437 self .assertEqual (P [T , T ][Tuple [T , S ]][int , str ],
14811438 P [Tuple [int , str ], Tuple [int , str ]])
14821439
1483- @skipUnless (not OLD_GENERICS , "New style generics required" )
14841440 def test_generic_protocols_special_from_generic (self ):
14851441 T = TypeVar ('T' )
14861442 class P (Protocol [T ]): pass
@@ -1517,9 +1473,6 @@ def meth(self):
15171473 self .assertEqual (typing_extensions ._get_protocol_attrs (PR ), {'x' })
15181474 self .assertEqual (frozenset (typing_extensions ._get_protocol_attrs (PG )),
15191475 frozenset ({'x' , 'meth' }))
1520- if not PEP_560 :
1521- self .assertEqual (frozenset (typing_extensions ._get_protocol_attrs (PG [int ])),
1522- frozenset ({'x' , 'meth' }))
15231476
15241477 def test_no_runtime_deco_on_nominal (self ):
15251478 with self .assertRaises (TypeError ):
@@ -1747,7 +1700,6 @@ def test_optional_keys(self):
17471700 assert Point2Dor3D .__required_keys__ == frozenset (['x' , 'y' ])
17481701 assert Point2Dor3D .__optional_keys__ == frozenset (['z' ])
17491702
1750- @skipUnless (PEP_560 , "runtime support for Required and NotRequired requires PEP 560" )
17511703 def test_required_notrequired_keys (self ):
17521704 assert NontotalMovie .__required_keys__ == frozenset ({'title' })
17531705 assert NontotalMovie .__optional_keys__ == frozenset ({'year' })
@@ -1821,16 +1773,14 @@ def test_flatten(self):
18211773 A = Annotated [Annotated [int , 4 ], 5 ]
18221774 self .assertEqual (A , Annotated [int , 4 , 5 ])
18231775 self .assertEqual (A .__metadata__ , (4 , 5 ))
1824- if PEP_560 :
1825- self .assertEqual (A .__origin__ , int )
1776+ self .assertEqual (A .__origin__ , int )
18261777
18271778 def test_specialize (self ):
18281779 L = Annotated [List [T ], "my decoration" ]
18291780 LI = Annotated [List [int ], "my decoration" ]
18301781 self .assertEqual (L [int ], Annotated [List [int ], "my decoration" ])
18311782 self .assertEqual (L [int ].__metadata__ , ("my decoration" ,))
1832- if PEP_560 :
1833- self .assertEqual (L [int ].__origin__ , List [int ])
1783+ self .assertEqual (L [int ].__origin__ , List [int ])
18341784 with self .assertRaises (TypeError ):
18351785 LI [int ]
18361786 with self .assertRaises (TypeError ):
@@ -1934,7 +1884,6 @@ def test_cannot_check_subclass(self):
19341884 with self .assertRaises (TypeError ):
19351885 issubclass (int , Annotated [int , "positive" ])
19361886
1937- @skipUnless (PEP_560 , "pickle support was added with PEP 560" )
19381887 def test_pickle (self ):
19391888 samples = [typing .Any , typing .Union [int , str ],
19401889 typing .Optional [str ], Tuple [int , ...],
@@ -2000,7 +1949,6 @@ def test_annotated_in_other_types(self):
20001949 self .assertEqual (X [int ], List [Annotated [int , 5 ]])
20011950
20021951
2003- @skipUnless (PEP_560 , "Python 3.7 required" )
20041952class GetTypeHintsTests (BaseTestCase ):
20051953 def test_get_type_hints (self ):
20061954 def foobar (x : List ['X' ]): ...
@@ -2355,9 +2303,7 @@ def baz(self) -> "LiteralString": ...
23552303 self .assertEqual (gth (Foo .bar ), {'return' : LiteralString })
23562304 self .assertEqual (gth (Foo .baz ), {'return' : LiteralString })
23572305
2358- @skipUnless (PEP_560 , "Python 3.7+ required" )
23592306 def test_get_origin (self ):
2360- from typing_extensions import get_origin
23612307 self .assertIsNone (get_origin (LiteralString ))
23622308
23632309 def test_repr (self ):
@@ -2510,7 +2456,6 @@ def test_union(self):
25102456 Union
25112457 )
25122458
2513- @skipUnless (PEP_560 , "Unimplemented for 3.6" )
25142459 def test_concatenation (self ):
25152460 Xs = TypeVarTuple ('Xs' )
25162461 self .assertEqual (Tuple [int , Unpack [Xs ]].__args__ , (int , Unpack [Xs ]))
@@ -2523,7 +2468,6 @@ class C(Generic[Unpack[Xs]]): pass
25232468 self .assertEqual (C [int , Unpack [Xs ], str ].__args__ ,
25242469 (int , Unpack [Xs ], str ))
25252470
2526- @skipUnless (PEP_560 , "Unimplemented for 3.6" )
25272471 def test_class (self ):
25282472 Ts = TypeVarTuple ('Ts' )
25292473
@@ -2766,8 +2710,7 @@ def test_typing_extensions_includes_standard(self):
27662710 self .assertIn ("Concatenate" , a )
27672711
27682712 self .assertIn ('Annotated' , a )
2769- if PEP_560 :
2770- self .assertIn ('get_type_hints' , a )
2713+ self .assertIn ('get_type_hints' , a )
27712714
27722715 self .assertIn ('Awaitable' , a )
27732716 self .assertIn ('AsyncIterator' , a )
0 commit comments