Skip to content

Commit

Permalink
Add a lint forbidding PEP-570 syntax (python#10660)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood authored Sep 4, 2023
1 parent 433204f commit af97a12
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tests/check_new_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit af97a12

Please sign in to comment.