42
42
43
43
# Flags used to mark tests that only apply after a specific
44
44
# version of the typing module.
45
- TYPING_3_8_0 = sys .version_info [:3 ] >= (3 , 8 , 0 )
46
45
TYPING_3_9_0 = sys .version_info [:3 ] >= (3 , 9 , 0 )
47
46
TYPING_3_10_0 = sys .version_info [:3 ] >= (3 , 10 , 0 )
48
47
52
51
# 3.12 changes the representation of Unpack[] (PEP 692)
53
52
TYPING_3_12_0 = sys .version_info [:3 ] >= (3 , 12 , 0 )
54
53
55
- only_with_typing_Protocol = skipUnless (
56
- hasattr (typing , "Protocol" ), "Only relevant when typing.Protocol exists"
57
- )
58
-
59
54
# https://github.com/python/cpython/pull/27017 was backported into some 3.9 and 3.10
60
55
# versions, but not all
61
56
HAS_FORWARD_MODULE = "module" in inspect .signature (typing ._type_check ).parameters
@@ -246,13 +241,7 @@ def some(arg: NoReturn) -> NoReturn: ...
246
241
def some_str (arg : 'NoReturn' ) -> 'typing.NoReturn' : ...
247
242
248
243
expected = {'arg' : NoReturn , 'return' : NoReturn }
249
- targets = [some ]
250
-
251
- # On 3.7.0 and 3.7.1, https://github.com/python/cpython/pull/10772
252
- # wasn't applied yet and NoReturn fails _type_check.
253
- if not ((3 , 7 , 0 ) <= sys .version_info < (3 , 7 , 2 )):
254
- targets .append (some_str )
255
- for target in targets :
244
+ for target in some , some_str :
256
245
with self .subTest (target = target ):
257
246
self .assertEqual (gth (target ), expected )
258
247
@@ -595,15 +584,11 @@ def test_basics(self):
595
584
Final [int ][str ]
596
585
597
586
def test_repr (self ):
598
- if hasattr (typing , 'Final' ) and sys .version_info [:2 ] >= (3 , 7 ):
599
- mod_name = 'typing'
600
- else :
601
- mod_name = 'typing_extensions'
602
- self .assertEqual (repr (Final ), mod_name + '.Final' )
587
+ self .assertEqual (repr (Final ), 'typing.Final' )
603
588
cv = Final [int ]
604
- self .assertEqual (repr (cv ), mod_name + ' .Final[int]' )
589
+ self .assertEqual (repr (cv ), 'typing .Final[int]' )
605
590
cv = Final [Employee ]
606
- self .assertEqual (repr (cv ), mod_name + f' .Final[{ __name__ } .Employee]' )
591
+ self .assertEqual (repr (cv ), f'typing .Final[{ __name__ } .Employee]' )
607
592
608
593
def test_cannot_subclass (self ):
609
594
with self .assertRaises (TypeError ):
@@ -1771,7 +1756,6 @@ class E(C, BP): pass
1771
1756
self .assertNotIsInstance (D (), E )
1772
1757
self .assertNotIsInstance (E (), D )
1773
1758
1774
- @only_with_typing_Protocol
1775
1759
def test_runtimecheckable_on_typing_dot_Protocol (self ):
1776
1760
@runtime_checkable
1777
1761
class Foo (typing .Protocol ):
@@ -1784,7 +1768,6 @@ def __init__(self):
1784
1768
self .assertIsInstance (Bar (), Foo )
1785
1769
self .assertNotIsInstance (object (), Foo )
1786
1770
1787
- @only_with_typing_Protocol
1788
1771
def test_typing_dot_runtimecheckable_on_Protocol (self ):
1789
1772
@typing .runtime_checkable
1790
1773
class Foo (Protocol ):
@@ -1797,7 +1780,6 @@ def __init__(self):
1797
1780
self .assertIsInstance (Bar (), Foo )
1798
1781
self .assertNotIsInstance (object (), Foo )
1799
1782
1800
- @only_with_typing_Protocol
1801
1783
def test_typing_Protocol_and_extensions_Protocol_can_mix (self ):
1802
1784
class TypingProto (typing .Protocol ):
1803
1785
x : int
@@ -3173,7 +3155,6 @@ def c(self) -> int: return 5
3173
3155
with self .assertRaisesRegex (TypeError , "not a Protocol" ):
3174
3156
get_protocol_members (ConcreteInherit ())
3175
3157
3176
- @only_with_typing_Protocol
3177
3158
def test_get_protocol_members_typing (self ):
3178
3159
with self .assertRaisesRegex (TypeError , "not a Protocol" ):
3179
3160
get_protocol_members (typing .Protocol )
@@ -3222,7 +3203,6 @@ def test_is_protocol(self):
3222
3203
# Protocol is not itself a protocol
3223
3204
self .assertFalse (is_protocol (Protocol ))
3224
3205
3225
- @only_with_typing_Protocol
3226
3206
def test_is_protocol_with_typing (self ):
3227
3207
self .assertFalse (is_protocol (typing .Protocol ))
3228
3208
@@ -3681,7 +3661,6 @@ class NewGeneric[T](TypedDict):
3681
3661
if hasattr (typing , "TypedDict" ):
3682
3662
self .assertIs (is_typeddict (typing .TypedDict ), False )
3683
3663
3684
- @skipUnless (TYPING_3_8_0 , "Python 3.8+ required" )
3685
3664
def test_is_typeddict_against_typeddict_from_typing (self ):
3686
3665
Point = typing .TypedDict ('Point' , {'x' : int , 'y' : int })
3687
3666
@@ -3844,7 +3823,7 @@ class WithImplicitAny(B):
3844
3823
def test_non_generic_subscript (self ):
3845
3824
# For backward compatibility, subscription works
3846
3825
# on arbitrary TypedDict types.
3847
- # (But we don't attempt to backport this misfeature onto 3.7 and 3. 8.)
3826
+ # (But we don't attempt to backport this misfeature onto 3.8.)
3848
3827
class TD (TypedDict ):
3849
3828
a : T
3850
3829
A = TD [int ]
@@ -4034,17 +4013,8 @@ class C:
4034
4013
classvar : Annotated [ClassVar [int ], "a decoration" ] = 4
4035
4014
const : Annotated [Final [int ], "Const" ] = 4
4036
4015
4037
- if sys .version_info [:2 ] >= (3 , 7 ):
4038
- self .assertEqual (get_type_hints (C , globals ())["classvar" ], ClassVar [int ])
4039
- self .assertEqual (get_type_hints (C , globals ())["const" ], Final [int ])
4040
- else :
4041
- self .assertEqual (
4042
- get_type_hints (C , globals ())["classvar" ],
4043
- Annotated [ClassVar [int ], "a decoration" ]
4044
- )
4045
- self .assertEqual (
4046
- get_type_hints (C , globals ())["const" ], Annotated [Final [int ], "Const" ]
4047
- )
4016
+ self .assertEqual (get_type_hints (C , globals ())["classvar" ], ClassVar [int ])
4017
+ self .assertEqual (get_type_hints (C , globals ())["const" ], Final [int ])
4048
4018
4049
4019
def test_cannot_subclass (self ):
4050
4020
with self .assertRaisesRegex (TypeError , "Cannot subclass .*Annotated" ):
@@ -5069,11 +5039,8 @@ def test_typing_extensions_defers_when_possible(self):
5069
5039
'dataclass_transform' ,
5070
5040
'overload' ,
5071
5041
'ParamSpec' ,
5072
- 'Text' ,
5073
5042
'TypeVar' ,
5074
5043
'TypeVarTuple' ,
5075
- 'TYPE_CHECKING' ,
5076
- 'Final' ,
5077
5044
'get_type_hints' ,
5078
5045
}
5079
5046
if sys .version_info < (3 , 10 ):
@@ -5189,13 +5156,6 @@ class NonDefaultAfterDefault(NamedTuple):
5189
5156
x : int = 3
5190
5157
y : int
5191
5158
5192
- @skipUnless (
5193
- (
5194
- TYPING_3_8_0
5195
- or hasattr (CoolEmployeeWithDefault , '_field_defaults' )
5196
- ),
5197
- '"_field_defaults" attribute was added in a micro version of 3.7'
5198
- )
5199
5159
def test_field_defaults (self ):
5200
5160
self .assertEqual (CoolEmployeeWithDefault ._field_defaults , dict (cool = 0 ))
5201
5161
@@ -5296,7 +5256,7 @@ class Group(NamedTuple):
5296
5256
self .assertEqual (a , (1 , [2 ]))
5297
5257
5298
5258
@skipIf (TYPING_3_9_0 , "Test isn't relevant to 3.9+" )
5299
- def test_non_generic_subscript_error_message_py38_minus (self ):
5259
+ def test_non_generic_subscript_error_message_py38 (self ):
5300
5260
class Group (NamedTuple ):
5301
5261
key : T
5302
5262
group : List [T ]
@@ -5389,10 +5349,7 @@ class CNT(NamedTuple):
5389
5349
self .assertEqual (struct ._fields , ())
5390
5350
self .assertEqual (struct .__annotations__ , {})
5391
5351
self .assertIsInstance (struct (), struct )
5392
- # Attribute was added in a micro version of 3.7
5393
- # and is tested more fully elsewhere
5394
- if hasattr (struct , "_field_defaults" ):
5395
- self .assertEqual (struct ._field_defaults , {})
5352
+ self .assertEqual (struct ._field_defaults , {})
5396
5353
5397
5354
def test_namedtuple_errors (self ):
5398
5355
with self .assertRaises (TypeError ):
@@ -5429,15 +5386,6 @@ def test_copy_and_pickle(self):
5429
5386
def test_docstring (self ):
5430
5387
self .assertIsInstance (NamedTuple .__doc__ , str )
5431
5388
5432
- @skipUnless (TYPING_3_8_0 , "NamedTuple had a bad signature on <=3.7" )
5433
- def test_signature_is_same_as_typing_NamedTuple (self ):
5434
- self .assertEqual (inspect .signature (NamedTuple ), inspect .signature (typing .NamedTuple ))
5435
-
5436
- @skipIf (TYPING_3_8_0 , "tests are only relevant to <=3.7" )
5437
- def test_signature_on_37 (self ):
5438
- self .assertIsInstance (inspect .signature (NamedTuple ), inspect .Signature )
5439
- self .assertFalse (hasattr (NamedTuple , "__text_signature__" ))
5440
-
5441
5389
@skipUnless (TYPING_3_9_0 , "NamedTuple was a class on 3.8 and lower" )
5442
5390
def test_same_as_typing_NamedTuple_39_plus (self ):
5443
5391
self .assertEqual (
@@ -5592,7 +5540,7 @@ def test_bound_errors(self):
5592
5540
r"Bound must be a type\. Got \(1, 2\)\." ):
5593
5541
TypeVar ('X' , bound = (1 , 2 ))
5594
5542
5595
- # Technically we could run it on later versions of 3.7 and 3. 8,
5543
+ # Technically we could run it on later versions of 3.8,
5596
5544
# but that's not worth the effort.
5597
5545
@skipUnless (TYPING_3_9_0 , "Fix was not backported" )
5598
5546
def test_missing__name__ (self ):
0 commit comments