-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathflake8_pep3101.py
More file actions
53 lines (40 loc) · 1.61 KB
/
flake8_pep3101.py
File metadata and controls
53 lines (40 loc) · 1.61 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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.Name) or self.is_text(stmt.left):
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)
def _is_left_hand_number(self, stmt):
"""Check if it is a case of `44 % SOMETHING`."""
return self.is_number(stmt.left)
def _is_modulo_variable_and_number(self, stmt):
"""Check if it is a case of `var % 44`."""
return self.is_number(stmt.right) and isinstance(stmt.left, ast.Name)
def is_number(self, stmt):
return isinstance(stmt, ast.Constant) and isinstance(stmt.value, int)
def is_text(self, stmt):
return isinstance(stmt, ast.Constant) and isinstance(stmt.value, str)