@@ -6859,12 +6859,10 @@ def test_forward_ref_and_final(self):
6859
6859
self .assertEqual (hints , {'value' : Final })
6860
6860
6861
6861
def test_top_level_class_var (self ):
6862
- # https://bugs.python.org/issue45166
6863
- with self .assertRaisesRegex (
6864
- TypeError ,
6865
- r'typing.ClassVar\[int\] is not valid as type argument' ,
6866
- ):
6867
- get_type_hints (ann_module6 )
6862
+ # This is not meaningful but we don't raise for it.
6863
+ # https://github.com/python/cpython/issues/133959
6864
+ hints = get_type_hints (ann_module6 )
6865
+ self .assertEqual (hints , {'wrong' : ClassVar [int ]})
6868
6866
6869
6867
def test_get_type_hints_typeddict (self ):
6870
6868
self .assertEqual (get_type_hints (TotalMovie ), {'title' : str , 'year' : int })
@@ -6967,6 +6965,11 @@ def foo(a: 'Callable[..., T]'):
6967
6965
self .assertEqual (get_type_hints (foo , globals (), locals ()),
6968
6966
{'a' : Callable [..., T ]})
6969
6967
6968
+ def test_special_forms_no_forward (self ):
6969
+ def f (x : ClassVar [int ]):
6970
+ pass
6971
+ self .assertEqual (get_type_hints (f ), {'x' : ClassVar [int ]})
6972
+
6970
6973
def test_special_forms_forward (self ):
6971
6974
6972
6975
class C :
@@ -6982,8 +6985,9 @@ class CF:
6982
6985
self .assertEqual (get_type_hints (C , globals ())['b' ], Final [int ])
6983
6986
self .assertEqual (get_type_hints (C , globals ())['x' ], ClassVar )
6984
6987
self .assertEqual (get_type_hints (C , globals ())['y' ], Final )
6985
- with self .assertRaises (TypeError ):
6986
- get_type_hints (CF , globals ()),
6988
+ lfi = get_type_hints (CF , globals ())['b' ]
6989
+ self .assertIs (get_origin (lfi ), list )
6990
+ self .assertEqual (get_args (lfi ), (Final [int ],))
6987
6991
6988
6992
def test_union_forward_recursion (self ):
6989
6993
ValueList = List ['Value' ]
@@ -7216,33 +7220,113 @@ class C(Generic[T]): pass
7216
7220
class EvaluateForwardRefTests (BaseTestCase ):
7217
7221
def test_evaluate_forward_ref (self ):
7218
7222
int_ref = ForwardRef ('int' )
7219
- missing = ForwardRef ( 'missing' )
7223
+ self . assertIs ( typing . evaluate_forward_ref ( int_ref ), int )
7220
7224
self .assertIs (
7221
7225
typing .evaluate_forward_ref (int_ref , type_params = ()),
7222
7226
int ,
7223
7227
)
7228
+ self .assertIs (
7229
+ typing .evaluate_forward_ref (int_ref , format = annotationlib .Format .VALUE ),
7230
+ int ,
7231
+ )
7224
7232
self .assertIs (
7225
7233
typing .evaluate_forward_ref (
7226
- int_ref , type_params = (), format = annotationlib .Format .FORWARDREF ,
7234
+ int_ref , format = annotationlib .Format .FORWARDREF ,
7227
7235
),
7228
7236
int ,
7229
7237
)
7238
+ self .assertEqual (
7239
+ typing .evaluate_forward_ref (
7240
+ int_ref , format = annotationlib .Format .STRING ,
7241
+ ),
7242
+ 'int' ,
7243
+ )
7244
+
7245
+ def test_evaluate_forward_ref_undefined (self ):
7246
+ missing = ForwardRef ('missing' )
7247
+ with self .assertRaises (NameError ):
7248
+ typing .evaluate_forward_ref (missing )
7230
7249
self .assertIs (
7231
7250
typing .evaluate_forward_ref (
7232
- missing , type_params = (), format = annotationlib .Format .FORWARDREF ,
7251
+ missing , format = annotationlib .Format .FORWARDREF ,
7233
7252
),
7234
7253
missing ,
7235
7254
)
7236
7255
self .assertEqual (
7237
7256
typing .evaluate_forward_ref (
7238
- int_ref , type_params = () , format = annotationlib .Format .STRING ,
7257
+ missing , format = annotationlib .Format .STRING ,
7239
7258
),
7240
- 'int' ,
7259
+ "missing" ,
7241
7260
)
7242
7261
7243
- def test_evaluate_forward_ref_no_type_params (self ):
7244
- ref = ForwardRef ('int' )
7245
- self .assertIs (typing .evaluate_forward_ref (ref ), int )
7262
+ def test_evaluate_forward_ref_nested (self ):
7263
+ ref = ForwardRef ("int | list['str']" )
7264
+ self .assertEqual (
7265
+ typing .evaluate_forward_ref (ref ),
7266
+ int | list [str ],
7267
+ )
7268
+ self .assertEqual (
7269
+ typing .evaluate_forward_ref (ref , format = annotationlib .Format .FORWARDREF ),
7270
+ int | list [str ],
7271
+ )
7272
+ self .assertEqual (
7273
+ typing .evaluate_forward_ref (ref , format = annotationlib .Format .STRING ),
7274
+ "int | list['str']" ,
7275
+ )
7276
+
7277
+ why = ForwardRef ('"\' str\' "' )
7278
+ self .assertIs (typing .evaluate_forward_ref (why ), str )
7279
+
7280
+ def test_evaluate_forward_ref_none (self ):
7281
+ none_ref = ForwardRef ('None' )
7282
+ self .assertIs (typing .evaluate_forward_ref (none_ref ), None )
7283
+
7284
+ def test_globals (self ):
7285
+ A = "str"
7286
+ ref = ForwardRef ('list[A]' )
7287
+ with self .assertRaises (NameError ):
7288
+ typing .evaluate_forward_ref (ref )
7289
+ self .assertEqual (
7290
+ typing .evaluate_forward_ref (ref , globals = {'A' : A }),
7291
+ list [str ],
7292
+ )
7293
+
7294
+ def test_owner (self ):
7295
+ ref = ForwardRef ("A" )
7296
+
7297
+ with self .assertRaises (NameError ):
7298
+ typing .evaluate_forward_ref (ref )
7299
+
7300
+ # We default to the globals of `owner`,
7301
+ # so it no longer raises `NameError`
7302
+ self .assertIs (
7303
+ typing .evaluate_forward_ref (ref , owner = Loop ), A
7304
+ )
7305
+
7306
+ def test_inherited_owner (self ):
7307
+ # owner passed to evaluate_forward_ref
7308
+ ref = ForwardRef ("list['A']" )
7309
+ self .assertEqual (
7310
+ typing .evaluate_forward_ref (ref , owner = Loop ),
7311
+ list [A ],
7312
+ )
7313
+
7314
+ # owner set on the ForwardRef
7315
+ ref = ForwardRef ("list['A']" , owner = Loop )
7316
+ self .assertEqual (
7317
+ typing .evaluate_forward_ref (ref ),
7318
+ list [A ],
7319
+ )
7320
+
7321
+ def test_partial_evaluation (self ):
7322
+ ref = ForwardRef ("list[A]" )
7323
+ with self .assertRaises (NameError ):
7324
+ typing .evaluate_forward_ref (ref )
7325
+
7326
+ self .assertEqual (
7327
+ typing .evaluate_forward_ref (ref , format = annotationlib .Format .FORWARDREF ),
7328
+ list [EqualToForwardRef ('A' )],
7329
+ )
7246
7330
7247
7331
7248
7332
class CollectionsAbcTests (BaseTestCase ):
0 commit comments