From 49751813084a60533a4ccdb7bd4b65e633e86560 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Tue, 30 Aug 2022 20:36:16 +0300 Subject: [PATCH] [3.11] gh-95987: Fix `repr` of `Any` type subclasses (GH-96412) (cherry picked from commit 4217393) Co-authored-by: Nikita Sobolev --- Lib/test/test_typing.py | 6 ++++++ Lib/typing.py | 4 +++- .../Library/2022-08-30-11-46-36.gh-issue-95987.CV7_u4.rst | 1 + 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2022-08-30-11-46-36.gh-issue-95987.CV7_u4.rst diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 776a6f003c0691..8f6a6877c369f1 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -109,6 +109,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), + ".Sub'>", + ) + def test_errors(self): with self.assertRaises(TypeError): issubclass(42, Any) diff --git a/Lib/typing.py b/Lib/typing.py index 354976caaaa007..1e335bb7204d40 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 00000000000000..232bba1b92440b --- /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.