From 2d11397aac49e0c413f18e9d55bbfce40532f85d Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 8 Apr 2025 10:45:27 -0700 Subject: [PATCH] gh-129463: Remove two attributes from ForwardRef equality --- Lib/annotationlib.py | 4 ---- Lib/test/test_typing.py | 7 +++++++ .../Library/2025-04-08-10-45-22.gh-issue-129463.b1qEP3.rst | 3 +++ 3 files changed, 10 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-04-08-10-45-22.gh-issue-129463.b1qEP3.rst diff --git a/Lib/annotationlib.py b/Lib/annotationlib.py index d6243c8863610e..63d40cb47342af 100644 --- a/Lib/annotationlib.py +++ b/Lib/annotationlib.py @@ -225,8 +225,6 @@ def __eq__(self, other): # because dictionaries are not hashable. and self.__globals__ is other.__globals__ and self.__forward_is_class__ == other.__forward_is_class__ - and self.__code__ == other.__code__ - and self.__ast_node__ == other.__ast_node__ and self.__cell__ == other.__cell__ and self.__owner__ == other.__owner__ ) @@ -237,8 +235,6 @@ def __hash__(self): self.__forward_module__, id(self.__globals__), # dictionaries are not hashable, so hash by identity self.__forward_is_class__, - self.__code__, - self.__ast_node__, self.__cell__, self.__owner__, )) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index edf3cf9d4a3658..face065a040a99 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -6529,6 +6529,13 @@ def test_or(self): self.assertEqual(X | "x", Union[X, "x"]) self.assertEqual("x" | X, Union["x", X]) + def test_multiple_ways_to_create(self): + X1 = Union["X"] + self.assertIsInstance(X1, ForwardRef) + X2 = ForwardRef("X") + self.assertIsInstance(X2, ForwardRef) + self.assertEqual(X1, X2) + class InternalsTests(BaseTestCase): def test_deprecation_for_no_type_params_passed_to__evaluate(self): diff --git a/Misc/NEWS.d/next/Library/2025-04-08-10-45-22.gh-issue-129463.b1qEP3.rst b/Misc/NEWS.d/next/Library/2025-04-08-10-45-22.gh-issue-129463.b1qEP3.rst new file mode 100644 index 00000000000000..9c5796f11e2078 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-04-08-10-45-22.gh-issue-129463.b1qEP3.rst @@ -0,0 +1,3 @@ +Comparison of :class:`annotationlib.ForwardRef` objects no longer uses the +internal ``__code__`` and ``__ast_node__`` attributes, which are used as +caches.