summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/test/test_typing.py12
-rw-r--r--Lib/typing.py5
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS.d/next/Library/2022-01-11-15-54-15.bpo-46333.B1faiF.rst4
4 files changed, 20 insertions, 2 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 9548a0c..1b3eb06 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -2800,6 +2800,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')
@@ -2836,6 +2840,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
diff --git a/Lib/typing.py b/Lib/typing.py
index 3233827..8f923fa 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -767,10 +767,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 __or__(self, other):
return Union[self, other]
diff --git a/Misc/ACKS b/Misc/ACKS
index 244d757..dceb2b6 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -680,6 +680,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 0000000..ec3c6d5
--- /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.