[Python-checkins] gh-95987: Fix `repr` of `Any` type subclasses (#96412)

gvanrossum webhook-mailer at python.org
Tue Aug 30 13:36:35 EDT 2022


https://github.com/python/cpython/commit/4217393aeed42d67dd4b16a128528f5ca8d939c4
commit: 4217393aeed42d67dd4b16a128528f5ca8d939c4
branch: main
author: Nikita Sobolev <mail at sobolevn.me>
committer: gvanrossum <gvanrossum at gmail.com>
date: 2022-08-30T10:36:16-07:00
summary:

gh-95987: Fix `repr` of `Any` type subclasses (#96412)

files:
A Misc/NEWS.d/next/Library/2022-08-30-11-46-36.gh-issue-95987.CV7_u4.rst
M Lib/test/test_typing.py
M Lib/typing.py

diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 7eea01909ec..9239673c248 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -113,6 +113,12 @@ def test_any_instance_type_error(self):
     def test_repr(self):
         self.assertEqual(repr(Any), 'typing.Any')
 
+        class Sub(Any): pass
+        self.assertEqual(
+            repr(Sub),
+            "<class 'test.test_typing.AnyTests.test_repr.<locals>.Sub'>",
+        )
+
     def test_errors(self):
         with self.assertRaises(TypeError):
             issubclass(42, Any)
diff --git a/Lib/typing.py b/Lib/typing.py
index 596744ed132..84fe007a9ee 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -493,7 +493,9 @@ def __instancecheck__(self, obj):
         return super().__instancecheck__(obj)
 
     def __repr__(self):
-        return "typing.Any"
+        if self is Any:
+            return "typing.Any"
+        return super().__repr__()  # respect to subclasses
 
 
 class Any(metaclass=_AnyMeta):
diff --git a/Misc/NEWS.d/next/Library/2022-08-30-11-46-36.gh-issue-95987.CV7_u4.rst b/Misc/NEWS.d/next/Library/2022-08-30-11-46-36.gh-issue-95987.CV7_u4.rst
new file mode 100644
index 00000000000..232bba1b924
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-08-30-11-46-36.gh-issue-95987.CV7_u4.rst
@@ -0,0 +1 @@
+Fix ``repr`` of ``Any`` subclasses.



More information about the Python-checkins mailing list