]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-95987: Fix `repr` of `Any` type subclasses (GH-96412) (#96451)
authorShantanu <12621235+hauntsaninja@users.noreply.github.com>
Wed, 5 Oct 2022 22:02:06 +0000 (15:02 -0700)
committerGitHub <noreply@github.com>
Wed, 5 Oct 2022 22:02:06 +0000 (15:02 -0700)
(cherry picked from commit 4217393)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
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 1bcadf8753f95133b8eec229baf3f5a735d7edc6..71590449dc5639ec56c2dca3649640a2f5ac44bf 100644 (file)
@@ -109,6 +109,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 354976caaaa0072cebde510dd1f1c4180555e421..1e335bb7204d401c768f17374f280d16dd0da4b0 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.