mirror of
https://github.com/python/cpython.git
synced 2025-08-08 10:58:51 +00:00
[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 8eec7ed714
)
Co-authored-by: 傅立业(Chris Fu) <17433201@qq.com>
This commit is contained in:
parent
655c4255fc
commit
9f831f442c
3 changed files with 22 additions and 1 deletions
|
@ -141,6 +141,26 @@ class AnyTests(BaseTestCase):
|
||||||
self.assertIsInstance(ms, Something)
|
self.assertIsInstance(ms, Something)
|
||||||
self.assertIsInstance(ms, Mock)
|
self.assertIsInstance(ms, Mock)
|
||||||
|
|
||||||
|
def test_subclassing_with_custom_constructor(self):
|
||||||
|
class Sub(Any):
|
||||||
|
def __init__(self, *args, **kwargs): pass
|
||||||
|
# The instantiation must not fail.
|
||||||
|
Sub(0, s="")
|
||||||
|
|
||||||
|
def test_multiple_inheritance_with_custom_constructors(self):
|
||||||
|
class Foo:
|
||||||
|
def __init__(self, x):
|
||||||
|
self.x = x
|
||||||
|
|
||||||
|
class Bar(Any, Foo):
|
||||||
|
def __init__(self, x, y):
|
||||||
|
self.y = y
|
||||||
|
super().__init__(x)
|
||||||
|
|
||||||
|
b = Bar(1, 2)
|
||||||
|
self.assertEqual(b.x, 1)
|
||||||
|
self.assertEqual(b.y, 2)
|
||||||
|
|
||||||
def test_cannot_instantiate(self):
|
def test_cannot_instantiate(self):
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
Any()
|
Any()
|
||||||
|
|
|
@ -544,7 +544,7 @@ class Any(metaclass=_AnyMeta):
|
||||||
def __new__(cls, *args, **kwargs):
|
def __new__(cls, *args, **kwargs):
|
||||||
if cls is Any:
|
if cls is Any:
|
||||||
raise TypeError("Any cannot be instantiated")
|
raise TypeError("Any cannot be instantiated")
|
||||||
return super().__new__(cls, *args, **kwargs)
|
return super().__new__(cls)
|
||||||
|
|
||||||
|
|
||||||
@_SpecialForm
|
@_SpecialForm
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix a bug that prevents subclasses of :class:`typing.Any` to be instantiated with arguments. Patch by Chris Fu.
|
Loading…
Add table
Add a link
Reference in a new issue