From c21569bb6b452d245bbf567aa0683b7b7913d4b7 Mon Sep 17 00:00:00 2001 From: Cody Maloney Date: Tue, 27 Oct 2026 17:44:04 -0700 Subject: [PATCH] gh-87613: Add always_reject_kwargs to Argument Clinic's self converter Several types reject keyword arguments in their `__init__~ even when a subclass overrides `__new__`. `Exception` and `set` are two places with the pattern. Add support for the pattern in Argument Clinic by extending the self converter to be able to flag it. Co-Authored-By: Claude Opus 5.5 --- Lib/test/clinic.test.c | 8 +++----- Lib/test/test_clinic.py | 12 ++++++++++++ Tools/clinic/libclinic/converters.py | 4 +++- Tools/clinic/libclinic/parse_args.py | 14 +++++++++++--- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/Lib/test/clinic.test.c b/Lib/test/clinic.test.c index 3b59db65f23b6f6..64d143c51613aa5 100644 --- a/Lib/test/clinic.test.c +++ b/Lib/test/clinic.test.c @@ -5175,6 +5175,7 @@ Test_an_metho_arg_named_arg_impl(TestObj *self, int arg) /*[clinic input] Test.__init__ + self: self(type="TestObj *", always_reject_kwargs=True) *args: tuple Varargs init method. @@ -5197,12 +5198,9 @@ static int Test___init__(PyObject *self, PyObject *args, PyObject *kwargs) { int return_value = -1; - PyTypeObject *base_tp = TestType; PyObject *__clinic_args = NULL; - if ((Py_IS_TYPE(self, base_tp) || - Py_TYPE(self)->tp_new == base_tp->tp_new) && - !_PyArg_NoKeywords("Test", kwargs)) { + if (!_PyArg_NoKeywords("Test", kwargs)) { goto exit; } __clinic_args = Py_NewRef(args); @@ -5217,7 +5215,7 @@ Test___init__(PyObject *self, PyObject *args, PyObject *kwargs) static int Test___init___impl(TestObj *self, PyObject *args) -/*[clinic end generated code: output=0e5836c40dbc2397 input=a615a4485c0fc3e2]*/ +/*[clinic end generated code: output=d912a2c5da6467d9 input=8e0d779137ce4f66]*/ /*[clinic input] @vectorcall diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py index cb4ed55fabbbc1d..97c38f87e50e71f 100644 --- a/Lib/test/test_clinic.py +++ b/Lib/test/test_clinic.py @@ -132,6 +132,18 @@ def test_mangled_marker_line(self): ) self.expect_failure(raw, err, filename="test.c", lineno=3) + def test_always_reject_kwargs_with_keyword_parameter(self): + raw = """ + /*[clinic input] + class Foo "FooObject *" "Foo_Type" + Foo.__init__ + self: self(always_reject_kwargs=True) + a: object + [clinic start generated code]*/ + """ + err = "always_reject_kwargs is incompatible with keyword parameters" + self.expect_failure(raw, err) + def test_checksum_mismatch(self): raw = """ /*[clinic input] diff --git a/Tools/clinic/libclinic/converters.py b/Tools/clinic/libclinic/converters.py index 014486f7f469def..d0c8f7f427413c1 100644 --- a/Tools/clinic/libclinic/converters.py +++ b/Tools/clinic/libclinic/converters.py @@ -1226,8 +1226,10 @@ class self_converter(CConverter): format_unit = '' specified_type: str | None = None - def converter_init(self, *, type: str | None = None) -> None: + def converter_init(self, *, type: str | None = None, + always_reject_kwargs: bool = False) -> None: self.specified_type = type + self.always_reject_kwargs = always_reject_kwargs def pre_render(self) -> None: f = self.function diff --git a/Tools/clinic/libclinic/parse_args.py b/Tools/clinic/libclinic/parse_args.py index 4608947cb7fff7c..40b09a19f6b1a1f 100644 --- a/Tools/clinic/libclinic/parse_args.py +++ b/Tools/clinic/libclinic/parse_args.py @@ -1484,14 +1484,18 @@ def _new_or_init_parser_body(self) -> None: raise ValueError("Slot methods cannot access their defining class.") if not parses_keywords: - self.declarations = '{base_type_ptr}' + always_reject = self.self_parameter_converter.always_reject_kwargs + # The no-positional check below still exempts subclasses. + if not (always_reject and parses_positional): + self.declarations = '{base_type_ptr}' self.codegen.add_include('pycore_modsupport.h', '_PyArg_NoKeywords()') + type_check = '' if always_reject else '{self_type_check}' fields.insert(0, libclinic.normalize_snippet(""" - if ({self_type_check}!_PyArg_NoKeywords("{name}", kwargs)) {{ + if (%s!_PyArg_NoKeywords("{name}", kwargs)) {{ goto exit; }} - """, indent=4)) + """ % type_check, indent=4)) if not parses_positional: self.codegen.add_include('pycore_modsupport.h', '_PyArg_NoPositional()') @@ -1812,6 +1816,10 @@ def parse_args(self, clang: CLanguage) -> dict[str, str]: else: self.parse_general(clang) + if (self.self_parameter_converter.always_reject_kwargs + and 'METH_KEYWORDS' in self.flags): + fail("always_reject_kwargs is incompatible with keyword parameters") + self.copy_includes() if self.is_new_or_init(): self.handle_new_or_init()