From d59658d475e13cf1d379dbd8bb57f1406fc67889 Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Wed, 26 Mar 2025 11:10:46 +0100 Subject: [PATCH 1/3] gh-119180: Use equality when comparing against `annotationlib.Format` --- Lib/test/test_annotationlib.py | 4 ++-- Lib/typing.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_annotationlib.py b/Lib/test/test_annotationlib.py index 20f74b4ed0aadb..495606b48ed2e8 100644 --- a/Lib/test/test_annotationlib.py +++ b/Lib/test/test_annotationlib.py @@ -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( @@ -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), diff --git a/Lib/typing.py b/Lib/typing.py index 96211553a21e39..e36da7e9f48b71 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -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: hints.update(ann) continue if globalns is None: @@ -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()} @@ -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: From 0ec4e4e36fa3ee04d6435f0b6f429af6c2456772 Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Thu, 27 Mar 2025 09:59:15 +0100 Subject: [PATCH 2/3] Add test --- Lib/test/test_typing.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 402353404cb0fb..5b148ce4f21c3a 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -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=3), {'x': 'undefined'}) def test_get_type_hints_format_function(self): def func(x: undefined) -> undefined: ... From c6a1a3141bde91d26af4f2cbc8bbb391d52161b6 Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Thu, 27 Mar 2025 21:25:29 +0100 Subject: [PATCH 3/3] Fix test --- Lib/test/test_typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 5b148ce4f21c3a..2c0297313cb4ab 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -7159,7 +7159,7 @@ 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=3), {'x': 'undefined'}) + self.assertEqual(get_type_hints(C, format=4), {'x': 'undefined'}) def test_get_type_hints_format_function(self): def func(x: undefined) -> undefined: ...