Use lowercase tuple where possible (#6170)

This commit is contained in:
Akuli
2021-10-15 00:18:19 +00:00
committed by GitHub
parent 5f386b0575
commit 994b69ef8f
242 changed files with 1212 additions and 1224 deletions

View File

@@ -14,6 +14,9 @@ def check_new_syntax(tree: ast.AST, path: Path) -> list[str]:
return ast.unparse(node)[1:-1]
return ast.unparse(node)
def is_dotdotdot(node: ast.AST) -> bool:
return isinstance(node, ast.Constant) and node.s is Ellipsis
class OldSyntaxFinder(ast.NodeVisitor):
def visit_Subscript(self, node: ast.Subscript) -> None:
if isinstance(node.value, ast.Name):
@@ -29,6 +32,14 @@ def check_new_syntax(tree: ast.AST, path: Path) -> list[str]:
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}`")
# Tuple[Foo, ...] must be allowed because of mypy bugs
if node.value.id == "Tuple" and not (
isinstance(node.slice, ast.Tuple)
and len(node.slice.elts) == 2
and is_dotdotdot(node.slice.elts[1])
):
new_syntax = f"tuple[{unparse_without_tuple_parens(node.slice)}]"
errors.append(f"{path}:{node.lineno}: Use built-in generics, e.g. `{new_syntax}`")
self.generic_visit(node)