Skip to content

Commit fb8aad1

Browse files
[3.9] bpo-45662: Fix the repr of InitVar with a type alias to the built-in class (GH-29291) (GH-29924)
For example, InitVar[list[int]]. (cherry picked from commit 1fd4de5) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 5ae4265 commit fb8aad1

File tree

3 files changed

+5
-1
lines changed

3 files changed

+5
-1
lines changed

Lib/dataclasses.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def __init__(self, type):
207207
self.type = type
208208

209209
def __repr__(self):
210-
if isinstance(self.type, type):
210+
if isinstance(self.type, type) and not isinstance(self.type, GenericAlias):
211211
type_name = self.type.__name__
212212
else:
213213
# typing objects, e.g. List[int]

Lib/test/test_dataclasses.py

+2
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,8 @@ def test_init_var_preserve_type(self):
11251125
self.assertEqual(repr(InitVar[int]), 'dataclasses.InitVar[int]')
11261126
self.assertEqual(repr(InitVar[List[int]]),
11271127
'dataclasses.InitVar[typing.List[int]]')
1128+
self.assertEqual(repr(InitVar[list[int]]),
1129+
'dataclasses.InitVar[list[int]]')
11281130

11291131
def test_init_var_inheritance(self):
11301132
# 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)