tests/check_new_syntax.py: check order of if statements (#6423)

This commit is contained in:
Akuli
2021-11-28 18:04:46 +02:00
committed by GitHub
parent 6d54c10387
commit 2b702233c6
4 changed files with 25 additions and 14 deletions

View File

@@ -117,7 +117,18 @@ def check_new_syntax(tree: ast.AST, path: Path) -> list[str]:
self.old_syntax_finder().visit(node.returns)
self.generic_visit(node)
class IfFinder(ast.NodeVisitor):
def visit_If(self, node: ast.If) -> None:
if isinstance(node.test, ast.Compare) and ast.unparse(node.test).startswith("sys.version_info < ") and node.orelse:
new_syntax = "if " + ast.unparse(node.test).replace("<", ">=", 1)
errors.append(
f"{path}:{node.lineno}: When using if/else with sys.version_info, "
f"put the code for new Python versions first, e.g. `{new_syntax}`"
)
self.generic_visit(node)
AnnotationFinder().visit(tree)
IfFinder().visit(tree)
return errors