summaryrefslogtreecommitdiffstats
path: root/Tools
diff options
context:
space:
mode:
authorAlex Waygood <Alex.Waygood@Gmail.com>2023-05-21 23:29:43 (GMT)
committerGitHub <noreply@github.com>2023-05-21 23:29:43 (GMT)
commit4b107d86f38f6778562d4fe5e1d881b52c9d9d6c (patch)
treed6e68f2ef3294b4674da15b3ccee6ba3035575e0 /Tools
parentcd9748409aa877d6d9905730bf68f25cf7a6a723 (diff)
downloadcpython-4b107d86f38f6778562d4fe5e1d881b52c9d9d6c.zip
cpython-4b107d86f38f6778562d4fe5e1d881b52c9d9d6c.tar.gz
cpython-4b107d86f38f6778562d4fe5e1d881b52c9d9d6c.tar.bz2
gh-104683: clinic.py: Modernise `parse_converter()` using pattern-matching (#104696)
Diffstat (limited to 'Tools')
-rwxr-xr-xTools/clinic/clinic.py34
1 files changed, 19 insertions, 15 deletions
diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py
index 1d7b778..d182e5e 100755
--- a/Tools/clinic/clinic.py
+++ b/Tools/clinic/clinic.py
@@ -5016,22 +5016,26 @@ class DSLParser:
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 == '*':