Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,12 @@ def __init__(self, type):
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}]'

def __class_getitem__(cls, type):
return InitVar(type)
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,8 @@ def test_init_var_preserve_type(self):

# 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
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ Benjamin Collar
Jeffery Collins
Robert Collins
Paul Colomiets
Samuel Colvin
Christophe Combelles
Geremy Condra
Denver Coneybeare
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix ``__repr__`` method for :class:`dataclasses.InitVar` to support typing objects, patch by Samuel Colvin.