]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-95987: Fix `repr` of `Any` type subclasses (#96412)
authorNikita Sobolev <mail@sobolevn.me>
Tue, 30 Aug 2022 17:36:16 +0000 (20:36 +0300)
committerGitHub <noreply@github.com>
Tue, 30 Aug 2022 17:36:16 +0000 (10:36 -0700)
Lib/test/test_typing.py
Lib/typing.py
Misc/NEWS.d/next/Library/2022-08-30-11-46-36.gh-issue-95987.CV7_u4.rst [new file with mode: 0644]

index 7eea01909ec83f35de17e95ac64751cda312ee1f..9239673c24802865936ebc9d1b723b5ae07ab5ff 100644 (file)
@@ -113,6 +113,12 @@ class AnyTests(BaseTestCase):
     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)
index 596744ed1322057d5116cd04056440a0f94dec54..84fe007a9ee6c241510cc347c5cc5b81cf27ae1a 100644 (file)
@@ -493,7 +493,9 @@ class _AnyMeta(type):
         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 (file)
index 0000000..232bba1
--- /dev/null
@@ -0,0 +1 @@
+Fix ``repr`` of ``Any`` subclasses.