23
23
from typing_extensions import Awaitable , AsyncIterator , AsyncContextManager , Required , NotRequired
24
24
from typing_extensions import Protocol , runtime , runtime_checkable , Annotated , overload , final , is_typeddict
25
25
from 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
38
27
39
28
# Flags used to mark tests that only apply after a specific
40
29
# version of the typing module.
41
- TYPING_3_6_1 = sys .version_info [:3 ] >= (3 , 6 , 1 )
42
30
TYPING_3_8_0 = sys .version_info [:3 ] >= (3 , 8 , 0 )
43
31
TYPING_3_10_0 = sys .version_info [:3 ] >= (3 , 10 , 0 )
44
32
TYPING_3_11_0 = sys .version_info [:3 ] >= (3 , 11 , 0 )
45
33
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
-
52
34
53
35
class BaseTestCase (TestCase ):
54
36
def assertIsSubclass (self , cls , class_or_tuple , msg = None ):
@@ -78,9 +60,7 @@ def test_equality(self):
78
60
self .assertIs (self .bottom_type , self .bottom_type )
79
61
self .assertNotEqual (self .bottom_type , None )
80
62
81
- @skipUnless (PEP_560 , "Python 3.7+ required" )
82
63
def test_get_origin (self ):
83
- from typing_extensions import get_origin
84
64
self .assertIs (get_origin (self .bottom_type ), None )
85
65
86
66
def test_instance_type_error (self ):
@@ -621,11 +601,8 @@ def test_final_forward_ref(self):
621
601
self .assertNotEqual (gth (Loop , globals ())['attr' ], Final )
622
602
623
603
624
- @skipUnless (PEP_560 , "Python 3.7+ required" )
625
604
class GetUtilitiesTestCase (TestCase ):
626
605
def test_get_origin (self ):
627
- from typing_extensions import get_origin
628
-
629
606
T = TypeVar ('T' )
630
607
P = ParamSpec ('P' )
631
608
Ts = TypeVarTuple ('Ts' )
@@ -655,8 +632,6 @@ class C(Generic[T]): pass
655
632
self .assertIs (get_origin (Unpack ), None )
656
633
657
634
def test_get_args (self ):
658
- from typing_extensions import get_args
659
-
660
635
T = TypeVar ('T' )
661
636
Ts = TypeVarTuple ('Ts' )
662
637
class C (Generic [T ]): pass
@@ -767,7 +742,6 @@ class MyDeque(typing_extensions.Deque[int]): ...
767
742
def test_counter (self ):
768
743
self .assertIsSubclass (collections .Counter , typing_extensions .Counter )
769
744
770
- @skipUnless (CAN_INSTANTIATE_COLLECTIONS , "Behavior added in typing 3.6.1" )
771
745
def test_defaultdict_instantiation (self ):
772
746
self .assertIs (
773
747
type (typing_extensions .DefaultDict ()),
@@ -790,7 +764,6 @@ class MyDefDict(typing_extensions.DefaultDict[str, int]):
790
764
self .assertIsSubclass (MyDefDict , collections .defaultdict )
791
765
self .assertNotIsSubclass (collections .defaultdict , MyDefDict )
792
766
793
- @skipUnless (CAN_INSTANTIATE_COLLECTIONS , "Behavior added in typing 3.6.1" )
794
767
def test_ordereddict_instantiation (self ):
795
768
self .assertIs (
796
769
type (typing_extensions .OrderedDict ()),
@@ -844,10 +817,7 @@ def test_counter_instantiation(self):
844
817
self .assertIs (type (typing_extensions .Counter [int ]()), collections .Counter )
845
818
class C (typing_extensions .Counter [T ]): ...
846
819
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 ))
851
821
852
822
def test_counter_subclass_instantiation (self ):
853
823
@@ -922,9 +892,8 @@ def manager():
922
892
cm = manager ()
923
893
self .assertNotIsInstance (cm , typing_extensions .AsyncContextManager )
924
894
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 ])
928
897
with self .assertRaises (TypeError ):
929
898
typing_extensions .AsyncContextManager [int , str ]
930
899
@@ -1189,10 +1158,6 @@ def x(self): ...
1189
1158
self .assertIsSubclass (C , P )
1190
1159
self .assertIsSubclass (C , PG )
1191
1160
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 )
1196
1161
with self .assertRaises (TypeError ):
1197
1162
issubclass (C , PG [T ])
1198
1163
with self .assertRaises (TypeError ):
@@ -1383,7 +1348,6 @@ class C: pass
1383
1348
with self .assertRaises (TypeError ):
1384
1349
issubclass (C (), P )
1385
1350
1386
- @skipUnless (not OLD_GENERICS , "New style generics required" )
1387
1351
def test_defining_generic_protocols (self ):
1388
1352
T = TypeVar ('T' )
1389
1353
S = TypeVar ('S' )
@@ -1392,16 +1356,19 @@ class PR(Protocol[T, S]):
1392
1356
def meth (self ): pass
1393
1357
class P (PR [int , T ], Protocol [T ]):
1394
1358
y = 1
1395
- self .assertIsSubclass (PR [int , T ], PR )
1396
- self .assertIsSubclass (P [str ], PR )
1397
1359
with self .assertRaises (TypeError ):
1398
- PR [int ]
1360
+ issubclass ( PR [int , T ], PR )
1399
1361
with self .assertRaises (TypeError ):
1400
- P [ int , str ]
1362
+ issubclass ( P [ str ], PR )
1401
1363
with self .assertRaises (TypeError ):
1402
- PR [int , 1 ]
1364
+ PR [int ]
1403
1365
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 ]
1405
1372
class C (PR [int , T ]): pass
1406
1373
self .assertIsInstance (C [str ](), C )
1407
1374
@@ -1413,11 +1380,8 @@ class PR(Protocol, Generic[T, S]):
1413
1380
def meth (self ): pass
1414
1381
class P (PR [int , str ], Protocol ):
1415
1382
y = 1
1416
- if not PEP_560 :
1383
+ with self . assertRaises ( TypeError ) :
1417
1384
self .assertIsSubclass (PR [int , str ], PR )
1418
- else :
1419
- with self .assertRaises (TypeError ):
1420
- self .assertIsSubclass (PR [int , str ], PR )
1421
1385
self .assertIsSubclass (P , PR )
1422
1386
with self .assertRaises (TypeError ):
1423
1387
PR [int ]
@@ -1448,7 +1412,6 @@ def __init__(self):
1448
1412
self .test = 'OK'
1449
1413
self .assertEqual (C [int ]().test , 'OK' )
1450
1414
1451
- @skipUnless (not OLD_GENERICS , "New style generics required" )
1452
1415
def test_protocols_bad_subscripts (self ):
1453
1416
T = TypeVar ('T' )
1454
1417
S = TypeVar ('S' )
@@ -1465,9 +1428,6 @@ def test_generic_protocols_repr(self):
1465
1428
T = TypeVar ('T' )
1466
1429
S = TypeVar ('S' )
1467
1430
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' ))
1471
1431
self .assertTrue (repr (P [T , S ]).endswith ('P[~T, ~S]' ))
1472
1432
self .assertTrue (repr (P [int , str ]).endswith ('P[int, str]' ))
1473
1433
@@ -1480,13 +1440,10 @@ class P(Protocol[T, S]): pass
1480
1440
self .assertEqual (P [T , T ][Tuple [T , S ]][int , str ],
1481
1441
P [Tuple [int , str ], Tuple [int , str ]])
1482
1442
1483
- @skipUnless (not OLD_GENERICS , "New style generics required" )
1484
1443
def test_generic_protocols_special_from_generic (self ):
1485
1444
T = TypeVar ('T' )
1486
1445
class P (Protocol [T ]): pass
1487
1446
self .assertEqual (P .__parameters__ , (T ,))
1488
- self .assertIs (P .__args__ , None )
1489
- self .assertIs (P .__origin__ , None )
1490
1447
self .assertEqual (P [int ].__parameters__ , ())
1491
1448
self .assertEqual (P [int ].__args__ , (int ,))
1492
1449
self .assertIs (P [int ].__origin__ , P )
@@ -1517,9 +1474,6 @@ def meth(self):
1517
1474
self .assertEqual (typing_extensions ._get_protocol_attrs (PR ), {'x' })
1518
1475
self .assertEqual (frozenset (typing_extensions ._get_protocol_attrs (PG )),
1519
1476
frozenset ({'x' , 'meth' }))
1520
- if not PEP_560 :
1521
- self .assertEqual (frozenset (typing_extensions ._get_protocol_attrs (PG [int ])),
1522
- frozenset ({'x' , 'meth' }))
1523
1477
1524
1478
def test_no_runtime_deco_on_nominal (self ):
1525
1479
with self .assertRaises (TypeError ):
@@ -1747,7 +1701,6 @@ def test_optional_keys(self):
1747
1701
assert Point2Dor3D .__required_keys__ == frozenset (['x' , 'y' ])
1748
1702
assert Point2Dor3D .__optional_keys__ == frozenset (['z' ])
1749
1703
1750
- @skipUnless (PEP_560 , "runtime support for Required and NotRequired requires PEP 560" )
1751
1704
def test_required_notrequired_keys (self ):
1752
1705
assert NontotalMovie .__required_keys__ == frozenset ({'title' })
1753
1706
assert NontotalMovie .__optional_keys__ == frozenset ({'year' })
@@ -1821,16 +1774,14 @@ def test_flatten(self):
1821
1774
A = Annotated [Annotated [int , 4 ], 5 ]
1822
1775
self .assertEqual (A , Annotated [int , 4 , 5 ])
1823
1776
self .assertEqual (A .__metadata__ , (4 , 5 ))
1824
- if PEP_560 :
1825
- self .assertEqual (A .__origin__ , int )
1777
+ self .assertEqual (A .__origin__ , int )
1826
1778
1827
1779
def test_specialize (self ):
1828
1780
L = Annotated [List [T ], "my decoration" ]
1829
1781
LI = Annotated [List [int ], "my decoration" ]
1830
1782
self .assertEqual (L [int ], Annotated [List [int ], "my decoration" ])
1831
1783
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 ])
1834
1785
with self .assertRaises (TypeError ):
1835
1786
LI [int ]
1836
1787
with self .assertRaises (TypeError ):
@@ -1934,7 +1885,6 @@ def test_cannot_check_subclass(self):
1934
1885
with self .assertRaises (TypeError ):
1935
1886
issubclass (int , Annotated [int , "positive" ])
1936
1887
1937
- @skipUnless (PEP_560 , "pickle support was added with PEP 560" )
1938
1888
def test_pickle (self ):
1939
1889
samples = [typing .Any , typing .Union [int , str ],
1940
1890
typing .Optional [str ], Tuple [int , ...],
@@ -2000,7 +1950,6 @@ def test_annotated_in_other_types(self):
2000
1950
self .assertEqual (X [int ], List [Annotated [int , 5 ]])
2001
1951
2002
1952
2003
- @skipUnless (PEP_560 , "Python 3.7 required" )
2004
1953
class GetTypeHintsTests (BaseTestCase ):
2005
1954
def test_get_type_hints (self ):
2006
1955
def foobar (x : List ['X' ]): ...
@@ -2355,9 +2304,7 @@ def baz(self) -> "LiteralString": ...
2355
2304
self .assertEqual (gth (Foo .bar ), {'return' : LiteralString })
2356
2305
self .assertEqual (gth (Foo .baz ), {'return' : LiteralString })
2357
2306
2358
- @skipUnless (PEP_560 , "Python 3.7+ required" )
2359
2307
def test_get_origin (self ):
2360
- from typing_extensions import get_origin
2361
2308
self .assertIsNone (get_origin (LiteralString ))
2362
2309
2363
2310
def test_repr (self ):
@@ -2510,7 +2457,6 @@ def test_union(self):
2510
2457
Union
2511
2458
)
2512
2459
2513
- @skipUnless (PEP_560 , "Unimplemented for 3.6" )
2514
2460
def test_concatenation (self ):
2515
2461
Xs = TypeVarTuple ('Xs' )
2516
2462
self .assertEqual (Tuple [int , Unpack [Xs ]].__args__ , (int , Unpack [Xs ]))
@@ -2523,7 +2469,6 @@ class C(Generic[Unpack[Xs]]): pass
2523
2469
self .assertEqual (C [int , Unpack [Xs ], str ].__args__ ,
2524
2470
(int , Unpack [Xs ], str ))
2525
2471
2526
- @skipUnless (PEP_560 , "Unimplemented for 3.6" )
2527
2472
def test_class (self ):
2528
2473
Ts = TypeVarTuple ('Ts' )
2529
2474
@@ -2766,8 +2711,7 @@ def test_typing_extensions_includes_standard(self):
2766
2711
self .assertIn ("Concatenate" , a )
2767
2712
2768
2713
self .assertIn ('Annotated' , a )
2769
- if PEP_560 :
2770
- self .assertIn ('get_type_hints' , a )
2714
+ self .assertIn ('get_type_hints' , a )
2771
2715
2772
2716
self .assertIn ('Awaitable' , a )
2773
2717
self .assertIn ('AsyncIterator' , a )
0 commit comments