]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-129463: Remove two attributes from ForwardRef equality (#132283)
authorJelle Zijlstra <jelle.zijlstra@gmail.com>
Tue, 15 Apr 2025 19:58:21 +0000 (12:58 -0700)
committerGitHub <noreply@github.com>
Tue, 15 Apr 2025 19:58:21 +0000 (12:58 -0700)
Lib/annotationlib.py
Lib/test/test_typing.py
Misc/NEWS.d/next/Library/2025-04-08-10-45-22.gh-issue-129463.b1qEP3.rst [new file with mode: 0644]

index e1c96298426283d404cfc071fa60a37dc6ce5f28..a5defefb10ea3a0e01e0965ceb7db93f723fad97 100644 (file)
@@ -225,8 +225,6 @@ class ForwardRef:
             # 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 @@ class ForwardRef:
             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__,
         ))
index fc893807837eb99263b18865c4ac06ef6fcee633..a252035ed71a031091a51e055e70a0176dbb275b 100644 (file)
@@ -6539,6 +6539,13 @@ class ForwardRefTests(BaseTestCase):
         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 (file)
index 0000000..9c5796f
--- /dev/null
@@ -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.