-
Notifications
You must be signed in to change notification settings - Fork 6
/
flake8_pep3101.py
49 lines (38 loc) · 1.41 KB
/
flake8_pep3101.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from flake8 import utils as stdin_utils
import ast
class Flake8Pep3101:
name = 'flake8_pep3101'
version = '1.2.1'
message = 'S001 found modulo formatter'
def __init__(self, tree, filename):
self.filename = filename
self.tree = tree
def run(self):
tree = self.tree
if self.filename == 'stdin':
lines = stdin_utils.stdin_get_value()
tree = ast.parse(lines)
for stmt in ast.walk(tree):
if self._is_module_operation(stmt):
if self._is_left_hand_number(stmt):
continue
if self._is_modulo_variable_and_number(stmt):
continue
if isinstance(stmt.left, (ast.Str, ast.Name)):
yield (
stmt.lineno,
stmt.col_offset,
self.message,
type(self),
)
@staticmethod
def _is_module_operation(stmt):
return isinstance(stmt, ast.BinOp) and isinstance(stmt.op, ast.Mod)
@staticmethod
def _is_left_hand_number(stmt):
"""Check if it is a case of `44 % SOMETHING`."""
return isinstance(stmt.left, ast.Num)
@staticmethod
def _is_modulo_variable_and_number(stmt):
"""Check if it is a case of `var % 44`."""
return isinstance(stmt.right, ast.Num) and isinstance(stmt.left, ast.Name)