diff options
author | aha79 <34090357+aha79@users.noreply.github.com> | 2022-02-12 15:35:57 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-12 15:35:57 (GMT) |
commit | b70690bb37cc4bac695051484734eede0c1f9ada (patch) | |
tree | 3ebf94f7f4ee7a00260db3b9e40b92c748303bfa | |
parent | 8aaaf7e182e22026c3487a3b86d4d7d4f0f5f778 (diff) | |
download | cpython-b70690bb37cc4bac695051484734eede0c1f9ada.zip cpython-b70690bb37cc4bac695051484734eede0c1f9ada.tar.gz cpython-b70690bb37cc4bac695051484734eede0c1f9ada.tar.bz2 |
bpo-46333: include `module` in `ForwardRef.__repr__` (#31283)
The module parameter carries semantic information about the forward ref.
Show to the user that forward refs with same argument but different
module are different.
Co-authored-by: Andreas Hangauer <andreas.hangauer@siemens.com>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
-rw-r--r-- | Lib/test/test_typing.py | 2 | ||||
-rw-r--r-- | Lib/typing.py | 6 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2022-02-11-20-01-49.bpo-46333.PMTBY9.rst | 3 |
3 files changed, 10 insertions, 1 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 6e2a2b1..2bb5d61 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -2862,6 +2862,8 @@ class ForwardRefTests(BaseTestCase): 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 1de48cc..4a8bdf8 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -779,7 +779,11 @@ class ForwardRef(_Final, _root=True): return Union[other, self] def __repr__(self): - return f'ForwardRef({self.__forward_arg__!r})' + 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})' class _TypeVarLike: """Mixin for TypeVar-like types (TypeVar and ParamSpec).""" diff --git a/Misc/NEWS.d/next/Library/2022-02-11-20-01-49.bpo-46333.PMTBY9.rst b/Misc/NEWS.d/next/Library/2022-02-11-20-01-49.bpo-46333.PMTBY9.rst new file mode 100644 index 0000000..669217e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-02-11-20-01-49.bpo-46333.PMTBY9.rst @@ -0,0 +1,3 @@ +The :meth:`__repr__` method of :class:`typing.ForwardRef` now
+includes the ``module`` parameter of :class:`typing.ForwardRef`
+when it is set. |