-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake8_printf_formatting.py
More file actions
35 lines (25 loc) · 1.04 KB
/
flake8_printf_formatting.py
File metadata and controls
35 lines (25 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import ast
import sys
from typing import Any, Generator, List, Tuple, Type
if sys.version_info < (3, 8): # pragma: no cover (<PY38)
import importlib_metadata
else: # pragma: no cover (PY38+)
import importlib.metadata as importlib_metadata
MOD001 = "MOD001 do not use printf-style string formatting"
class Visitor(ast.NodeVisitor):
def __init__(self) -> None:
self.expressions: List[Tuple[int, int]] = []
def visit_BinOp(self, node: ast.BinOp) -> None:
if isinstance(node.op, ast.Mod) and isinstance(node.left, (ast.Str, ast.Bytes)):
self.expressions.append((node.lineno, node.col_offset))
self.generic_visit(node)
class Plugin:
name = __name__
version = importlib_metadata.version(__name__)
def __init__(self, tree: ast.AST) -> None:
self._tree = tree
def run(self) -> Generator[Tuple[int, int, str, Type[Any]], None, None]:
visitor = Visitor()
visitor.visit(self._tree)
for line, col in visitor.expressions:
yield line, col, MOD001, type(self)