]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.9] bpo-45662: Fix the repr of InitVar with a type alias to the built-in class...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 10 Dec 2021 09:42:49 +0000 (01:42 -0800)
committerGitHub <noreply@github.com>
Fri, 10 Dec 2021 09:42:49 +0000 (11:42 +0200)
For example, InitVar[list[int]].
(cherry picked from commit 1fd4de5bddbbf2a97cdbac4d298c89e1156bdc6c)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Lib/dataclasses.py
Lib/test/test_dataclasses.py
Misc/NEWS.d/next/Library/2021-10-28-22-58-14.bpo-45662.sJd7Ir.rst [new file with mode: 0644]

index 68907058403fd6b3ff9709263ad9ba7f9016c287..5ff67ad2ea9ead8d9bf5f29095386faa47442b06 100644 (file)
@@ -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]
index fa5adfcea1b4312216e81350bd506cbaa11d88b7..9b6013456eb6031f4c503189200035958369e4dd 100644 (file)
@@ -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 (file)
index 0000000..050b443
--- /dev/null
@@ -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]]``.