Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-106359: Fix corner case bugs in Argument Clinic converter parser #106361

Merged
merged 14 commits into from
Jul 3, 2023
Merged
14 changes: 14 additions & 0 deletions Lib/test/test_clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,20 @@ def test_other_bizarre_things_in_annotations_fail(self):
)
self.assertEqual(s, expected_failure_message)

def test_bizarre_parseable_annotations(self):
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved
expected_error_msg = (
"Error on line 0\n"
"Cannot use a kwarg splat in a function-call annotation"
)
dataset = (
'module fo\nfo.barbaz\n o: bool(**{None: "bang!"})',
'module fo\nfo.barbaz -> bool(**{None: "bang!"})',
)
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved
for fn in dataset:
with self.subTest(fn=fn):
out = self.parse_function_should_fail(fn)
self.assertEqual(out, expected_error_msg)

def test_unused_param(self):
block = self.parse("""
module foo
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Argument Clinic now explicitly require that keys are string if the provided
parameter annotation is a dict.
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved
16 changes: 9 additions & 7 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4291,6 +4291,7 @@ def dedent(self, line):


StateKeeper = Callable[[str | None], None]
ConverterArgs = dict[str, Any]

class DSLParser:
function: Function | None
Expand Down Expand Up @@ -5033,21 +5034,22 @@ def bad_node(self, node):
key = f"{parameter_name}_as_{c_name}" if c_name else parameter_name
self.function.parameters[key] = p

KwargDict = dict[str | None, Any]

@staticmethod
def parse_converter(annotation: ast.expr | None) -> tuple[str, bool, KwargDict]:
def parse_converter(
annotation: ast.expr | None
) -> tuple[str, bool, ConverterArgs]:
match annotation:
case ast.Constant(value=str() as value):
return value, True, {}
case ast.Name(name):
return name, False, {}
case ast.Call(func=ast.Name(name)):
symbols = globals()
kwargs = {
node.arg: eval_ast_expr(node.value, symbols)
for node in annotation.keywords
}
kwargs: ConverterArgs = {}
for node in annotation.keywords:
if not isinstance(node.arg, str):
fail("Cannot use a kwarg splat in a function-call annotation")
kwargs[node.arg] = eval_ast_expr(node.value, symbols)
return name, False, kwargs
case _:
fail(
Expand Down