Skip to content

Commit 4217393

Browse files
authored
gh-95987: Fix repr of Any type subclasses (#96412)
1 parent 6d791a9 commit 4217393

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

Lib/test/test_typing.py

+6
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ def test_any_instance_type_error(self):
113113
def test_repr(self):
114114
self.assertEqual(repr(Any), 'typing.Any')
115115

116+
class Sub(Any): pass
117+
self.assertEqual(
118+
repr(Sub),
119+
"<class 'test.test_typing.AnyTests.test_repr.<locals>.Sub'>",
120+
)
121+
116122
def test_errors(self):
117123
with self.assertRaises(TypeError):
118124
issubclass(42, Any)

Lib/typing.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,9 @@ def __instancecheck__(self, obj):
493493
return super().__instancecheck__(obj)
494494

495495
def __repr__(self):
496-
return "typing.Any"
496+
if self is Any:
497+
return "typing.Any"
498+
return super().__repr__() # respect to subclasses
497499

498500

499501
class Any(metaclass=_AnyMeta):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix ``repr`` of ``Any`` subclasses.

0 commit comments

Comments
 (0)