bpo-45662: Fix the repr of InitVar with a type alias to the built-in … · python/cpython@f1dd5ed (original) (raw)

Skip to content

Provide feedback

Saved searches

Use saved searches to filter your results more quickly

Sign up

Appearance settings

Commit f1dd5ed

miss-islingtonserhiy-storchaka

and

authored

bpo-45662: Fix the repr of InitVar with a type alias to the built-in class (GH-29291)

For example, InitVar[list[int]]. (cherry picked from commit 1fd4de5) Co-authored-by: Serhiy Storchaka storchaka@gmail.com

File tree

3 files changed

lines changed

3 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -229,7 +229,7 @@ def __init__(self, type):
229 229 self.type = type
230 230
231 231 def __repr__(self):
232 -if isinstance(self.type, type):
232 +if isinstance(self.type, type) and not isinstance(self.type, GenericAlias):
233 233 type_name = self.type.__name__
234 234 else:
235 235 # typing objects, e.g. List[int]
Original file line number Diff line number Diff line change
@@ -1126,6 +1126,10 @@ def test_init_var_preserve_type(self):
1126 1126 self.assertEqual(repr(InitVar[int]), 'dataclasses.InitVar[int]')
1127 1127 self.assertEqual(repr(InitVar[List[int]]),
1128 1128 '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]')
1129 1133
1130 1134 def test_init_var_inheritance(self):
1131 1135 # Note that this deliberately tests that a dataclass need not
Original file line number Diff line number Diff 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]]``.