Mailing List Archive

bpo-45662: Fix the repr of InitVar with a type alias to the built-in class (GH-29291)
https://github.com/python/cpython/commit/1fd4de5bddbbf2a97cdbac4d298c89e1156bdc6c
commit: 1fd4de5bddbbf2a97cdbac4d298c89e1156bdc6c
branch: main
author: Serhiy Storchaka <storchaka@gmail.com>
committer: serhiy-storchaka <storchaka@gmail.com>
date: 2021-12-05T22:41:58+02:00
summary:

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

For example, InitVar[list[int]].

files:
A Misc/NEWS.d/next/Library/2021-10-28-22-58-14.bpo-45662.sJd7Ir.rst
M Lib/dataclasses.py
M Lib/test/test_dataclasses.py

diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py
index 8643589077a4a..da06aa69148b7 100644
--- a/Lib/dataclasses.py
+++ b/Lib/dataclasses.py
@@ -229,7 +229,7 @@ def __init__(self, type):
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 bcd004f4ec3aa..47075df8d59f3 100644
--- a/Lib/test/test_dataclasses.py
+++ b/Lib/test/test_dataclasses.py
@@ -1126,6 +1126,10 @@ def test_init_var_preserve_type(self):
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
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 0000000000000..050b443dd7cb2
--- /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]]``.

_______________________________________________
Python-checkins mailing list
Python-checkins@python.org
https://mail.python.org/mailman/listinfo/python-checkins
bpo-45662: Fix the repr of InitVar with a type alias to the built-in class (GH-29291) [ In reply to ]
https://github.com/python/cpython/commit/f1dd5ed1f35a7ed5c3833c822e9965de2400d77e
commit: f1dd5ed1f35a7ed5c3833c822e9965de2400d77e
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
committer: miss-islington <31488909+miss-islington@users.noreply.github.com>
date: 2021-12-05T13:02:47-08:00
summary:

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 1fd4de5bddbbf2a97cdbac4d298c89e1156bdc6c)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>

files:
A Misc/NEWS.d/next/Library/2021-10-28-22-58-14.bpo-45662.sJd7Ir.rst
M Lib/dataclasses.py
M Lib/test/test_dataclasses.py

diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py
index b3a9194d9d068..fe4094b891b4a 100644
--- a/Lib/dataclasses.py
+++ b/Lib/dataclasses.py
@@ -229,7 +229,7 @@ def __init__(self, type):
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 bcd004f4ec3aa..47075df8d59f3 100644
--- a/Lib/test/test_dataclasses.py
+++ b/Lib/test/test_dataclasses.py
@@ -1126,6 +1126,10 @@ def test_init_var_preserve_type(self):
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
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 0000000000000..050b443dd7cb2
--- /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]]``.

_______________________________________________
Python-checkins mailing list
Python-checkins@python.org
https://mail.python.org/mailman/listinfo/python-checkins