]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-117110: Fix subclasses of typing.Any with custom constructors (GH-117111...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 29 Mar 2024 00:38:13 +0000 (01:38 +0100)
committerGitHub <noreply@github.com>
Fri, 29 Mar 2024 00:38:13 +0000 (00:38 +0000)
gh-117110: Fix subclasses of typing.Any with custom constructors (GH-117111)
(cherry picked from commit 8eec7ed714e65d616573b7331780b0aa43c6ed6a)

Co-authored-by: 傅立业(Chris Fu) <17433201@qq.com>
Lib/test/test_typing.py
Lib/typing.py
Misc/NEWS.d/next/Library/2024-03-21-07-27-36.gh-issue-117110.9K1InX.rst [new file with mode: 0644]

index ab430e3f76d150af174a79cee2e73d95d27cb136..9804f915abc2d6832280baf747b7320340c13ceb 100644 (file)
@@ -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()
index e3c9e6e786721056796a6ff44de7b6500cad0b09..c26d01f76ace55648e0385b6887feb9a9208b6bc 100644 (file)
@@ -536,7 +536,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 (file)
index 0000000..32f8f81
--- /dev/null
@@ -0,0 +1 @@
+Fix a bug that prevents subclasses of :class:`typing.Any` to be instantiated with arguments. Patch by Chris Fu.