[Python-checkins] [3.10] bpo-46333: Honor `module` parameter in ForwardRef (GH-30536) (GH-31379)
miss-islington
webhook-mailer at python.org
Wed Feb 16 22:53:12 EST 2022
https://github.com/python/cpython/commit/a17d59a6df146077b5420b36c35a1b82ab3f071a
commit: a17d59a6df146077b5420b36c35a1b82ab3f071a
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-02-16T19:53:03-08:00
summary:
[3.10] bpo-46333: Honor `module` parameter in ForwardRef (GH-30536) (GH-31379)
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 at siemens.com>
Co-authored-by: Alex Waygood <Alex.Waygood at Gmail.com>
Co-authored-by: Ken Jin <28750310+Fidget-Spinner at users.noreply.github.com>
(cherry picked from commit 6e7b813195f9bd6a2a15c1f00ef2c0180f6c751a)
Co-authored-by: aha79 <34090357+aha79 at users.noreply.github.com>
Automerge-Triggered-By: GH:JelleZijlstra
files:
A Misc/NEWS.d/next/Library/2022-01-11-15-54-15.bpo-46333.B1faiF.rst
M Lib/test/test_typing.py
M Lib/typing.py
M Misc/ACKS
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 2842e28c5ab59..c4c06008d91fa 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -2658,6 +2658,10 @@ def test_forward_equality(self):
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')
@@ -2694,6 +2698,14 @@ def foo(a: c1_gth, b: c2_gth):
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
diff --git a/Lib/typing.py b/Lib/typing.py
index 135ca582177b9..5d900dd6c33ab 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -705,10 +705,11 @@ def __eq__(self, other):
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})'
diff --git a/Misc/ACKS b/Misc/ACKS
index 7f9166cd74cfa..4bd05b59a9982 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -677,6 +677,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
index 0000000000000..ec3c6d54ee499
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-01-11-15-54-15.bpo-46333.B1faiF.rst
@@ -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.
More information about the Python-checkins
mailing list