diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py index 9135b07c9f259a9..91c1f6f80fc6846 100644 --- a/Lib/dataclasses.py +++ b/Lib/dataclasses.py @@ -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) diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py index 037bf4c22142798..238335e7d9edad0 100644 --- a/Lib/test/test_dataclasses.py +++ b/Lib/test/test_dataclasses.py @@ -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 diff --git a/Misc/ACKS b/Misc/ACKS index 71e61c3db386c13..d8e2630814a869f 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -321,6 +321,7 @@ Benjamin Collar Jeffery Collins Robert Collins Paul Colomiets +Samuel Colvin Christophe Combelles Geremy Condra Denver Coneybeare diff --git a/Misc/NEWS.d/next/Library/2019-10-10-16-53-00.bpo-38431.d5wzNp.rst b/Misc/NEWS.d/next/Library/2019-10-10-16-53-00.bpo-38431.d5wzNp.rst new file mode 100644 index 000000000000000..c2f860d804c13aa --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-10-10-16-53-00.bpo-38431.d5wzNp.rst @@ -0,0 +1 @@ +Fix ``__repr__`` method for :class:`dataclasses.InitVar` to support typing objects, patch by Samuel Colvin.