check_new_syntax.py: check nested annotations (#6167)

This commit is contained in:
Akuli
2021-10-13 21:28:38 +00:00
committed by GitHub
parent f30b5ae363
commit 75ca712f3c
6 changed files with 28 additions and 24 deletions

View File

@@ -11,18 +11,18 @@ def check_new_syntax(tree: ast.AST, path: Path) -> list[str]:
class OldSyntaxFinder(ast.NodeVisitor):
def visit_Subscript(self, node: ast.Subscript) -> None:
if not isinstance(node.value, ast.Name):
return
if isinstance(node.value, ast.Name):
if node.value.id == "Union" and isinstance(node.slice, ast.Tuple):
new_syntax = " | ".join(ast.unparse(x) for x in node.slice.elts)
errors.append(f"{path}:{node.lineno}: Use PEP 604 syntax for Union, e.g. `{new_syntax}`")
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)}]"
errors.append(f"{path}:{node.lineno}: Use built-in generics, e.g. `{new_syntax}`")
if node.value.id == "Union" and isinstance(node.slice, ast.Tuple):
new_syntax = " | ".join(ast.unparse(x) for x in node.slice.elts)
errors.append(f"{path}:{node.lineno}: Use PEP 604 syntax for Union, e.g. `{new_syntax}`")
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)}]"
errors.append(f"{path}:{node.lineno}: Use built-in generics, e.g. `{new_syntax}`")
self.generic_visit(node)
# This doesn't check type aliases (or type var bounds, etc), since those are not
# currently supported