@@ -115,8 +115,11 @@ def f(
115
115
self .assertEqual (z_anno , support .EqualToForwardRef ("some(module)" , owner = f ))
116
116
117
117
alpha_anno = anno ["alpha" ]
118
- self .assertIsInstance (alpha_anno , ForwardRef )
119
- self .assertEqual (alpha_anno , support .EqualToForwardRef ("some | obj" , owner = f ))
118
+ self .assertIsInstance (alpha_anno , Union )
119
+ self .assertEqual (
120
+ typing .get_args (alpha_anno ),
121
+ (support .EqualToForwardRef ("some" , owner = f ), support .EqualToForwardRef ("obj" , owner = f ))
122
+ )
120
123
121
124
beta_anno = anno ["beta" ]
122
125
self .assertIsInstance (beta_anno , ForwardRef )
@@ -126,6 +129,27 @@ def f(
126
129
self .assertIsInstance (gamma_anno , ForwardRef )
127
130
self .assertEqual (gamma_anno , support .EqualToForwardRef ("some < obj" , owner = f ))
128
131
132
+ def test_partially_nonexistent_union (self ):
133
+ # Test unions with '|' syntax equal unions with typing.Union[] with some forwardrefs
134
+ class UnionForwardrefs :
135
+ pipe : str | undefined
136
+ union : Union [str , undefined ]
137
+
138
+ annos = get_annotations (UnionForwardrefs , format = Format .FORWARDREF )
139
+
140
+ match = (
141
+ str ,
142
+ support .EqualToForwardRef ("undefined" , is_class = True , owner = UnionForwardrefs )
143
+ )
144
+
145
+ self .assertEqual (
146
+ typing .get_args (annos ["pipe" ]),
147
+ typing .get_args (annos ["union" ])
148
+ )
149
+
150
+ self .assertEqual (typing .get_args (annos ["pipe" ]), match )
151
+ self .assertEqual (typing .get_args (annos ["union" ]), match )
152
+
129
153
130
154
class TestSourceFormat (unittest .TestCase ):
131
155
def test_closure (self ):
@@ -936,28 +960,6 @@ def __call__(self):
936
960
annotationlib .get_annotations (obj , format = format ), {}
937
961
)
938
962
939
- def test_union_forwardref (self ):
940
- # Test unions with '|' syntax equal unions with typing.Union[] with forwardrefs
941
- class UnionForwardrefs :
942
- pipe : str | undefined
943
- union : Union [str , undefined ]
944
-
945
- annos = get_annotations (UnionForwardrefs , format = Format .FORWARDREF )
946
-
947
- match = (
948
- str ,
949
- support .EqualToForwardRef ("undefined" , is_class = True , owner = UnionForwardrefs )
950
- )
951
-
952
- self .assertEqual (
953
- typing .get_args (annos ["pipe" ]),
954
- typing .get_args (annos ["union" ])
955
- )
956
-
957
- self .assertEqual (typing .get_args (annos ["pipe" ]), match )
958
- self .assertEqual (typing .get_args (annos ["union" ]), match )
959
-
960
-
961
963
def test_pep695_generic_class_with_future_annotations (self ):
962
964
ann_module695 = inspect_stringized_annotations_pep695
963
965
A_annotations = annotationlib .get_annotations (ann_module695 .A , eval_str = True )
0 commit comments