Skip to content

Commit 4b107d8

Browse files
authored
gh-104683: clinic.py: Modernise parse_converter() using pattern-matching (#104696)
1 parent cd97484 commit 4b107d8

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

Tools/clinic/clinic.py

+19-15
Original file line numberDiff line numberDiff line change
@@ -5016,22 +5016,26 @@ def bad_node(self, node):
50165016
key = f"{parameter_name}_as_{c_name}" if c_name else parameter_name
50175017
self.function.parameters[key] = p
50185018

5019-
def parse_converter(self, annotation):
5020-
if (isinstance(annotation, ast.Constant) and
5021-
type(annotation.value) is str):
5022-
return annotation.value, True, {}
5019+
KwargDict = dict[str | None, Any]
50235020

5024-
if isinstance(annotation, ast.Name):
5025-
return annotation.id, False, {}
5026-
5027-
if not isinstance(annotation, ast.Call):
5028-
fail("Annotations must be either a name, a function call, or a string.")
5029-
5030-
name = annotation.func.id
5031-
symbols = globals()
5032-
5033-
kwargs = {node.arg: eval_ast_expr(node.value, symbols) for node in annotation.keywords}
5034-
return name, False, kwargs
5021+
@staticmethod
5022+
def parse_converter(annotation: ast.expr | None) -> tuple[str, bool, KwargDict]:
5023+
match annotation:
5024+
case ast.Constant(value=str() as value):
5025+
return value, True, {}
5026+
case ast.Name(name):
5027+
return name, False, {}
5028+
case ast.Call(func=ast.Name(name)):
5029+
symbols = globals()
5030+
kwargs = {
5031+
node.arg: eval_ast_expr(node.value, symbols)
5032+
for node in annotation.keywords
5033+
}
5034+
return name, False, kwargs
5035+
case _:
5036+
fail(
5037+
"Annotations must be either a name, a function call, or a string."
5038+
)
50355039

50365040
def parse_special_symbol(self, symbol):
50375041
if symbol == '*':

0 commit comments

Comments
 (0)