@@ -172,13 +172,6 @@ def assertNotIsSubclass(self, cls, class_or_tuple, msg=None):
172
172
message += f' : { msg } '
173
173
raise self .failureException (message )
174
174
175
- @contextlib .contextmanager
176
- def assertWarnsIf (self , condition : bool , expected_warning : Type [Warning ]):
177
- with contextlib .ExitStack () as stack :
178
- if condition :
179
- stack .enter_context (self .assertWarns (expected_warning ))
180
- yield
181
-
182
175
183
176
class Employee :
184
177
pass
@@ -2467,7 +2460,7 @@ def test_basics_iterable_syntax(self):
2467
2460
self .assertEqual (Emp .__total__ , True )
2468
2461
2469
2462
def test_basics_keywords_syntax (self ):
2470
- with self .assertWarnsIf ( sys . version_info >= ( 3 , 11 ), DeprecationWarning ):
2463
+ with self .assertWarns ( DeprecationWarning ):
2471
2464
Emp = TypedDict ('Emp' , name = str , id = int )
2472
2465
self .assertIsSubclass (Emp , dict )
2473
2466
self .assertIsSubclass (Emp , typing .MutableMapping )
@@ -2483,7 +2476,7 @@ def test_basics_keywords_syntax(self):
2483
2476
self .assertEqual (Emp .__total__ , True )
2484
2477
2485
2478
def test_typeddict_special_keyword_names (self ):
2486
- with self .assertWarnsIf ( sys . version_info >= ( 3 , 11 ), DeprecationWarning ):
2479
+ with self .assertWarns ( DeprecationWarning ):
2487
2480
TD = TypedDict ("TD" , cls = type , self = object , typename = str , _typename = int ,
2488
2481
fields = list , _fields = dict )
2489
2482
self .assertEqual (TD .__name__ , 'TD' )
@@ -2519,7 +2512,7 @@ def test_typeddict_create_errors(self):
2519
2512
2520
2513
def test_typeddict_errors (self ):
2521
2514
Emp = TypedDict ('Emp' , {'name' : str , 'id' : int })
2522
- if hasattr ( typing , "Required" ):
2515
+ if sys . version_info >= ( 3 , 12 ):
2523
2516
self .assertEqual (TypedDict .__module__ , 'typing' )
2524
2517
else :
2525
2518
self .assertEqual (TypedDict .__module__ , 'typing_extensions' )
@@ -2532,7 +2525,7 @@ def test_typeddict_errors(self):
2532
2525
issubclass (dict , Emp )
2533
2526
2534
2527
if not TYPING_3_11_0 :
2535
- with self .assertRaises (TypeError ):
2528
+ with self .assertRaises (TypeError ), self . assertWarns ( DeprecationWarning ) :
2536
2529
TypedDict ('Hi' , x = 1 )
2537
2530
with self .assertRaises (TypeError ):
2538
2531
TypedDict ('Hi' , [('x' , int ), ('y' , 1 )])
@@ -3036,6 +3029,49 @@ def test_get_type_hints_typeddict(self):
3036
3029
'year' : NotRequired [Annotated [int , 2000 ]],
3037
3030
}
3038
3031
3032
+ def test_orig_bases (self ):
3033
+ T = TypeVar ('T' )
3034
+
3035
+ class Parent (TypedDict ):
3036
+ pass
3037
+
3038
+ class Child (Parent ):
3039
+ pass
3040
+
3041
+ class OtherChild (Parent ):
3042
+ pass
3043
+
3044
+ class MixedChild (Child , OtherChild , Parent ):
3045
+ pass
3046
+
3047
+ class GenericParent (TypedDict , Generic [T ]):
3048
+ pass
3049
+
3050
+ class GenericChild (GenericParent [int ]):
3051
+ pass
3052
+
3053
+ class OtherGenericChild (GenericParent [str ]):
3054
+ pass
3055
+
3056
+ class MixedGenericChild (GenericChild , OtherGenericChild , GenericParent [float ]):
3057
+ pass
3058
+
3059
+ class MultipleGenericBases (GenericParent [int ], GenericParent [float ]):
3060
+ pass
3061
+
3062
+ CallTypedDict = TypedDict ('CallTypedDict' , {})
3063
+
3064
+ self .assertEqual (Parent .__orig_bases__ , (TypedDict ,))
3065
+ self .assertEqual (Child .__orig_bases__ , (Parent ,))
3066
+ self .assertEqual (OtherChild .__orig_bases__ , (Parent ,))
3067
+ self .assertEqual (MixedChild .__orig_bases__ , (Child , OtherChild , Parent ,))
3068
+ self .assertEqual (GenericParent .__orig_bases__ , (TypedDict , Generic [T ]))
3069
+ self .assertEqual (GenericChild .__orig_bases__ , (GenericParent [int ],))
3070
+ self .assertEqual (OtherGenericChild .__orig_bases__ , (GenericParent [str ],))
3071
+ self .assertEqual (MixedGenericChild .__orig_bases__ , (GenericChild , OtherGenericChild , GenericParent [float ]))
3072
+ self .assertEqual (MultipleGenericBases .__orig_bases__ , (GenericParent [int ], GenericParent [float ]))
3073
+ self .assertEqual (CallTypedDict .__orig_bases__ , (TypedDict ,))
3074
+
3039
3075
3040
3076
class TypeAliasTests (BaseTestCase ):
3041
3077
def test_canonical_usage_with_variable_annotation (self ):
@@ -3802,22 +3838,23 @@ def test_typing_extensions_defers_when_possible(self):
3802
3838
'overload' ,
3803
3839
'ParamSpec' ,
3804
3840
'Text' ,
3805
- 'TypedDict' ,
3806
3841
'TypeVar' ,
3807
3842
'TypeVarTuple' ,
3808
3843
'TYPE_CHECKING' ,
3809
3844
'Final' ,
3810
3845
'get_type_hints' ,
3811
- 'is_typeddict' ,
3812
3846
}
3813
3847
if sys .version_info < (3 , 10 ):
3814
3848
exclude |= {'get_args' , 'get_origin' }
3815
3849
if sys .version_info < (3 , 10 , 1 ):
3816
3850
exclude |= {"Literal" }
3817
3851
if sys .version_info < (3 , 11 ):
3818
- exclude |= {'final' , 'NamedTuple' , ' Any' }
3852
+ exclude |= {'final' , 'Any' }
3819
3853
if sys .version_info < (3 , 12 ):
3820
- exclude |= {'Protocol' , 'runtime_checkable' , 'SupportsIndex' }
3854
+ exclude |= {
3855
+ 'Protocol' , 'runtime_checkable' , 'SupportsIndex' , 'TypedDict' ,
3856
+ 'is_typeddict' , 'NamedTuple' ,
3857
+ }
3821
3858
for item in typing_extensions .__all__ :
3822
3859
if item not in exclude and hasattr (typing , item ):
3823
3860
self .assertIs (
@@ -3863,7 +3900,6 @@ def __add__(self, other):
3863
3900
return 0
3864
3901
3865
3902
3866
- @skipIf (TYPING_3_11_0 , "These invariants should all be tested upstream on 3.11+" )
3867
3903
class NamedTupleTests (BaseTestCase ):
3868
3904
class NestedEmployee (NamedTuple ):
3869
3905
name : str
@@ -4003,7 +4039,9 @@ class Y(Generic[T], NamedTuple):
4003
4039
self .assertIs (type (a ), G )
4004
4040
self .assertEqual (a .x , 3 )
4005
4041
4006
- with self .assertRaisesRegex (TypeError , 'Too many parameters' ):
4042
+ things = "arguments" if sys .version_info >= (3 , 11 ) else "parameters"
4043
+
4044
+ with self .assertRaisesRegex (TypeError , f'Too many { things } ' ):
4007
4045
G [int , str ]
4008
4046
4009
4047
@skipUnless (TYPING_3_9_0 , "tuple.__class_getitem__ was added in 3.9" )
@@ -4134,6 +4172,22 @@ def test_same_as_typing_NamedTuple_38_minus(self):
4134
4172
self .NestedEmployee ._field_types
4135
4173
)
4136
4174
4175
+ def test_orig_bases (self ):
4176
+ T = TypeVar ('T' )
4177
+
4178
+ class SimpleNamedTuple (NamedTuple ):
4179
+ pass
4180
+
4181
+ class GenericNamedTuple (NamedTuple , Generic [T ]):
4182
+ pass
4183
+
4184
+ self .assertEqual (SimpleNamedTuple .__orig_bases__ , (NamedTuple ,))
4185
+ self .assertEqual (GenericNamedTuple .__orig_bases__ , (NamedTuple , Generic [T ]))
4186
+
4187
+ CallNamedTuple = NamedTuple ('CallNamedTuple' , [])
4188
+
4189
+ self .assertEqual (CallNamedTuple .__orig_bases__ , (NamedTuple ,))
4190
+
4137
4191
4138
4192
class TypeVarLikeDefaultsTests (BaseTestCase ):
4139
4193
def test_typevar (self ):
0 commit comments