@@ -6859,12 +6859,10 @@ def test_forward_ref_and_final(self):
68596859 self .assertEqual (hints , {'value' : Final })
68606860
68616861 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 ]})
68686866
68696867 def test_get_type_hints_typeddict (self ):
68706868 self .assertEqual (get_type_hints (TotalMovie ), {'title' : str , 'year' : int })
@@ -6967,6 +6965,11 @@ def foo(a: 'Callable[..., T]'):
69676965 self .assertEqual (get_type_hints (foo , globals (), locals ()),
69686966 {'a' : Callable [..., T ]})
69696967
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+
69706973 def test_special_forms_forward (self ):
69716974
69726975 class C :
@@ -6982,8 +6985,9 @@ class CF:
69826985 self .assertEqual (get_type_hints (C , globals ())['b' ], Final [int ])
69836986 self .assertEqual (get_type_hints (C , globals ())['x' ], ClassVar )
69846987 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 ],))
69876991
69886992 def test_union_forward_recursion (self ):
69896993 ValueList = List ['Value' ]
@@ -7216,33 +7220,113 @@ class C(Generic[T]): pass
72167220class EvaluateForwardRefTests (BaseTestCase ):
72177221 def test_evaluate_forward_ref (self ):
72187222 int_ref = ForwardRef ('int' )
7219- missing = ForwardRef ( 'missing' )
7223+ self . assertIs ( typing . evaluate_forward_ref ( int_ref ), int )
72207224 self .assertIs (
72217225 typing .evaluate_forward_ref (int_ref , type_params = ()),
72227226 int ,
72237227 )
7228+ self .assertIs (
7229+ typing .evaluate_forward_ref (int_ref , format = annotationlib .Format .VALUE ),
7230+ int ,
7231+ )
72247232 self .assertIs (
72257233 typing .evaluate_forward_ref (
7226- int_ref , type_params = (), format = annotationlib .Format .FORWARDREF ,
7234+ int_ref , format = annotationlib .Format .FORWARDREF ,
72277235 ),
72287236 int ,
72297237 )
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 )
72307249 self .assertIs (
72317250 typing .evaluate_forward_ref (
7232- missing , type_params = (), format = annotationlib .Format .FORWARDREF ,
7251+ missing , format = annotationlib .Format .FORWARDREF ,
72337252 ),
72347253 missing ,
72357254 )
72367255 self .assertEqual (
72377256 typing .evaluate_forward_ref (
7238- int_ref , type_params = () , format = annotationlib .Format .STRING ,
7257+ missing , format = annotationlib .Format .STRING ,
72397258 ),
7240- 'int' ,
7259+ "missing" ,
72417260 )
72427261
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+ )
72467330
72477331
72487332class CollectionsAbcTests (BaseTestCase ):
0 commit comments