[Python-checkins] bpo-43827: Make arguments to abc.ABCMeta.__new__ pos-only (#25385)

JelleZijlstra webhook-mailer at python.org
Thu May 5 09:40:09 EDT 2022


https://github.com/python/cpython/commit/42fee931d055a3ef8ed31abe44603b9b2856e04d
commit: 42fee931d055a3ef8ed31abe44603b9b2856e04d
branch: main
author: Vlad Hoi <hoivladyslav at yahoo.com>
committer: JelleZijlstra <jelle.zijlstra at gmail.com>
date: 2022-05-05T06:40:01-07:00
summary:

bpo-43827: Make arguments to abc.ABCMeta.__new__ pos-only (#25385)

To avoid conflicts with `__init__subclass__`.

files:
A Misc/NEWS.d/next/Library/2021-04-16-17-32-44.bpo-43827.uJaXdP.rst
M Lib/abc.py
M Lib/test/test_abc.py
M Misc/ACKS

diff --git a/Lib/abc.py b/Lib/abc.py
index 3c552cebb4226..42048ddb85538 100644
--- a/Lib/abc.py
+++ b/Lib/abc.py
@@ -102,7 +102,7 @@ class ABCMeta(type):
         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
diff --git a/Lib/test/test_abc.py b/Lib/test/test_abc.py
index c1d750dba83f9..1e7a0351db489 100644
--- a/Lib/test/test_abc.py
+++ b/Lib/test/test_abc.py
@@ -668,6 +668,19 @@ def __init_subclass__(cls, **kwargs):
             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,
diff --git a/Misc/ACKS b/Misc/ACKS
index ec4de61ff9273..2b9485e7aa80c 100644
--- 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
index 0000000000000..6e4f82f2d27eb
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-04-16-17-32-44.bpo-43827.uJaXdP.rst
@@ -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__`.



More information about the Python-checkins mailing list