mirror of
https://github.com/python/cpython.git
synced 2025-11-26 13:22:51 +00:00
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>
This commit is contained in:
parent
8aaaf7e182
commit
b70690bb37
3 changed files with 10 additions and 1 deletions
|
|
@ -2862,6 +2862,8 @@ class ForwardRefTests(BaseTestCase):
|
||||||
|
|
||||||
def test_forward_repr(self):
|
def test_forward_repr(self):
|
||||||
self.assertEqual(repr(List['int']), "typing.List[ForwardRef('int')]")
|
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):
|
def test_union_forward(self):
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -779,7 +779,11 @@ class ForwardRef(_Final, _root=True):
|
||||||
return Union[other, self]
|
return Union[other, self]
|
||||||
|
|
||||||
def __repr__(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:
|
class _TypeVarLike:
|
||||||
"""Mixin for TypeVar-like types (TypeVar and ParamSpec)."""
|
"""Mixin for TypeVar-like types (TypeVar and ParamSpec)."""
|
||||||
|
|
|
||||||
|
|
@ -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.
|
||||||
Loading…
Add table
Add a link
Reference in a new issue