https://github.com/python/cpython/commit/9f831f442c8b0ff625887ed33dda80e1f77ee1aa
commit: 9f831f442c8b0ff625887ed33dda80e1f77ee1aa
branch: 3.12
author: Miss Islington (bot) <[email protected]>
committer: AlexWaygood <[email protected]>
date: 2024-03-29T00:36:14Z
summary:

[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 8eec7ed714e65d616573b7331780b0aa43c6ed6a)

Co-authored-by: 傅立业(Chris Fu) <[email protected]>

files:
A Misc/NEWS.d/next/Library/2024-03-21-07-27-36.gh-issue-117110.9K1InX.rst
M Lib/test/test_typing.py
M Lib/typing.py

diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 8ad26a77e5dbca..dc117b348210bb 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -141,6 +141,26 @@ class MockSomething(Something, Mock): pass
         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 1d8feed5ebcb82..c9962b9c9bccad 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -544,7 +544,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 00000000000000..32f8f81c8d052f
--- /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.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to