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
Prev Previous commit
Next Next commit
modify broken test, move test to forwardref format group
  • Loading branch information
DavidCEllis committed Apr 22, 2025
commit 1908a4a6ca0a70e45128237a07f0e307ff283eef
50 changes: 26 additions & 24 deletions Lib/test/test_annotationlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,11 @@ def f(
self.assertEqual(z_anno, support.EqualToForwardRef("some(module)", owner=f))

alpha_anno = anno["alpha"]
self.assertIsInstance(alpha_anno, ForwardRef)
self.assertEqual(alpha_anno, support.EqualToForwardRef("some | obj", owner=f))
self.assertIsInstance(alpha_anno, Union)
self.assertEqual(
typing.get_args(alpha_anno),
(support.EqualToForwardRef("some", owner=f), support.EqualToForwardRef("obj", owner=f))
)

beta_anno = anno["beta"]
self.assertIsInstance(beta_anno, ForwardRef)
Expand All @@ -126,6 +129,27 @@ def f(
self.assertIsInstance(gamma_anno, ForwardRef)
self.assertEqual(gamma_anno, support.EqualToForwardRef("some < obj", owner=f))

def test_partially_nonexistent_union(self):
# Test unions with '|' syntax equal unions with typing.Union[] with some 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)


class TestSourceFormat(unittest.TestCase):
def test_closure(self):
Expand Down Expand Up @@ -936,28 +960,6 @@ 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
Loading