Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add a test for union forward references
  • Loading branch information
DavidCEllis committed Apr 22, 2025
commit fba5efa2a04871b845403443f85b18a20d303e3a
22 changes: 22 additions & 0 deletions Lib/test/test_annotationlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,28 @@ def __call__(self):
annotationlib.get_annotations(obj, format=format), {}
)

def test_union_forwardref(self):
# Test unions with '|' syntax equal unions with typing.Union[] with forwardrefs
class UnionForwardrefs:
pipe: str | undefined
union: Union[str, undefined]

annos = get_annotations(UnionForwardrefs, format=Format.FORWARDREF)

match = (
str,
support.EqualToForwardRef("undefined", is_class=True, owner=UnionForwardrefs)
)

self.assertEqual(
typing.get_args(annos["pipe"]),
typing.get_args(annos["union"])
)

self.assertEqual(typing.get_args(annos["pipe"]), match)
self.assertEqual(typing.get_args(annos["union"]), match)


def test_pep695_generic_class_with_future_annotations(self):
ann_module695 = inspect_stringized_annotations_pep695
A_annotations = annotationlib.get_annotations(ann_module695.A, eval_str=True)
Expand Down