From e0451430f8875e6f1a7f5c0d4aff31cdcb99d9bc Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Thu, 10 Oct 2019 16:55:36 +0100 Subject: [PATCH 1/4] bpo-38431: fix dataclasses.InitVar.__repr__ Fix repr method of InitVar to work with typing objects. --- Lib/dataclasses.py | 7 ++++++- Lib/test/test_dataclasses.py | 5 +++++ Misc/ACKS | 1 + .../next/Library/2019-10-10-16-53-00.bpo-38431.d5wzNp.rst | 1 + 4 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2019-10-10-16-53-00.bpo-38431.d5wzNp.rst diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py index 9135b07c9f259a9..8062b6ca03d4c26 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__}]' + try: + type_name = self.type.__name__ + except AttributeError: + # typing objects, e.g. Union + 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..980c9599b9105f4 100644 --- a/Lib/test/test_dataclasses.py +++ b/Lib/test/test_dataclasses.py @@ -2883,6 +2883,11 @@ class C: # x is not an InitVar, so there will be a member x. self.assertEqual(C(10).x, 10) + def test_initvar_repr(self): + self.assertEqual(repr(InitVar[str]), 'dataclasses.InitVar[str]') + self.assertEqual(repr(InitVar[Optional[str]]), + 'dataclasses.InitVar[typing.Union[str, NoneType]]') + def test_classvar_module_level_import(self): from test import dataclass_module_1 from test import dataclass_module_1_str 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..730ee987bfb52fb --- /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. From 86c8688158275b3ec772adad7e3cefba52e312e2 Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Thu, 10 Oct 2019 17:25:37 +0100 Subject: [PATCH 2/4] fix test and InitVar comments --- Lib/dataclasses.py | 6 +++--- Lib/test/test_dataclasses.py | 7 ++----- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py index 8062b6ca03d4c26..91c1f6f80fc6846 100644 --- a/Lib/dataclasses.py +++ b/Lib/dataclasses.py @@ -206,10 +206,10 @@ def __init__(self, type): self.type = type def __repr__(self): - try: + if isinstance(self.type, type): type_name = self.type.__name__ - except AttributeError: - # typing objects, e.g. Union + else: + # typing objects, e.g. List[int] type_name = repr(self.type) return f'dataclasses.InitVar[{type_name}]' diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py index 980c9599b9105f4..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 @@ -2883,11 +2885,6 @@ class C: # x is not an InitVar, so there will be a member x. self.assertEqual(C(10).x, 10) - def test_initvar_repr(self): - self.assertEqual(repr(InitVar[str]), 'dataclasses.InitVar[str]') - self.assertEqual(repr(InitVar[Optional[str]]), - 'dataclasses.InitVar[typing.Union[str, NoneType]]') - def test_classvar_module_level_import(self): from test import dataclass_module_1 from test import dataclass_module_1_str From 5c301dabe6f4327fc8609c4b72280dcd30f98106 Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Thu, 10 Oct 2019 17:29:00 +0100 Subject: [PATCH 3/4] update news as suggested --- .../next/Library/2019-10-10-16-53-00.bpo-38431.d5wzNp.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 index 730ee987bfb52fb..a07a18a970a8fdc 100644 --- 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 @@ -1 +1,2 @@ -Fix ``__repr__`` method for :class:`dataclasses.InitVar` to support typing objects. +Fix ``__repr__`` method for :class:`dataclasses.InitVar` to support typing objects, +patched by: Samuel Colvin. From 425edc72bbc5f9c5dc99facf7bcaba3d2062450c Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Thu, 10 Oct 2019 17:59:24 +0100 Subject: [PATCH 4/4] correct tense of news entry --- .../next/Library/2019-10-10-16-53-00.bpo-38431.d5wzNp.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 index a07a18a970a8fdc..c2f860d804c13aa 100644 --- 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 @@ -1,2 +1 @@ -Fix ``__repr__`` method for :class:`dataclasses.InitVar` to support typing objects, -patched by: Samuel Colvin. +Fix ``__repr__`` method for :class:`dataclasses.InitVar` to support typing objects, patch by Samuel Colvin.