bpo-38431: Fix __repr__ method of InitVar to work with typing objects. (GH-16702)
(cherry picked from commit 793cb85437
)
Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
This commit is contained in:
parent
ba44ea6ff8
commit
6da52ac411
|
@ -210,7 +210,12 @@ class InitVar(metaclass=_InitVarMeta):
|
|||
self.type = type
|
||||
|
||||
def __repr__(self):
|
||||
return f'dataclasses.InitVar[{self.type.__name__}]'
|
||||
if isinstance(self.type, type):
|
||||
type_name = self.type.__name__
|
||||
else:
|
||||
# typing objects, e.g. List[int]
|
||||
type_name = repr(self.type)
|
||||
return f'dataclasses.InitVar[{type_name}]'
|
||||
|
||||
|
||||
# Instances of Field are only ever created from within this module,
|
||||
|
|
|
@ -1102,6 +1102,8 @@ class TestCase(unittest.TestCase):
|
|||
|
||||
# Make sure the repr is correct.
|
||||
self.assertEqual(repr(InitVar[int]), 'dataclasses.InitVar[int]')
|
||||
self.assertEqual(repr(InitVar[List[int]]),
|
||||
'dataclasses.InitVar[typing.List[int]]')
|
||||
|
||||
def test_init_var_inheritance(self):
|
||||
# Note that this deliberately tests that a dataclass need not
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
Fix ``__repr__`` method for :class:`dataclasses.InitVar` to support typing objects, patch by Samuel Colvin.
|
Loading…
Reference in New Issue