Skip to content
Merged
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
Next Next commit
clinic.py: Modernise parse_converter() using pattern-matching
  • Loading branch information
AlexWaygood committed May 20, 2023
commit 763ce08e6bd160ee2a1ebebecaa5a05ff50a5502
37 changes: 21 additions & 16 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4906,22 +4906,27 @@ def bad_node(self, node):
key = f"{parameter_name}_as_{c_name}" if c_name else parameter_name
self.function.parameters[key] = p

def parse_converter(self, annotation):
if (isinstance(annotation, ast.Constant) and
type(annotation.value) is str):
return annotation.value, True, {}

if isinstance(annotation, ast.Name):
return annotation.id, False, {}

if not isinstance(annotation, ast.Call):
fail("Annotations must be either a name, a function call, or a string.")

name = annotation.func.id
symbols = globals()

kwargs = {node.arg: eval_ast_expr(node.value, symbols) for node in annotation.keywords}
return name, False, kwargs
KwargDict = dict[str | None, Any]

def parse_converter(
self, annotation: ast.expr | None
Comment thread
AlexWaygood marked this conversation as resolved.
Outdated
) -> tuple[str, bool, KwargDict]:
match annotation:
case ast.Constant(value=str() as value):
return value, True, {}
Comment thread
AlexWaygood marked this conversation as resolved.
case ast.Name(name):
return name, False, {}
Comment thread
erlend-aasland marked this conversation as resolved.
case ast.Call(func=ast.Name(name)):
symbols = globals()
kwargs = {
node.arg: eval_ast_expr(node.value, symbols)
for node in annotation.keywords
}
return name, False, kwargs
Comment thread
erlend-aasland marked this conversation as resolved.
case _:
fail(
"Annotations must be either a name, a function call, or a string."
)
Comment thread
AlexWaygood marked this conversation as resolved.

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