From 8eec7ed714e65d616573b7331780b0aa43c6ed6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=82=85=E7=AB=8B=E4=B8=9A=EF=BC=88Chris=20Fu=EF=BC=89?= <17433201@qq.com> Date: Fri, 29 Mar 2024 08:19:20 +0800 Subject: gh-117110: Fix subclasses of typing.Any with custom constructors (#117111) --- Lib/test/test_typing.py | 20 ++++++++++++++++++++ Lib/typing.py | 2 +- .../2024-03-21-07-27-36.gh-issue-117110.9K1InX.rst | 1 + 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2024-03-21-07-27-36.gh-issue-117110.9K1InX.rst diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 54c7b97..927f74e 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -140,6 +140,26 @@ class AnyTests(BaseTestCase): self.assertIsInstance(ms, Something) 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): with self.assertRaises(TypeError): Any() diff --git a/Lib/typing.py b/Lib/typing.py index 581d187..ef532f6 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -539,7 +539,7 @@ class Any(metaclass=_AnyMeta): def __new__(cls, *args, **kwargs): if cls is Any: raise TypeError("Any cannot be instantiated") - return super().__new__(cls, *args, **kwargs) + return super().__new__(cls) @_SpecialForm diff --git a/Misc/NEWS.d/next/Library/2024-03-21-07-27-36.gh-issue-117110.9K1InX.rst b/Misc/NEWS.d/next/Library/2024-03-21-07-27-36.gh-issue-117110.9K1InX.rst new file mode 100644 index 0000000..32f8f81 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-03-21-07-27-36.gh-issue-117110.9K1InX.rst @@ -0,0 +1 @@ +Fix a bug that prevents subclasses of :class:`typing.Any` to be instantiated with arguments. Patch by Chris Fu. -- cgit v0.12