Skip to content

Commit

Permalink
gh-104683: clinic.py: Modernise parse_converter() using pattern-mat…
Browse files Browse the repository at this point in the history
…ching (#104696)
  • Loading branch information
AlexWaygood committed May 21, 2023
1 parent cd97484 commit 4b107d8
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions Tools/clinic/clinic.py
Expand Up @@ -5016,22 +5016,26 @@ 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, {}
KwargDict = dict[str | None, Any]

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
@staticmethod
def parse_converter(annotation: ast.expr | None) -> tuple[str, bool, KwargDict]:
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
}
return name, False, kwargs
case _:
fail(
"Annotations must be either a name, a function call, or a string."
)

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

0 comments on commit 4b107d8

Please sign in to comment.