From b2082ce5594f3f47630f6602f723afc768e3cc60 Mon Sep 17 00:00:00 2001 From: Akuli Date: Thu, 14 Oct 2021 01:14:05 +0000 Subject: [PATCH] check_new_syntax.py: improve dicts in error message (#6168) Improve error message for bad dict. Before dict[(int, str)] now dict[int, str] --- tests/check_new_syntax.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/check_new_syntax.py b/tests/check_new_syntax.py index 3ad4739d2..eb71bc455 100755 --- a/tests/check_new_syntax.py +++ b/tests/check_new_syntax.py @@ -9,6 +9,11 @@ from pathlib import Path def check_new_syntax(tree: ast.AST, path: Path) -> list[str]: errors = [] + def unparse_without_tuple_parens(node: ast.AST) -> str: + if isinstance(node, ast.Tuple) and node.elts: + return ast.unparse(node)[1:-1] + return ast.unparse(node) + class OldSyntaxFinder(ast.NodeVisitor): def visit_Subscript(self, node: ast.Subscript) -> None: if isinstance(node.value, ast.Name): @@ -18,8 +23,11 @@ def check_new_syntax(tree: ast.AST, path: Path) -> list[str]: if node.value.id == "Optional": new_syntax = f"{ast.unparse(node.slice)} | None" errors.append(f"{path}:{node.lineno}: Use PEP 604 syntax for Optional, e.g. `{new_syntax}`") - if node.value.id in {"List", "Dict"}: - new_syntax = f"{node.value.id.lower()}[{ast.unparse(node.slice)}]" + if node.value.id == "List": + new_syntax = f"list[{ast.unparse(node.slice)}]" + errors.append(f"{path}:{node.lineno}: Use built-in generics, e.g. `{new_syntax}`") + if node.value.id == "Dict": + new_syntax = f"dict[{unparse_without_tuple_parens(node.slice)}]" errors.append(f"{path}:{node.lineno}: Use built-in generics, e.g. `{new_syntax}`") self.generic_visit(node)