Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Lib/test/test_clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1347,6 +1347,28 @@ def test_unused_param(self):
parser_decl = p.simple_declaration(in_parser=True)
self.assertNotIn("Py_UNUSED", parser_decl)

def test_kind_defining_class(self):
function = self.parse_function("""
module m
class m.C "PyObject *" ""
m.C.meth
cls: defining_class
""", signatures_in_block=3, function_index=2)
p = function.parameters['cls']
self.assertEqual(p.kind, inspect.Parameter.POSITIONAL_ONLY)

def test_disallow_defining_class_at_module_level(self):
expected_error_msg = (
"Error on line 0:\n"
"A 'defining_class' parameter cannot be defined at module level.\n"
)
out = self.parse_function_should_fail("""
module m
m.func
cls: defining_class
""")
self.assertEqual(out, expected_error_msg)

def parse(self, text):
c = FakeClinic()
parser = DSLParser(c)
Expand Down
8 changes: 7 additions & 1 deletion Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4997,6 +4997,9 @@ def bad_node(self, node):
fail("A 'defining_class' parameter cannot have a default value.")
if self.group:
fail("A 'defining_class' parameter cannot be in an optional group.")
if self.function.cls is None:
fail("A 'defining_class' parameter cannot be defined at module level.")
kind = inspect.Parameter.POSITIONAL_ONLY
else:
fail("A 'defining_class' parameter, if specified, must either be the first thing in the parameter block, or come just after 'self'.")

Expand Down Expand Up @@ -5074,7 +5077,10 @@ def parse_special_symbol(self, symbol):
for p in self.function.parameters.values():
if p.is_vararg():
continue
if (p.kind != inspect.Parameter.POSITIONAL_OR_KEYWORD and not isinstance(p.converter, self_converter)):
if (p.kind != inspect.Parameter.POSITIONAL_OR_KEYWORD and
not isinstance(p.converter, self_converter) and
not isinstance(p.converter, defining_class_converter)
):
fail("Function " + self.function.name + " mixes keyword-only and positional-only parameters, which is unsupported.")
p.kind = inspect.Parameter.POSITIONAL_ONLY

Expand Down