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 @@ -2106,6 +2106,28 @@ def test_disallowed_grouping__no_matching_bracket(self):
err = "Function 'empty_group' has a ']' without a matching '['"
self.expect_failure(block, err)

def test_disallowed_grouping__varpos(self):
err = "cannot use optional groups with a var-positional parameter"
block = """
module foo
foo.bar
[
a: int
]
*args: tuple
"""
self.expect_failure(block, err, lineno=5)
block = """
module foo
foo.bar
a: int
*args: tuple
[
b: int
]
"""
self.expect_failure(block, err, lineno=4)

def test_disallowed_grouping__must_be_position_only(self):
dataset = ("""
with_kwds
Expand Down
9 changes: 9 additions & 0 deletions Tools/clinic/libclinic/dsl_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,12 @@ def bad_node(self, node: ast.AST) -> None:

kind: inspect._ParameterKind
if is_vararg:
if any(p.group for p in self.function.parameters.values()):
# With "foo([a, b], *args)" the number of arguments does not
# tell whether the group is passed or all arguments belong
# to the var-positional parameter.
fail(f"Function {self.function.name!r} cannot use optional "
f"groups with a var-positional parameter.")
kind = inspect.Parameter.VAR_POSITIONAL
elif is_var_keyword:
kind = inspect.Parameter.VAR_KEYWORD
Expand Down Expand Up @@ -1303,6 +1309,9 @@ def parse_star(self, function: Function, version: VersionTuple | None) -> None:

def parse_opening_square_bracket(self, function: Function) -> None:
"""Parse opening parameter group symbol '['."""
if any(p.is_vararg() for p in function.parameters.values()):
fail(f"Function {function.name!r} cannot use optional groups "
f"with a var-positional parameter.")
# A group can only be nested in a group which does not contain
# parameters yet, but two groups on the same nesting level can
# follow each other.
Expand Down
79 changes: 79 additions & 0 deletions clinic/test.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading