]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-43827: Make arguments to abc.ABCMeta.__new__ pos-only (#25385)
authorVlad Hoi <hoivladyslav@yahoo.com>
Thu, 5 May 2022 13:40:01 +0000 (16:40 +0300)
committerGitHub <noreply@github.com>
Thu, 5 May 2022 13:40:01 +0000 (06:40 -0700)
To avoid conflicts with `__init__subclass__`.

Lib/abc.py
Lib/test/test_abc.py
Misc/ACKS
Misc/NEWS.d/next/Library/2021-04-16-17-32-44.bpo-43827.uJaXdP.rst [new file with mode: 0644]

index 3c552cebb4226c02bd6f741c5446623a0cd6a7ed..42048ddb855381f7ec7f10be2aa1b7d437107989 100644 (file)
@@ -102,7 +102,7 @@ else:
         implementations defined by the registering ABC be callable (not
         even via super()).
         """
-        def __new__(mcls, name, bases, namespace, **kwargs):
+        def __new__(mcls, name, bases, namespace, /, **kwargs):
             cls = super().__new__(mcls, name, bases, namespace, **kwargs)
             _abc_init(cls)
             return cls
index c1d750dba83f99d22088f8fb46bfbb66cecc3510..1e7a0351db489d13c9577b42229b6aa239bcfbf6 100644 (file)
@@ -668,6 +668,19 @@ def test_factory(abc_ABCMeta, abc_get_cache_token):
             class Receiver(ReceivesClassKwargs, abc_ABC, x=1, y=2, z=3):
                 pass
             self.assertEqual(saved_kwargs, dict(x=1, y=2, z=3))
+
+        def test_positional_only_and_kwonlyargs_with_init_subclass(self):
+            saved_kwargs = {}
+
+            class A:
+                def __init_subclass__(cls, **kwargs):
+                    super().__init_subclass__()
+                    saved_kwargs.update(kwargs)
+
+            class B(A, metaclass=abc_ABCMeta, name="test"):
+                pass
+            self.assertEqual(saved_kwargs, dict(name="test"))
+
     return TestLegacyAPI, TestABC, TestABCWithInitSubclass
 
 TestLegacyAPI_Py, TestABC_Py, TestABCWithInitSubclass_Py = test_factory(abc.ABCMeta,
index ec4de61ff9273e54623aa226d1a59a6eaa4abce7..2b9485e7aa80c9cda858df63c886e3271addb23d 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -753,6 +753,7 @@ Albert Hofkamp
 Chris Hogan
 Tomas Hoger
 Jonathan Hogg
+Vladyslav Hoi
 Kamilla Holanda
 Steve Holden
 Akintayo Holder
diff --git a/Misc/NEWS.d/next/Library/2021-04-16-17-32-44.bpo-43827.uJaXdP.rst b/Misc/NEWS.d/next/Library/2021-04-16-17-32-44.bpo-43827.uJaXdP.rst
new file mode 100644 (file)
index 0000000..6e4f82f
--- /dev/null
@@ -0,0 +1 @@
+All positional-or-keyword parameters to ``ABCMeta.__new__`` are now positional-only to avoid conflicts with keyword arguments to be passed to :meth:`__init_subclass__`.