From 980ea280e58aa09e5a9ddb40489294f31700ed57 Mon Sep 17 00:00:00 2001 From: Andreas Hangauer Date: Tue, 11 Jan 2022 14:20:12 +0100 Subject: [PATCH 01/12] bpo-46333: Honor `module` parameter in ForwardRef The `module` parameter carries semantic information about the forward ref. Forward refs are different if they refer to different module even if they have same name. This affects __eq__, __repr__ and __hash__ and methods. --- Lib/test/test_typing.py | 6 ++++++ Lib/typing.py | 11 ++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index a94d77d4edf4be..39b3769a39b0d0 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -2611,6 +2611,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') @@ -2673,6 +2677,8 @@ def fun(x: a): def test_forward_repr(self): self.assertEqual(repr(List['int']), "typing.List[ForwardRef('int')]") + self.assertEqual(repr(List[ForwardRef('int', module='mod')]), + "typing.List[ForwardRef('int', module='mod')]") def test_union_forward(self): diff --git a/Lib/typing.py b/Lib/typing.py index ae1dd5c2d76891..3fbf69c00f29eb 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -713,10 +713,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 __or__(self, other): return Union[self, other] @@ -725,7 +726,11 @@ def __ror__(self, other): return Union[other, self] def __repr__(self): - return f'ForwardRef({self.__forward_arg__!r})' + if self.__forward_module__ is not None: + return (f'ForwardRef({self.__forward_arg__!r},' + f' module={self.__forward_module__!r})') + else: + return f'ForwardRef({self.__forward_arg__!r})' class _TypeVarLike: """Mixin for TypeVar-like types (TypeVar and ParamSpec).""" From 82fa061653fc999715f98101d2e49a5a6c25d787 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 11 Jan 2022 15:54:16 +0000 Subject: [PATCH 02/12] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20b?= =?UTF-8?q?lurb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2022-01-11-15-54-15.bpo-46333.B1faiF.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-01-11-15-54-15.bpo-46333.B1faiF.rst 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 00000000000000..24dc584719a4fc --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-01-11-15-54-15.bpo-46333.B1faiF.rst @@ -0,0 +1 @@ +Honor module parameter in :class:`ForwardRef` as it carries semantic information about the forward ref. This affects :func:`__eq__`, :func:`__repr__` and :func:`__hash__` . \ No newline at end of file From ac51d98a75a4acf9e03d235c44025d163b9203ac Mon Sep 17 00:00:00 2001 From: aha79 <34090357+aha79@users.noreply.github.com> Date: Tue, 11 Jan 2022 16:56:09 +0100 Subject: [PATCH 03/12] Update Lib/test/test_typing.py Co-authored-by: Alex Waygood --- Lib/test/test_typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 39b3769a39b0d0..934db1085f4924 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -2614,7 +2614,7 @@ def test_forward_equality(self): 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___')) + self.assertNotEqual(frm, typing.ForwardRef('int', module='__other_name__')) def test_forward_equality_gth(self): c1 = typing.ForwardRef('C') From d6438610be0b3ba4f2bbbf639c4074627362721f Mon Sep 17 00:00:00 2001 From: aha79 <34090357+aha79@users.noreply.github.com> Date: Tue, 11 Jan 2022 16:56:35 +0100 Subject: [PATCH 04/12] Update Lib/typing.py Co-authored-by: Alex Waygood --- Lib/typing.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Lib/typing.py b/Lib/typing.py index 3fbf69c00f29eb..13a4774cc2478e 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -726,11 +726,11 @@ def __ror__(self, other): return Union[other, self] def __repr__(self): - if self.__forward_module__ is not None: - return (f'ForwardRef({self.__forward_arg__!r},' - f' module={self.__forward_module__!r})') + if self.__forward_module__ is None: + module_repr = '' else: - return f'ForwardRef({self.__forward_arg__!r})' + module_repr = f', module={self.__forward_module__!r}' + return f'ForwardRef({self.__forward_arg__!r}{module_repr})' class _TypeVarLike: """Mixin for TypeVar-like types (TypeVar and ParamSpec).""" From ac16c5c1c864237c6bc13568211bdf592cafc9a0 Mon Sep 17 00:00:00 2001 From: aha79 <34090357+aha79@users.noreply.github.com> Date: Tue, 11 Jan 2022 16:59:40 +0100 Subject: [PATCH 05/12] Update Misc/NEWS.d/next/Library/2022-01-11-15-54-15.bpo-46333.B1faiF.rst Co-authored-by: Alex Waygood --- .../next/Library/2022-01-11-15-54-15.bpo-46333.B1faiF.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 24dc584719a4fc..255fac290143f2 100644 --- 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 @@ -1 +1 @@ -Honor module parameter in :class:`ForwardRef` as it carries semantic information about the forward ref. This affects :func:`__eq__`, :func:`__repr__` and :func:`__hash__` . \ No newline at end of file +Honor the ``module`` parameter in :class:`typing.ForwardRef`, as it carries semantic information about the forward reference. This affects the :meth:`__eq__`, :meth:`__repr__` and :meth:`__hash__` methods of the class. \ No newline at end of file From a2e13772798b6fd844b3e903ad8c933c2a20b449 Mon Sep 17 00:00:00 2001 From: Andreas Hangauer Date: Tue, 11 Jan 2022 17:23:20 +0100 Subject: [PATCH 06/12] Update ACKS --- Misc/ACKS | 1 + 1 file changed, 1 insertion(+) diff --git a/Misc/ACKS b/Misc/ACKS index 7f2e94dfa615f1..7c4a37e52992b3 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 From c084fdbfdd6a64c75321243a2076060def52a174 Mon Sep 17 00:00:00 2001 From: aha79 <34090357+aha79@users.noreply.github.com> Date: Wed, 12 Jan 2022 14:55:18 +0100 Subject: [PATCH 07/12] Update Misc/NEWS.d/next/Library/2022-01-11-15-54-15.bpo-46333.B1faiF.rst Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> --- .../next/Library/2022-01-11-15-54-15.bpo-46333.B1faiF.rst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 index 255fac290143f2..9685b61996c166 100644 --- 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 @@ -1 +1,4 @@ -Honor the ``module`` parameter in :class:`typing.ForwardRef`, as it carries semantic information about the forward reference. This affects the :meth:`__eq__`, :meth:`__repr__` and :meth:`__hash__` methods of the class. \ No newline at end of file +The :meth:`__eq__`, :meth:`__repr__` 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. \ No newline at end of file From 75776331161f264f2077e6061775d22b88e59e80 Mon Sep 17 00:00:00 2001 From: Andreas Hangauer Date: Thu, 10 Feb 2022 12:38:01 +0100 Subject: [PATCH 08/12] Remove module output from __repr__ --- Lib/typing.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Lib/typing.py b/Lib/typing.py index 13a4774cc2478e..cf42289a66d2e2 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -726,11 +726,7 @@ def __ror__(self, other): return Union[other, self] def __repr__(self): - if self.__forward_module__ is None: - module_repr = '' - else: - module_repr = f', module={self.__forward_module__!r}' - return f'ForwardRef({self.__forward_arg__!r}{module_repr})' + return f'ForwardRef({self.__forward_arg__!r})' class _TypeVarLike: """Mixin for TypeVar-like types (TypeVar and ParamSpec).""" From 3df5009ef6eb2f8bb9961029b3f11007eadfe62b Mon Sep 17 00:00:00 2001 From: Andreas Hangauer Date: Thu, 10 Feb 2022 23:10:55 +0100 Subject: [PATCH 09/12] take out repr change --- Lib/test/test_typing.py | 2 -- .../next/Library/2022-01-11-15-54-15.bpo-46333.B1faiF.rst | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 934db1085f4924..f618e30f92d931 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -2677,8 +2677,6 @@ def fun(x: a): def test_forward_repr(self): self.assertEqual(repr(List['int']), "typing.List[ForwardRef('int')]") - self.assertEqual(repr(List[ForwardRef('int', module='mod')]), - "typing.List[ForwardRef('int', module='mod')]") def test_union_forward(self): 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 index 9685b61996c166..9f9eed0d6754f3 100644 --- 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 @@ -1,4 +1,4 @@ -The :meth:`__eq__`, :meth:`__repr__` and :meth:`__hash__` methods of +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. \ No newline at end of file From 42fc26bd82d3d901f069c51a47e261fc66124e27 Mon Sep 17 00:00:00 2001 From: aha79 <34090357+aha79@users.noreply.github.com> Date: Fri, 11 Feb 2022 09:01:19 +0100 Subject: [PATCH 10/12] Update Misc/NEWS.d/next/Library/2022-01-11-15-54-15.bpo-46333.B1faiF.rst Co-authored-by: Alex Waygood --- .../next/Library/2022-01-11-15-54-15.bpo-46333.B1faiF.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 9f9eed0d6754f3..ec3c6d54ee499c 100644 --- 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 @@ -1,4 +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. \ No newline at end of file +modules are now differentiated. From 9dd2f8fb605b625fe289c537016906c41ee999d2 Mon Sep 17 00:00:00 2001 From: Andreas Hangauer Date: Fri, 11 Feb 2022 09:37:34 +0100 Subject: [PATCH 11/12] add tests for ForwardRef.hash --- Lib/test/test_typing.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index f618e30f92d931..65f5c7eb0474ac 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -2616,6 +2616,14 @@ def test_forward_equality(self): self.assertEqual(frm, typing.ForwardRef('int', module=__name__)) self.assertNotEqual(frm, typing.ForwardRef('int', module='__other_name__')) + def test_forward_hash(self): + fr = typing.ForwardRef('int') + self.assertEqual(hash(fr), hash(typing.ForwardRef('int'))) + self.assertNotEqual(hash(fr), hash(typing.ForwardRef('int', module=__name__))) + frm = typing.ForwardRef('int', module=__name__) + self.assertEqual(hash(frm), hash(typing.ForwardRef('int', module=__name__))) + self.assertNotEqual(hash(frm), hash(typing.ForwardRef('int', module='__other_name__'))) + def test_forward_equality_gth(self): c1 = typing.ForwardRef('C') c1_gth = typing.ForwardRef('C') From 94c9a6999430fb7f878c432b73fc34bb6ae6496d Mon Sep 17 00:00:00 2001 From: Andreas Hangauer Date: Fri, 11 Feb 2022 10:57:04 +0100 Subject: [PATCH 12/12] move ForwardRef.__hash__ test to correct function --- Lib/test/test_typing.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 65f5c7eb0474ac..7d6d8ef6f2bb83 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -2616,14 +2616,6 @@ def test_forward_equality(self): self.assertEqual(frm, typing.ForwardRef('int', module=__name__)) self.assertNotEqual(frm, typing.ForwardRef('int', module='__other_name__')) - def test_forward_hash(self): - fr = typing.ForwardRef('int') - self.assertEqual(hash(fr), hash(typing.ForwardRef('int'))) - self.assertNotEqual(hash(fr), hash(typing.ForwardRef('int', module=__name__))) - frm = typing.ForwardRef('int', module=__name__) - self.assertEqual(hash(frm), hash(typing.ForwardRef('int', module=__name__))) - self.assertNotEqual(hash(frm), hash(typing.ForwardRef('int', module='__other_name__'))) - def test_forward_equality_gth(self): c1 = typing.ForwardRef('C') c1_gth = typing.ForwardRef('C') @@ -2659,6 +2651,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 pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy