Skip to content

Commit 8eec7ed

Browse files
gh-117110: Fix subclasses of typing.Any with custom constructors (#117111)
1 parent a17f313 commit 8eec7ed

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

Lib/test/test_typing.py

+20
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,26 @@ class MockSomething(Something, Mock): pass
140140
self.assertIsInstance(ms, Something)
141141
self.assertIsInstance(ms, Mock)
142142

143+
def test_subclassing_with_custom_constructor(self):
144+
class Sub(Any):
145+
def __init__(self, *args, **kwargs): pass
146+
# The instantiation must not fail.
147+
Sub(0, s="")
148+
149+
def test_multiple_inheritance_with_custom_constructors(self):
150+
class Foo:
151+
def __init__(self, x):
152+
self.x = x
153+
154+
class Bar(Any, Foo):
155+
def __init__(self, x, y):
156+
self.y = y
157+
super().__init__(x)
158+
159+
b = Bar(1, 2)
160+
self.assertEqual(b.x, 1)
161+
self.assertEqual(b.y, 2)
162+
143163
def test_cannot_instantiate(self):
144164
with self.assertRaises(TypeError):
145165
Any()

Lib/typing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ class Any(metaclass=_AnyMeta):
539539
def __new__(cls, *args, **kwargs):
540540
if cls is Any:
541541
raise TypeError("Any cannot be instantiated")
542-
return super().__new__(cls, *args, **kwargs)
542+
return super().__new__(cls)
543543

544544

545545
@_SpecialForm
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a bug that prevents subclasses of :class:`typing.Any` to be instantiated with arguments. Patch by Chris Fu.

0 commit comments

Comments
 (0)