Skip to content

gh-119180: Use equality when comparing against annotationlib.Format #131755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions Lib/test/test_annotationlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ def foo(a: int, b: str):

foo.__annotations__ = {"a": "foo", "b": "str"}
for format in Format:
if format is Format.VALUE_WITH_FAKE_GLOBALS:
if format == Format.VALUE_WITH_FAKE_GLOBALS:
continue
with self.subTest(format=format):
self.assertEqual(
Expand Down Expand Up @@ -816,7 +816,7 @@ def __annotations__(self):

wa = WeirdAnnotations()
for format in Format:
if format is Format.VALUE_WITH_FAKE_GLOBALS:
if format == Format.VALUE_WITH_FAKE_GLOBALS:
continue
with (
self.subTest(format=format),
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7158,6 +7158,8 @@ class C:

self.assertEqual(get_type_hints(C, format=annotationlib.Format.STRING),
{'x': 'undefined'})
# Make sure using an int as format also works:
self.assertEqual(get_type_hints(C, format=4), {'x': 'undefined'})

def test_get_type_hints_format_function(self):
def func(x: undefined) -> undefined: ...
Expand Down
6 changes: 3 additions & 3 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2315,7 +2315,7 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False,
hints = {}
for base in reversed(obj.__mro__):
ann = annotationlib.get_annotations(base, format=format)
if format is annotationlib.Format.STRING:
if format == annotationlib.Format.STRING:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add at least one test case affected by this? Would require passing Format.STRING.value to get_type_hints().

hints.update(ann)
continue
if globalns is None:
Expand All @@ -2339,7 +2339,7 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False,
value = _eval_type(value, base_globals, base_locals, base.__type_params__,
format=format, owner=obj)
hints[name] = value
if include_extras or format is annotationlib.Format.STRING:
if include_extras or format == annotationlib.Format.STRING:
return hints
else:
return {k: _strip_annotations(t) for k, t in hints.items()}
Expand All @@ -2353,7 +2353,7 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False,
and not hasattr(obj, '__annotate__')
):
raise TypeError(f"{obj!r} is not a module, class, or callable.")
if format is annotationlib.Format.STRING:
if format == annotationlib.Format.STRING:
return hints

if globalns is None:
Expand Down
Loading