diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2021-12-05 20:41:58 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-05 20:41:58 (GMT) |
commit | 1fd4de5bddbbf2a97cdbac4d298c89e1156bdc6c (patch) | |
tree | 905cb4c796f82a7ce786b767f63220202f701d07 /Lib | |
parent | 60c320c38e4e95877cde0b1d8562ebd6bc02ac61 (diff) | |
download | cpython-1fd4de5bddbbf2a97cdbac4d298c89e1156bdc6c.zip cpython-1fd4de5bddbbf2a97cdbac4d298c89e1156bdc6c.tar.gz cpython-1fd4de5bddbbf2a97cdbac4d298c89e1156bdc6c.tar.bz2 |
bpo-45662: Fix the repr of InitVar with a type alias to the built-in class (GH-29291)
For example, InitVar[list[int]].
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/dataclasses.py | 2 | ||||
-rw-r--r-- | Lib/test/test_dataclasses.py | 4 |
2 files changed, 5 insertions, 1 deletions
diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py index 8643589..da06aa6 100644 --- a/Lib/dataclasses.py +++ b/Lib/dataclasses.py @@ -229,7 +229,7 @@ class InitVar: self.type = type def __repr__(self): - if isinstance(self.type, type): + if isinstance(self.type, type) and not isinstance(self.type, GenericAlias): type_name = self.type.__name__ else: # typing objects, e.g. List[int] diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py index bcd004f..47075df 100644 --- a/Lib/test/test_dataclasses.py +++ b/Lib/test/test_dataclasses.py @@ -1126,6 +1126,10 @@ class TestCase(unittest.TestCase): self.assertEqual(repr(InitVar[int]), 'dataclasses.InitVar[int]') self.assertEqual(repr(InitVar[List[int]]), 'dataclasses.InitVar[typing.List[int]]') + self.assertEqual(repr(InitVar[list[int]]), + 'dataclasses.InitVar[list[int]]') + self.assertEqual(repr(InitVar[int|str]), + 'dataclasses.InitVar[int | str]') def test_init_var_inheritance(self): # Note that this deliberately tests that a dataclass need not |