]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-117110: Fix subclasses of typing.Any with custom constructors (#117111)
author傅立业(Chris Fu) <17433201@qq.com>
Fri, 29 Mar 2024 00:19:20 +0000 (08:19 +0800)
committerGitHub <noreply@github.com>
Fri, 29 Mar 2024 00:19:20 +0000 (00:19 +0000)
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 54c7b976185585b06f4d6994ad9b332638144b0c..927f74eb69fbc7613de2ddd39582e6606fda24fa 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 581d187235dc7e05f1dd289263f669cafdcb9259..ef532f6c91539d8a63a4a8164bdc9911633d7a72 100644 (file)
@@ -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 (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.