File tree Expand file tree Collapse file tree 1 file changed +19
-0
lines changed
Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -1961,6 +1961,11 @@ def test_forward_repr(self):
19611961 "typing.List[ForwardRef('int', owner='class')]" ,
19621962 )
19631963
1964+ def test_forward_repr_extra_names (self ):
1965+ fr = ForwardRef ("__annotationlib_name_1__" )
1966+ fr .__extra_names__ = {"__annotationlib_name_1__" : list [str ]}
1967+ self .assertEqual (repr (fr ), "ForwardRef('list[str]')" )
1968+
19641969 def test_forward_recursion_actually (self ):
19651970 def namespace1 ():
19661971 a = ForwardRef ("A" )
@@ -2037,6 +2042,20 @@ def test_evaluate_string_format(self):
20372042 fr = ForwardRef ("set[Any]" )
20382043 self .assertEqual (fr .evaluate (format = Format .STRING ), "set[Any]" )
20392044
2045+ def test_evaluate_string_format_extra_names (self ):
2046+ # Test that internal extra_names are replaced when evaluating as strings
2047+
2048+ # As identifier
2049+ fr = ForwardRef ("__annotationlib_name_1__" )
2050+ fr .__extra_names__ = {"__annotationlib_name_1__" : str }
2051+ self .assertEqual (fr .evaluate (format = Format .STRING ), "str" )
2052+
2053+ # Via AST visitor
2054+ def f (a : ref | str ): ...
2055+
2056+ fr = get_annotations (f , format = Format .FORWARDREF )['a' ]
2057+ self .assertEqual (fr .evaluate (format = Format .STRING ), "ref | str" )
2058+
20402059 def test_evaluate_forwardref_format (self ):
20412060 fr = ForwardRef ("undef" )
20422061 evaluated = fr .evaluate (format = Format .FORWARDREF )
You can’t perform that action at this time.
0 commit comments