Commit 793cb854 authored by Samuel Colvin's avatar Samuel Colvin Committed by Serhiy Storchaka

bpo-38431: Fix __repr__ method of InitVar to work with typing objects. (GH-16702)

parent 140a7d1f
...@@ -206,7 +206,12 @@ class InitVar: ...@@ -206,7 +206,12 @@ class InitVar:
self.type = type self.type = type
def __repr__(self): 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}]'
def __class_getitem__(cls, type): def __class_getitem__(cls, type):
return InitVar(type) return InitVar(type)
......
...@@ -1102,6 +1102,8 @@ class TestCase(unittest.TestCase): ...@@ -1102,6 +1102,8 @@ class TestCase(unittest.TestCase):
# Make sure the repr is correct. # Make sure the repr is correct.
self.assertEqual(repr(InitVar[int]), 'dataclasses.InitVar[int]') 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): def test_init_var_inheritance(self):
# Note that this deliberately tests that a dataclass need not # Note that this deliberately tests that a dataclass need not
......
Fix ``__repr__`` method for :class:`dataclasses.InitVar` to support typing objects, patch by Samuel Colvin.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment