Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Address review: try with a slightly more accurate error message
  • Loading branch information
erlend-aasland committed Jul 3, 2023
commit 288d8ca4f47e1e0e8a108413e54687230d78b115
27 changes: 21 additions & 6 deletions Lib/test/test_clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,18 +797,33 @@ def test_other_bizarre_things_in_annotations_fail(self):
Error on line 0:
Annotations must be either a name, a function call, or a string.
"""

s = self.parse_function_should_fail(
'module os\nos.access\n path: {"some": "dictionary"}'
)
self.assertEqual(s, expected_failure_message)

s = self.parse_function_should_fail(
'module os\nos.access\n path: ["list", "of", "strings"]'
)
self.assertEqual(s, expected_failure_message)

s = self.parse_function_should_fail(
'module os\nos.access\n path: (x for x in range(42))'
)
self.assertEqual(s, expected_failure_message)

def test_bizarre_parseable_annotations(self):
Comment thread
erlend-aasland marked this conversation as resolved.
Outdated
msg = "Annotation dicts must have str keys"
dataset = (
'module os\nos.access\n path: {"some": "dictionary"}',
'module os\nos.access\n path: ["list", "of", "strings"]',
'module os\nos.access\n path: (x for x in range(42))',
'module fo\nfo.barbaz\n o: bool(**{None: "bang!"})',
'module fo\nfo.barbaz -> bool(**{None: "bang!"})',
)
Comment thread
erlend-aasland marked this conversation as resolved.

for fn in dataset:
with self.subTest(fn=fn):
actual = self.parse_function_should_fail(fn)
self.assertEqual(actual, expected_failure_message)
out = self.parse_function_should_fail(fn)
self.assertTrue(out.startswith("Error on line 0"))
self.assertIn(msg, out)

def test_unused_param(self):
block = self.parse("""
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Fix bizarre bugs.
Argument Clinic now explicitly require that keys are string if the provided
parameter annotation is a dict.
Comment thread
erlend-aasland marked this conversation as resolved.
Outdated
7 changes: 4 additions & 3 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5038,7 +5038,6 @@ def bad_node(self, node):
def parse_converter(
annotation: ast.expr | None
) -> tuple[str, bool, ConverterArgs]:
msg = "Annotations must be either a name, a function call, or a string."
match annotation:
case ast.Constant(value=str() as value):
return value, True, {}
Expand All @@ -5049,11 +5048,13 @@ def parse_converter(
kwargs: ConverterArgs = {}
for node in annotation.keywords:
if not isinstance(node.arg, str):
fail(msg)
fail("Annotation dicts must have str keys")
kwargs[node.arg] = eval_ast_expr(node.value, symbols)
return name, False, kwargs
case _:
fail(msg)
fail(
"Annotations must be either a name, a function call, or a string."
)

def parse_special_symbol(self, symbol):
if symbol == '*':
Expand Down