8000 gh-104683: Argument clinic: cleanup `state_modulename_name()` (#107340) · python/cpython@c2b1689 · GitHub
[go: up one dir, main page]

Skip to content

Commit c2b1689

Browse files
authored
gh-104683: Argument clinic: cleanup state_modulename_name() (#107340)
1 parent 2f9bb77 commit c2b1689

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

Lib/test/test_clinic.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,32 @@ def test_return_converter(self):
679679
""")
680680
self.assertIsInstance(function.return_converter, clinic.int_return_converter)
681681

682+
def test_return_converter_invalid_syntax(self):
683+
stdout = self.parse_function_should_fail("""
684+
module os
685+
os.stat -> invalid syntax
686+
""")
687+
expected_error = "Badly formed annotation for os.stat: 'invalid syntax'"
688+
self.assertIn(expected_error, stdout)
689+
690+
def test_legacy_converter_disallowed_in_return_annotation(self):
691+
stdout = self.parse_function_should_fail("""
692+
module os
693+
os.stat -> "s"
694+
""")
695+
expected_error = "Legacy converter 's' not allowed as a return converter"
696+
self.assertIn(expected_error, stdout)
697+
698+
def test_unknown_return_converter(self):
699+
stdout = self.parse_function_should_fail("""
700+
module os
701+
os.stat -> foooooooooooooooooooooooo
702+
""")
703+
expected_error = (
704+
"No available return converter called 'foooooooooooooooooooooooo'"
705+
)
706+
self.assertIn(expected_error, stdout)
707+
682708
def test_star(self):
683709
function = self.parse_function("""
684710
module os

Tools/clinic/clinic.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4730,6 +4730,7 @@ def state_modulename_name(self, line: str | None) -> None:
47304730
return
47314731

47324732
line, _, returns = line.partition('->')
4733+
returns = returns.strip()
47334734

47344735
full_name, _, c_basename = line.partition(' as ')
47354736
full_name = full_name.strip()
@@ -4743,23 +4744,21 @@ def state_modulename_name(self, line: str | None) -> None:
47434744
return_converter = None
47444745
if returns:
47454746
ast_input = f"def x() -> {returns}: pass"
4746-
module = None
47474747
try:
4748-
module = ast.parse(ast_input)
4748+
module_node = ast.parse(ast_input)
47494749
except SyntaxError:
4750-
pass
4751-
if not module:
4752-
fail("Badly-formed annotation for " + full_name + ": " + returns)
4750+
fail(f"Badly formed annotation for {full_name}: {returns!r}")
4751+
function_node = module_node.body[0]
4752+
assert isinstance(function_node, ast.FunctionDef)
47534753
try:
4754-
name, legacy, kwargs = self.parse_converter(module.body[0].returns)
4754+
name, legacy, kwargs = self.parse_converter(function_node.returns)
47554755
if legacy:
4756-
fail("Legacy converter {!r} not allowed as a return converter"
4757-
.format(name))
4756+
fail(f"Legacy converter {name!r} not allowed as a return converter")
47584757
if name not in return_converters:
4759-
fail("No available return converter called " + repr(name))
4758+
fail(f"No available return converter called {name!r}")
47604759
return_converter = return_converters[name](**kwargs)
47614760
except ValueError:
4762-
fail("Badly-formed annotation for " + full_name + ": " + returns)
4761+
fail(f"Badly formed annotation for {full_name}: {returns!r}")
47634762

47644763
fields = [x.strip() for x in full_name.split('.')]
47654764
function_name = fields.pop()

0 commit comments

Comments
 (0)
0