8000 [3.12] gh-117110: Fix subclasses of typing.Any with custom constructo… · python/cpython@9f831f4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9f831f4

Browse files
[3.12] gh-117110: Fix subclasses of typing.Any with custom constructors (GH-117111) (#117357)
gh-117110: Fix subclasses of typing.Any with custom constructors (GH-117111) (cherry picked from commit 8eec7ed) Co-authored-by: 傅立业(Chris Fu) <17433201@qq.com>
1 parent 655c425 commit 9f831f4

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
@@ -141,6 +141,26 @@ class MockSomething(Something, Mock): pass
141141
self.assertIsInstance(ms, Something)
142142
self.assertIsInstance(ms, Mock)
143143

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

Lib/typing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ class Any(metaclass=_AnyMeta):
544544
def __new__(cls, *args, **kwargs):
545545
if cls is Any:
546546
raise TypeError("Any cannot be instantiated")
547-
return super().__new__(cls, *args, **kwargs)
547+
return super().__new__(cls)
548548

549549

550550
@_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)
0