110
110
111
111
# Flags used to mark tests that only apply after a specific
112
112
# version of the typing module.
113
- TYPING_3_9_0 = sys .version_info [:3 ] >= (3 , 9 , 0 )
114
113
TYPING_3_10_0 = sys .version_info [:3 ] >= (3 , 10 , 0 )
115
114
116
115
# 3.11 makes runtime type checks (_type_check) more lenient.
@@ -1779,8 +1778,7 @@ class C(Generic[T]): pass
1779
1778
self .assertIs (get_origin (List ), list )
1780
1779
self .assertIs (get_origin (Tuple ), tuple )
1781
1780
self .assertIs (get_origin (Callable ), collections .abc .Callable )
1782
- if sys .version_info >= (3 , 9 ):
1783
- self .assertIs (get_origin (list [int ]), list )
1781
+ self .assertIs (get_origin (list [int ]), list )
1784
1782
self .assertIs (get_origin (list ), None )
1785
1783
self .assertIs (get_origin (P .args ), P )
1786
1784
self .assertIs (get_origin (P .kwargs ), P )
@@ -1817,20 +1815,18 @@ class C(Generic[T]): pass
1817
1815
self .assertEqual (get_args (List ), ())
1818
1816
self .assertEqual (get_args (Tuple ), ())
1819
1817
self .assertEqual (get_args (Callable ), ())
1820
- if sys .version_info >= (3 , 9 ):
1821
- self .assertEqual (get_args (list [int ]), (int ,))
1818
+ self .assertEqual (get_args (list [int ]), (int ,))
1822
1819
self .assertEqual (get_args (list ), ())
1823
- if sys .version_info >= (3 , 9 ):
1824
- # Support Python versions with and without the fix for
1825
- # https://bugs.python.org/issue42195
1826
- # The first variant is for 3.9.2+, the second for 3.9.0 and 1
1827
- self .assertIn (get_args (collections .abc .Callable [[int ], str ]),
1828
- (([int ], str ), ([[int ]], str )))
1829
- self .assertIn (get_args (collections .abc .Callable [[], str ]),
1830
- (([], str ), ([[]], str )))
1831
- self .assertEqual (get_args (collections .abc .Callable [..., str ]), (..., str ))
1820
+ # Support Python versions with and without the fix for
1821
+ # https://bugs.python.org/issue42195
1822
+ # The first variant is for 3.9.2+, the second for 3.9.0 and 1
1823
+ self .assertIn (get_args (collections .abc .Callable [[int ], str ]),
1824
+ (([int ], str ), ([[int ]], str )))
1825
+ self .assertIn (get_args (collections .abc .Callable [[], str ]),
1826
+ (([], str ), ([[]], str )))
1827
+ self .assertEqual (get_args (collections .abc .Callable [..., str ]), (..., str ))
1832
1828
P = ParamSpec ('P' )
1833
- # In 3.9 and lower we use typing_extensions's hacky implementation
1829
+ # In 3.9 we use typing_extensions's hacky implementation
1834
1830
# of ParamSpec, which gets incorrectly wrapped in a list
1835
1831
self .assertIn (get_args (Callable [P , int ]), [(P , int ), ([P ], int )])
1836
1832
self .assertEqual (get_args (Required [int ]), (int ,))
@@ -3808,7 +3804,7 @@ def __call__(self, *args: P.args, **kwargs: P.kwargs) -> T: ...
3808
3804
MemoizedFunc [[int , str , str ]]
3809
3805
3810
3806
if sys .version_info >= (3 , 10 ):
3811
- # These unfortunately don't pass on <= 3.9,
3807
+ # These unfortunately don't pass on 3.9,
3812
3808
# due to typing._type_check on older Python versions
3813
3809
X = MemoizedFunc [[int , str , str ], T , T2 ]
3814
3810
self .assertEqual (X .__parameters__ , (T , T2 ))
@@ -4553,7 +4549,7 @@ class PointDict3D(PointDict2D, total=False):
4553
4549
assert is_typeddict (PointDict2D ) is True
4554
4550
assert is_typeddict (PointDict3D ) is True
4555
4551
4556
- @skipUnless (HAS_FORWARD_MODULE , "ForwardRef.__forward_module__ was added in 3.9" )
4552
+ @skipUnless (HAS_FORWARD_MODULE , "ForwardRef.__forward_module__ was added in 3.9.7 " )
4557
4553
def test_get_type_hints_cross_module_subclass (self ):
4558
4554
self .assertNotIn ("_DoNotImport" , globals ())
4559
4555
self .assertEqual (
@@ -4696,11 +4692,9 @@ class WithImplicitAny(B):
4696
4692
with self .assertRaises (TypeError ):
4697
4693
WithImplicitAny [str ]
4698
4694
4699
- @skipUnless (TYPING_3_9_0 , "Was changed in 3.9" )
4700
4695
def test_non_generic_subscript (self ):
4701
4696
# For backward compatibility, subscription works
4702
4697
# on arbitrary TypedDict types.
4703
- # (But we don't attempt to backport this misfeature onto 3.8.)
4704
4698
class TD (TypedDict ):
4705
4699
a : T
4706
4700
A = TD [int ]
@@ -5163,7 +5157,7 @@ class C:
5163
5157
A .x = 5
5164
5158
self .assertEqual (C .x , 5 )
5165
5159
5166
- @skipIf (sys .version_info [:2 ] in (( 3 , 9 ), ( 3 , 10 )) , "Waiting for bpo-46491 bugfix." )
5160
+ @skipIf (sys .version_info [:2 ] == ( 3 , 10 ), "Waiting for https://github.com/python/cpython/issues/90649 bugfix." )
5167
5161
def test_special_form_containment (self ):
5168
5162
class C :
5169
5163
classvar : Annotated [ClassVar [int ], "a decoration" ] = 4
@@ -5475,21 +5469,20 @@ def test_valid_uses(self):
5475
5469
self .assertEqual (C2 .__parameters__ , (P , T ))
5476
5470
5477
5471
# Test collections.abc.Callable too.
5478
- if sys .version_info [:2 ] >= (3 , 9 ):
5479
- # Note: no tests for Callable.__parameters__ here
5480
- # because types.GenericAlias Callable is hardcoded to search
5481
- # for tp_name "TypeVar" in C. This was changed in 3.10.
5482
- C3 = collections .abc .Callable [P , int ]
5483
- self .assertEqual (C3 .__args__ , (P , int ))
5484
- C4 = collections .abc .Callable [P , T ]
5485
- self .assertEqual (C4 .__args__ , (P , T ))
5472
+ # Note: no tests for Callable.__parameters__ here
5473
+ # because types.GenericAlias Callable is hardcoded to search
5474
+ # for tp_name "TypeVar" in C. This was changed in 3.10.
5475
+ C3 = collections .abc .Callable [P , int ]
5476
+ self .assertEqual (C3 .__args__ , (P , int ))
5477
+ C4 = collections .abc .Callable [P , T ]
5478
+ self .assertEqual (C4 .__args__ , (P , T ))
5486
5479
5487
5480
# ParamSpec instances should also have args and kwargs attributes.
5488
5481
# Note: not in dir(P) because of __class__ hacks
5489
5482
self .assertTrue (hasattr (P , 'args' ))
5490
5483
self .assertTrue (hasattr (P , 'kwargs' ))
5491
5484
5492
- @skipIf ((3 , 10 , 0 ) <= sys .version_info [:3 ] <= (3 , 10 , 2 ), "Needs bpo-46676 ." )
5485
+ @skipIf ((3 , 10 , 0 ) <= sys .version_info [:3 ] <= (3 , 10 , 2 ), "Needs https://github.com/python/cpython/issues/90834 ." )
5493
5486
def test_args_kwargs (self ):
5494
5487
P = ParamSpec ('P' )
5495
5488
P_2 = ParamSpec ('P_2' )
@@ -5649,8 +5642,6 @@ class ProtoZ(Protocol[P]):
5649
5642
G10 = klass [int , Concatenate [str , P ]]
5650
5643
with self .subTest ("Check invalid form substitution" ):
5651
5644
self .assertEqual (G10 .__parameters__ , (P , ))
5652
- if sys .version_info < (3 , 9 ):
5653
- self .skipTest ("3.8 typing._type_subst does not support this substitution process" )
5654
5645
H10 = G10 [int ]
5655
5646
if (3 , 10 ) <= sys .version_info < (3 , 11 , 3 ):
5656
5647
self .skipTest ("3.10-3.11.2 does not substitute Concatenate here" )
@@ -5780,9 +5771,6 @@ def test_valid_uses(self):
5780
5771
T = TypeVar ('T' )
5781
5772
for callable_variant in (Callable , collections .abc .Callable ):
5782
5773
with self .subTest (callable_variant = callable_variant ):
5783
- if not TYPING_3_9_0 and callable_variant is collections .abc .Callable :
5784
- self .skipTest ("Needs PEP 585" )
5785
-
5786
5774
C1 = callable_variant [Concatenate [int , P ], int ]
5787
5775
C2 = callable_variant [Concatenate [int , T , P ], T ]
5788
5776
self .assertEqual (C1 .__origin__ , C2 .__origin__ )
@@ -5830,7 +5818,7 @@ def test_invalid_uses(self):
5830
5818
):
5831
5819
Concatenate [(str ,), P ]
5832
5820
5833
- @skipUnless (TYPING_3_10_0 , "Missing backport to <= 3.9. See issue #48" )
5821
+ @skipUnless (TYPING_3_10_0 , "Missing backport to 3.9. See issue #48" )
5834
5822
def test_alias_subscription_with_ellipsis (self ):
5835
5823
P = ParamSpec ('P' )
5836
5824
X = Callable [Concatenate [int , P ], Any ]
@@ -6813,7 +6801,6 @@ class Y(Generic[T], NamedTuple):
6813
6801
with self .assertRaisesRegex (TypeError , f'Too many { things } ' ):
6814
6802
G [int , str ]
6815
6803
6816
- @skipUnless (TYPING_3_9_0 , "tuple.__class_getitem__ was added in 3.9" )
6817
6804
def test_non_generic_subscript_py39_plus (self ):
6818
6805
# For backward compatibility, subscription works
6819
6806
# on arbitrary NamedTuple types.
@@ -6828,19 +6815,6 @@ class Group(NamedTuple):
6828
6815
self .assertIs (type (a ), Group )
6829
6816
self .assertEqual (a , (1 , [2 ]))
6830
6817
6831
- @skipIf (TYPING_3_9_0 , "Test isn't relevant to 3.9+" )
6832
- def test_non_generic_subscript_error_message_py38 (self ):
6833
- class Group (NamedTuple ):
6834
- key : T
6835
- group : List [T ]
6836
-
6837
- with self .assertRaisesRegex (TypeError , 'not subscriptable' ):
6838
- Group [int ]
6839
-
6840
- for attr in ('__args__' , '__origin__' , '__parameters__' ):
6841
- with self .subTest (attr = attr ):
6842
- self .assertFalse (hasattr (Group , attr ))
6843
-
6844
6818
def test_namedtuple_keyword_usage (self ):
6845
6819
with self .assertWarnsRegex (
6846
6820
DeprecationWarning ,
@@ -6959,21 +6933,13 @@ def test_copy_and_pickle(self):
6959
6933
def test_docstring (self ):
6960
6934
self .assertIsInstance (NamedTuple .__doc__ , str )
6961
6935
6962
- @skipUnless (TYPING_3_9_0 , "NamedTuple was a class on 3.8 and lower" )
6963
- def test_same_as_typing_NamedTuple_39_plus (self ):
6936
+ def test_same_as_typing_NamedTuple (self ):
6964
6937
self .assertEqual (
6965
6938
set (dir (NamedTuple )) - {"__text_signature__" },
6966
6939
set (dir (typing .NamedTuple ))
6967
6940
)
6968
6941
self .assertIs (type (NamedTuple ), type (typing .NamedTuple ))
6969
6942
6970
- @skipIf (TYPING_3_9_0 , "tests are only relevant to <=3.8" )
6971
- def test_same_as_typing_NamedTuple_38_minus (self ):
6972
- self .assertEqual (
6973
- self .NestedEmployee .__annotations__ ,
6974
- self .NestedEmployee ._field_types
6975
- )
6976
-
6977
6943
def test_orig_bases (self ):
6978
6944
T = TypeVar ('T' )
6979
6945
@@ -7235,11 +7201,8 @@ def test_bound_errors(self):
7235
7201
r"Bound must be a type\. Got \(1, 2\)\." ):
7236
7202
TypeVar ('X' , bound = (1 , 2 ))
7237
7203
7238
- # Technically we could run it on later versions of 3.8,
7239
- # but that's not worth the effort.
7240
- @skipUnless (TYPING_3_9_0 , "Fix was not backported" )
7241
7204
def test_missing__name__ (self ):
7242
- # See bpo-39942
7205
+ # See https://github.com/python/cpython/issues/84123
7243
7206
code = ("import typing\n "
7244
7207
"T = typing.TypeVar('T')\n "
7245
7208
)
@@ -7420,9 +7383,8 @@ def test_allow_default_after_non_default_in_alias(self):
7420
7383
a1 = Callable [[T_default ], T ]
7421
7384
self .assertEqual (a1 .__args__ , (T_default , T ))
7422
7385
7423
- if sys .version_info >= (3 , 9 ):
7424
- a2 = dict [T_default , T ]
7425
- self .assertEqual (a2 .__args__ , (T_default , T ))
7386
+ a2 = dict [T_default , T ]
7387
+ self .assertEqual (a2 .__args__ , (T_default , T ))
7426
7388
7427
7389
a3 = typing .Dict [T_default , T ]
7428
7390
self .assertEqual (a3 .__args__ , (T_default , T ))
@@ -7602,7 +7564,6 @@ class D(B[str], float): pass
7602
7564
with self .assertRaisesRegex (TypeError , "Expected an instance of type" ):
7603
7565
get_original_bases (object ())
7604
7566
7605
- @skipUnless (TYPING_3_9_0 , "PEP 585 is yet to be" )
7606
7567
def test_builtin_generics (self ):
7607
7568
class E (list [T ]): pass
7608
7569
class F (list [int ]): pass
@@ -8848,7 +8809,6 @@ def test_fwdref_value_is_cached(self):
8848
8809
self .assertIs (evaluate_forward_ref (fr , globals = {"hello" : str }), str )
8849
8810
self .assertIs (evaluate_forward_ref (fr ), str )
8850
8811
8851
- @skipUnless (TYPING_3_9_0 , "Needs PEP 585 support" )
8852
8812
def test_fwdref_with_owner (self ):
8853
8813
self .assertEqual (
8854
8814
evaluate_forward_ref (typing .ForwardRef ("Counter[int]" ), owner = collections ),
@@ -8894,16 +8854,14 @@ class Y(Generic[Tx]):
8894
8854
with self .subTest ("nested string of TypeVar" ):
8895
8855
evaluated_ref2 = evaluate_forward_ref (typing .ForwardRef ("""Y["Y['Tx']"]""" ), locals = {"Y" : Y })
8896
8856
self .assertEqual (get_origin (evaluated_ref2 ), Y )
8897
- if not TYPING_3_9_0 :
8898
- self .skipTest ("Nested string 'Tx' stays ForwardRef in 3.8" )
8899
8857
self .assertEqual (get_args (evaluated_ref2 ), (Y [Tx ],))
8900
8858
8901
8859
with self .subTest ("nested string of TypeAliasType and alias" ):
8902
8860
# NOTE: Using Y here works for 3.10
8903
8861
evaluated_ref3 = evaluate_forward_ref (typing .ForwardRef ("""Y['Z["StrAlias"]']""" ), locals = {"Y" : Y , "Z" : Z , "StrAlias" : str })
8904
8862
self .assertEqual (get_origin (evaluated_ref3 ), Y )
8905
- if sys .version_info [:2 ] in (( 3 , 8 ), ( 3 , 10 ) ):
8906
- self .skipTest ("Nested string 'StrAlias' is not resolved in 3.8 and 3. 10" )
8863
+ if sys .version_info [:2 ] == ( 3 , 10 ):
8864
+ self .skipTest ("Nested string 'StrAlias' is not resolved in 3.10" )
8907
8865
self .assertEqual (get_args (evaluated_ref3 ), (Z [str ],))
8908
8866
8909
8867
def test_invalid_special_forms (self ):
0 commit comments