Skip to content

Commit 1fd4de5

Browse files
authoredDec 5, 2021
bpo-45662: Fix the repr of InitVar with a type alias to the built-in class (GH-29291)
For example, InitVar[list[int]].
1 parent 60c320c commit 1fd4de5

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed
 

‎Lib/dataclasses.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def __init__(self, type):
229229
self.type = type
230230

231231
def __repr__(self):
232-
if isinstance(self.type, type):
232+
if isinstance(self.type, type) and not isinstance(self.type, GenericAlias):
233233
type_name = self.type.__name__
234234
else:
235235
# typing objects, e.g. List[int]

‎Lib/test/test_dataclasses.py

+4
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,10 @@ def test_init_var_preserve_type(self):
11261126
self.assertEqual(repr(InitVar[int]), 'dataclasses.InitVar[int]')
11271127
self.assertEqual(repr(InitVar[List[int]]),
11281128
'dataclasses.InitVar[typing.List[int]]')
1129+
self.assertEqual(repr(InitVar[list[int]]),
1130+
'dataclasses.InitVar[list[int]]')
1131+
self.assertEqual(repr(InitVar[int|str]),
1132+
'dataclasses.InitVar[int | str]')
11291133

11301134
def test_init_var_inheritance(self):
11311135
# Note that this deliberately tests that a dataclass need not
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix the repr of :data:`dataclasses.InitVar` with a type alias to the
2+
built-in class, e.g. ``InitVar[list[int]]``.

0 commit comments

Comments
 (0)
Please sign in to comment.