Skip to content

Commit c21569b

Browse files
cmaloneyclaude
andcommitted
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 <noreply@anthropic.com>
1 parent d88a6b9 commit c21569b

4 files changed

Lines changed: 29 additions & 9 deletions

File tree

‎Lib/test/clinic.test.c‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5175,6 +5175,7 @@ Test_an_metho_arg_named_arg_impl(TestObj *self, int arg)
51755175

51765176
/*[clinic input]
51775177
Test.__init__
5178+
self: self(type="TestObj *", always_reject_kwargs=True)
51785179
*args: tuple
51795180
51805181
Varargs init method.
@@ -5197,12 +5198,9 @@ static int
51975198
Test___init__(PyObject *self, PyObject *args, PyObject *kwargs)
51985199
{
51995200
int return_value = -1;
5200-
PyTypeObject *base_tp = TestType;
52015201
PyObject *__clinic_args = NULL;
52025202

5203-
if ((Py_IS_TYPE(self, base_tp) ||
5204-
Py_TYPE(self)->tp_new == base_tp->tp_new) &&
5205-
!_PyArg_NoKeywords("Test", kwargs)) {
5203+
if (!_PyArg_NoKeywords("Test", kwargs)) {
52065204
goto exit;
52075205
}
52085206
__clinic_args = Py_NewRef(args);
@@ -5217,7 +5215,7 @@ Test___init__(PyObject *self, PyObject *args, PyObject *kwargs)
52175215

52185216
static int
52195217
Test___init___impl(TestObj *self, PyObject *args)
5220-
/*[clinic end generated code: output=0e5836c40dbc2397 input=a615a4485c0fc3e2]*/
5218+
/*[clinic end generated code: output=d912a2c5da6467d9 input=8e0d779137ce4f66]*/
52215219

52225220
/*[clinic input]
52235221
@vectorcall

‎Lib/test/test_clinic.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ def test_mangled_marker_line(self):
132132
)
133133
self.expect_failure(raw, err, filename="test.c", lineno=3)
134134

135+
def test_always_reject_kwargs_with_keyword_parameter(self):
136+
raw = """
137+
/*[clinic input]
138+
class Foo "FooObject *" "Foo_Type"
139+
Foo.__init__
140+
self: self(always_reject_kwargs=True)
141+
a: object
142+
[clinic start generated code]*/
143+
"""
144+
err = "always_reject_kwargs is incompatible with keyword parameters"
145+
self.expect_failure(raw, err)
146+
135147
def test_checksum_mismatch(self):
136148
raw = """
137149
/*[clinic input]

‎Tools/clinic/libclinic/converters.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1226,8 +1226,10 @@ class self_converter(CConverter):
12261226
format_unit = ''
12271227
specified_type: str | None = None
12281228

1229-
def converter_init(self, *, type: str | None = None) -> None:
1229+
def converter_init(self, *, type: str | None = None,
1230+
always_reject_kwargs: bool = False) -> None:
12301231
self.specified_type = type
1232+
self.always_reject_kwargs = always_reject_kwargs
12311233

12321234
def pre_render(self) -> None:
12331235
f = self.function

‎Tools/clinic/libclinic/parse_args.py‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,14 +1484,18 @@ def _new_or_init_parser_body(self) -> None:
14841484
raise ValueError("Slot methods cannot access their defining class.")
14851485

14861486
if not parses_keywords:
1487-
self.declarations = '{base_type_ptr}'
1487+
always_reject = self.self_parameter_converter.always_reject_kwargs
1488+
# The no-positional check below still exempts subclasses.
1489+
if not (always_reject and parses_positional):
1490+
self.declarations = '{base_type_ptr}'
14881491
self.codegen.add_include('pycore_modsupport.h',
14891492
'_PyArg_NoKeywords()')
1493+
type_check = '' if always_reject else '{self_type_check}'
14901494
fields.insert(0, libclinic.normalize_snippet("""
1491-
if ({self_type_check}!_PyArg_NoKeywords("{name}", kwargs)) {{
1495+
if (%s!_PyArg_NoKeywords("{name}", kwargs)) {{
14921496
goto exit;
14931497
}}
1494-
""", indent=4))
1498+
""" % type_check, indent=4))
14951499
if not parses_positional:
14961500
self.codegen.add_include('pycore_modsupport.h',
14971501
'_PyArg_NoPositional()')
@@ -1812,6 +1816,10 @@ def parse_args(self, clang: CLanguage) -> dict[str, str]:
18121816
else:
18131817
self.parse_general(clang)
18141818

1819+
if (self.self_parameter_converter.always_reject_kwargs
1820+
and 'METH_KEYWORDS' in self.flags):
1821+
fail("always_reject_kwargs is incompatible with keyword parameters")
1822+
18151823
self.copy_includes()
18161824
if self.is_new_or_init():
18171825
self.handle_new_or_init()

0 commit comments

Comments
 (0)