From af97a12a872f26def7fb415a8c74951657db8c50 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Mon, 4 Sep 2023 23:37:50 +0100 Subject: [PATCH] Add a lint forbidding PEP-570 syntax (#10660) --- tests/check_new_syntax.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/check_new_syntax.py b/tests/check_new_syntax.py index 84a4c8c3d5e8..2c177597c693 100755 --- a/tests/check_new_syntax.py +++ b/tests/check_new_syntax.py @@ -26,7 +26,29 @@ def visit_If(self, node: ast.If) -> None: ) self.generic_visit(node) + class PEP570Finder(ast.NodeVisitor): + def __init__(self) -> None: + self.lineno: int | None = None + + def _visit_function(self, node: ast.FunctionDef | ast.AsyncFunctionDef) -> None: + old_lineno = self.lineno + self.lineno = node.lineno + self.generic_visit(node) + self.lineno = old_lineno + + visit_FunctionDef = visit_AsyncFunctionDef = _visit_function + + def visit_arguments(self, node: ast.arguments) -> None: + if node.posonlyargs: + assert isinstance(self.lineno, int) + errors.append( + f"{path}:{self.lineno}: PEP-570 syntax cannot be used in typeshed yet. " + f"Prefix parameter names with `__` to indicate positional-only parameters" + ) + self.generic_visit(node) + IfFinder().visit(tree) + PEP570Finder().visit(tree) return errors