]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-46333: Honor `module` parameter in ForwardRef (GH-30536)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 17 Feb 2022 03:53:31 +0000 (19:53 -0800)
committerGitHub <noreply@github.com>
Thu, 17 Feb 2022 03:53:31 +0000 (19:53 -0800)
The `module` parameter carries semantic information about the forward ref.
Forward refs are different if they refer to different module even if they
have the same name. This affects the `__eq__`, `__repr__` and `__hash__` methods.

Co-authored-by: Andreas Hangauer <andreas.hangauer@siemens.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
(cherry picked from commit 6e7b813195f9bd6a2a15c1f00ef2c0180f6c751a)

Co-authored-by: aha79 <34090357+aha79@users.noreply.github.com>
Lib/test/test_typing.py
Lib/typing.py
Misc/ACKS
Misc/NEWS.d/next/Library/2022-01-11-15-54-15.bpo-46333.B1faiF.rst [new file with mode: 0644]

index 641527329501fa5e4c2a74716358e22b18e350ad..08eddb095d561438233f64a1bccdb45492d6f99c 100644 (file)
@@ -2463,6 +2463,10 @@ class ForwardRefTests(BaseTestCase):
         fr = typing.ForwardRef('int')
         self.assertEqual(fr, typing.ForwardRef('int'))
         self.assertNotEqual(List['int'], List[int])
+        self.assertNotEqual(fr, typing.ForwardRef('int', module=__name__))
+        frm = typing.ForwardRef('int', module=__name__)
+        self.assertEqual(frm, typing.ForwardRef('int', module=__name__))
+        self.assertNotEqual(frm, typing.ForwardRef('int', module='__other_name__'))
 
     def test_forward_equality_gth(self):
         c1 = typing.ForwardRef('C')
@@ -2499,6 +2503,14 @@ class ForwardRefTests(BaseTestCase):
         self.assertEqual(hash(c1_gth), hash(c2_gth))
         self.assertEqual(hash(c1), hash(c1_gth))
 
+        c3 = typing.ForwardRef('int', module=__name__)
+        c4 = typing.ForwardRef('int', module='__other_name__')
+
+        self.assertNotEqual(hash(c3), hash(c1))
+        self.assertNotEqual(hash(c3), hash(c1_gth))
+        self.assertNotEqual(hash(c3), hash(c4))
+        self.assertEqual(hash(c3), hash(typing.ForwardRef('int', module=__name__)))
+
     def test_forward_equality_namespace(self):
         class A:
             pass
index 18b21e96435b32d024548253ff1743f4172e318e..af2d8c758a4545bae7f8e8d7fadaa26e71cb3564 100644 (file)
@@ -568,10 +568,11 @@ class ForwardRef(_Final, _root=True):
         if self.__forward_evaluated__ and other.__forward_evaluated__:
             return (self.__forward_arg__ == other.__forward_arg__ and
                     self.__forward_value__ == other.__forward_value__)
-        return self.__forward_arg__ == other.__forward_arg__
+        return (self.__forward_arg__ == other.__forward_arg__ and
+                self.__forward_module__ == other.__forward_module__)
 
     def __hash__(self):
-        return hash(self.__forward_arg__)
+        return hash((self.__forward_arg__, self.__forward_module__))
 
     def __repr__(self):
         return f'ForwardRef({self.__forward_arg__!r})'
index 61267d2a23bead384f20eada1d7cc0a4a7f5c1ee..fe7e692e04967df3843075363be7aab1cd48d604 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -665,6 +665,7 @@ Anders Hammarquist
 Mark Hammond
 Harald Hanche-Olsen
 Manus Hand
+Andreas Hangauer
 Milton L. Hankins
 Carl Bordum Hansen
 Stephen Hansen
diff --git a/Misc/NEWS.d/next/Library/2022-01-11-15-54-15.bpo-46333.B1faiF.rst b/Misc/NEWS.d/next/Library/2022-01-11-15-54-15.bpo-46333.B1faiF.rst
new file mode 100644 (file)
index 0000000..ec3c6d5
--- /dev/null
@@ -0,0 +1,4 @@
+The :meth:`__eq__` and :meth:`__hash__` methods of
+:class:`typing.ForwardRef` now honor the ``module`` parameter of
+:class:`typing.ForwardRef`. Forward references from different
+modules are now differentiated.