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:
Miss Islington (bot) 2019-10-13 05:04:05 -07:00 committed by GitHub
parent ba44ea6ff8
commit 6da52ac411
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 1 deletions

View File

@ -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,

View File

@ -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

View File

@ -0,0 +1 @@
Fix ``__repr__`` method for :class:`dataclasses.InitVar` to support typing objects, patch by Samuel Colvin.