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' )
@@ -1392,16 +1356,19 @@ class PR(Protocol[T, S]):
13921356 def meth (self ): pass
13931357 class P (PR [int , T ], Protocol [T ]):
13941358 y = 1
1395- self .assertIsSubclass (PR [int , T ], PR )
1396- self .assertIsSubclass (P [str ], PR )
13971359 with self .assertRaises (TypeError ):
1398- PR [int ]
1360+ issubclass ( PR [int , T ], PR )
13991361 with self .assertRaises (TypeError ):
1400- P [ int , str ]
1362+ issubclass ( P [ str ], PR )
14011363 with self .assertRaises (TypeError ):
1402- PR [int , 1 ]
1364+ PR [int ]
14031365 with self .assertRaises (TypeError ):
1404- PR [int , ClassVar ]
1366+ P [int , str ]
1367+ if not TYPING_3_10_0 :
1368+ with self .assertRaises (TypeError ):
1369+ PR [int , 1 ]
1370+ with self .assertRaises (TypeError ):
1371+ PR [int , ClassVar ]
14051372 class C (PR [int , T ]): pass
14061373 self .assertIsInstance (C [str ](), C )
14071374
@@ -1413,11 +1380,8 @@ class PR(Protocol, Generic[T, S]):
14131380 def meth (self ): pass
14141381 class P (PR [int , str ], Protocol ):
14151382 y = 1
1416- if not PEP_560 :
1383+ with self . assertRaises ( TypeError ) :
14171384 self .assertIsSubclass (PR [int , str ], PR )
1418- else :
1419- with self .assertRaises (TypeError ):
1420- self .assertIsSubclass (PR [int , str ], PR )
14211385 self .assertIsSubclass (P , PR )
14221386 with self .assertRaises (TypeError ):
14231387 PR [int ]
@@ -1448,7 +1412,6 @@ def __init__(self):
14481412 self .test = 'OK'
14491413 self .assertEqual (C [int ]().test , 'OK' )
14501414
1451- @skipUnless (not OLD_GENERICS , "New style generics required" )
14521415 def test_protocols_bad_subscripts (self ):
14531416 T = TypeVar ('T' )
14541417 S = TypeVar ('S' )
@@ -1465,9 +1428,6 @@ def test_generic_protocols_repr(self):
14651428 T = TypeVar ('T' )
14661429 S = TypeVar ('S' )
14671430 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' ))
14711431 self .assertTrue (repr (P [T , S ]).endswith ('P[~T, ~S]' ))
14721432 self .assertTrue (repr (P [int , str ]).endswith ('P[int, str]' ))
14731433
@@ -1480,13 +1440,10 @@ class P(Protocol[T, S]): pass
14801440 self .assertEqual (P [T , T ][Tuple [T , S ]][int , str ],
14811441 P [Tuple [int , str ], Tuple [int , str ]])
14821442
1483- @skipUnless (not OLD_GENERICS , "New style generics required" )
14841443 def test_generic_protocols_special_from_generic (self ):
14851444 T = TypeVar ('T' )
14861445 class P (Protocol [T ]): pass
14871446 self .assertEqual (P .__parameters__ , (T ,))
1488- self .assertIs (P .__args__ , None )
1489- self .assertIs (P .__origin__ , None )
14901447 self .assertEqual (P [int ].__parameters__ , ())
14911448 self .assertEqual (P [int ].__args__ , (int ,))
14921449 self .assertIs (P [int ].__origin__ , P )
@@ -1517,9 +1474,6 @@ def meth(self):
15171474 self .assertEqual (typing_extensions ._get_protocol_attrs (PR ), {'x' })
15181475 self .assertEqual (frozenset (typing_extensions ._get_protocol_attrs (PG )),
15191476 frozenset ({'x' , 'meth' }))
1520- if not PEP_560 :
1521- self .assertEqual (frozenset (typing_extensions ._get_protocol_attrs (PG [int ])),
1522- frozenset ({'x' , 'meth' }))
15231477
15241478 def test_no_runtime_deco_on_nominal (self ):
15251479 with self .assertRaises (TypeError ):
@@ -1747,7 +1701,6 @@ def test_optional_keys(self):
17471701 assert Point2Dor3D .__required_keys__ == frozenset (['x' , 'y' ])
17481702 assert Point2Dor3D .__optional_keys__ == frozenset (['z' ])
17491703
1750- @skipUnless (PEP_560 , "runtime support for Required and NotRequired requires PEP 560" )
17511704 def test_required_notrequired_keys (self ):
17521705 assert NontotalMovie .__required_keys__ == frozenset ({'title' })
17531706 assert NontotalMovie .__optional_keys__ == frozenset ({'year' })
@@ -1821,16 +1774,14 @@ def test_flatten(self):
18211774 A = Annotated [Annotated [int , 4 ], 5 ]
18221775 self .assertEqual (A , Annotated [int , 4 , 5 ])
18231776 self .assertEqual (A .__metadata__ , (4 , 5 ))
1824- if PEP_560 :
1825- self .assertEqual (A .__origin__ , int )
1777+ self .assertEqual (A .__origin__ , int )
18261778
18271779 def test_specialize (self ):
18281780 L = Annotated [List [T ], "my decoration" ]
18291781 LI = Annotated [List [int ], "my decoration" ]
18301782 self .assertEqual (L [int ], Annotated [List [int ], "my decoration" ])
18311783 self .assertEqual (L [int ].__metadata__ , ("my decoration" ,))
1832- if PEP_560 :
1833- self .assertEqual (L [int ].__origin__ , List [int ])
1784+ self .assertEqual (L [int ].__origin__ , List [int ])
18341785 with self .assertRaises (TypeError ):
18351786 LI [int ]
18361787 with self .assertRaises (TypeError ):
@@ -1934,7 +1885,6 @@ def test_cannot_check_subclass(self):
19341885 with self .assertRaises (TypeError ):
19351886 issubclass (int , Annotated [int , "positive" ])
19361887
1937- @skipUnless (PEP_560 , "pickle support was added with PEP 560" )
19381888 def test_pickle (self ):
19391889 samples = [typing .Any , typing .Union [int , str ],
19401890 typing .Optional [str ], Tuple [int , ...],
@@ -2000,7 +1950,6 @@ def test_annotated_in_other_types(self):
20001950 self .assertEqual (X [int ], List [Annotated [int , 5 ]])
20011951
20021952
2003- @skipUnless (PEP_560 , "Python 3.7 required" )
20041953class GetTypeHintsTests (BaseTestCase ):
20051954 def test_get_type_hints (self ):
20061955 def foobar (x : List ['X' ]): ...
@@ -2355,9 +2304,7 @@ def baz(self) -> "LiteralString": ...
23552304 self .assertEqual (gth (Foo .bar ), {'return' : LiteralString })
23562305 self .assertEqual (gth (Foo .baz ), {'return' : LiteralString })
23572306
2358- @skipUnless (PEP_560 , "Python 3.7+ required" )
23592307 def test_get_origin (self ):
2360- from typing_extensions import get_origin
23612308 self .assertIsNone (get_origin (LiteralString ))
23622309
23632310 def test_repr (self ):
@@ -2510,7 +2457,6 @@ def test_union(self):
25102457 Union
25112458 )
25122459
2513- @skipUnless (PEP_560 , "Unimplemented for 3.6" )
25142460 def test_concatenation (self ):
25152461 Xs = TypeVarTuple ('Xs' )
25162462 self .assertEqual (Tuple [int , Unpack [Xs ]].__args__ , (int , Unpack [Xs ]))
@@ -2523,7 +2469,6 @@ class C(Generic[Unpack[Xs]]): pass
25232469 self .assertEqual (C [int , Unpack [Xs ], str ].__args__ ,
25242470 (int , Unpack [Xs ], str ))
25252471
2526- @skipUnless (PEP_560 , "Unimplemented for 3.6" )
25272472 def test_class (self ):
25282473 Ts = TypeVarTuple ('Ts' )
25292474
@@ -2766,8 +2711,7 @@ def test_typing_extensions_includes_standard(self):
27662711 self .assertIn ("Concatenate" , a )
27672712
27682713 self .assertIn ('Annotated' , a )
2769- if PEP_560 :
2770- self .assertIn ('get_type_hints' , a )
2714+ self .assertIn ('get_type_hints' , a )
27712715
27722716 self .assertIn ('Awaitable' , a )
27732717 self .assertIn ('AsyncIterator' , a )
0 commit comments