8000 bpo-46333: include `module` in `ForwardRef.__repr__` (#31283) · python/cpython@b70690b · GitHub
[go: up one dir, main page]

Skip to content
< 8000 /div>

Commit b70690b

Browse files
aha79andreas-h-sieblurb-it[bot]
authored
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>
1 parent 8aaaf7e commit b70690b

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

Lib/test/test_typing.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2862,6 +2862,8 @@ def fun(x: a):
28622862

28632863
def test_forward_repr(self):
28642864
self.assertEqual(repr(List['int']), "typing.List[ForwardRef('int')]")
2865+
self.assertEqual(repr(List[ForwardRef('int', module='mod')]),
2866+
"typing.List[ForwardRef('int', module='mod')]")
28652867

28662868
def test_union_forward(self):
28672869

Lib/typing.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,11 @@ def __ror__(self, other):
779779
return Union[other, self]
780780

781781
def __repr__(self):
782-
return f'ForwardRef({self.__forward_arg__!r})'
782+
if self.__forward_module__ is None:
783+
module_repr = ''
784+
else:
785+
module_repr = f', module={self.__forward_module__!r}'
786+
return f'ForwardRef({self.__forward_arg__!r}{module_repr})'
783787

784788
class _TypeVarLike:
785789
"""Mixin for TypeVar-like types (TypeVar and ParamSpec)."""
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The :meth:`__repr__` method of :class:`typing.ForwardRef` now
2+
includes the ``module`` parameter of :class:`typing.ForwardRef`
3+
when it is set.

0 commit comments

Comments
 (0)
0