Skip to content

Commit 01ab43c

Browse files
[3.11] gh-117110: Fix subclasses of typing.Any with custom constructors (GH-117111) (#117358)
gh-117110: Fix subclasses of typing.Any with custom constructors (GH-117111) (cherry picked from commit 8eec7ed) Co-authored-by: 傅立业(Chris Fu) <[email protected]>
1 parent 8bbb121 commit 01ab43c

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

Lib/test/test_typing.py

Lines changed: 20 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ class Any(metaclass=_AnyMeta):
536536
def __new__(cls, *args, **kwargs):
537537
if cls is Any:
538538
raise TypeError("Any cannot be instantiated")
539-
return super().__new__(cls, *args, **kwargs)
539+
return super().__new__(cls)
540540

541541

542542
@_SpecialForm
Lines changed: 1 addition & 0 deletions
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)