summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2021-12-10 09:42:49 (GMT)
committerGitHub <noreply@github.com>2021-12-10 09:42:49 (GMT)
commitfb8aad16401e081a6a9059c7b428f7e8aae85d58 (patch)
tree0358580d4dade3ef387ee9ac575c6b7d8f9b529a
parent5ae4265b8c8042c496e569b6dbf9ef107e1d5b31 (diff)
downloadcpython-fb8aad16401e081a6a9059c7b428f7e8aae85d58.zip
cpython-fb8aad16401e081a6a9059c7b428f7e8aae85d58.tar.gz
cpython-fb8aad16401e081a6a9059c7b428f7e8aae85d58.tar.bz2
[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 1fd4de5bddbbf2a97cdbac4d298c89e1156bdc6c) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
-rw-r--r--Lib/dataclasses.py2
-rw-r--r--Lib/test/test_dataclasses.py2
-rw-r--r--Misc/NEWS.d/next/Library/2021-10-28-22-58-14.bpo-45662.sJd7Ir.rst2
3 files changed, 5 insertions, 1 deletions
diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py
index 6890705..5ff67ad 100644
--- a/Lib/dataclasses.py
+++ b/Lib/dataclasses.py
@@ -207,7 +207,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 fa5adfc..9b60134 100644
--- a/Lib/test/test_dataclasses.py
+++ b/Lib/test/test_dataclasses.py
@@ -1125,6 +1125,8 @@ 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]]')
def test_init_var_inheritance(self):
# Note that this deliberately tests that a dataclass need not
diff --git a/Misc/NEWS.d/next/Library/2021-10-28-22-58-14.bpo-45662.sJd7Ir.rst b/Misc/NEWS.d/next/Library/2021-10-28-22-58-14.bpo-45662.sJd7Ir.rst
new file mode 100644
index 0000000..050b443
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-10-28-22-58-14.bpo-45662.sJd7Ir.rst
@@ -0,0 +1,2 @@
+Fix the repr of :data:`dataclasses.InitVar` with a type alias to the
+built-in class, e.g. ``InitVar[list[int]]``.