-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflake8_timeout.py
74 lines (64 loc) · 2.43 KB
/
flake8_timeout.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import ast
import importlib.metadata as importlib_metadata
from typing import Any
from typing import Generator
from typing import List
from typing import Tuple
from typing import Type
MSG = 'TIM100 request call has no timeout'
METHODS = [
'request', 'get', 'head', 'post',
'patch', 'put', 'delete', 'options',
]
class Visitor(ast.NodeVisitor):
def __init__(self) -> None:
self.assignments: List[Tuple[int, int]] = []
def visit_Call(self, node: ast.Call) -> None:
if (
isinstance(node, ast.Call) and
isinstance(node.func, ast.Attribute) and
isinstance(node.func.value, ast.Name) and
node.func.value.id == 'requests' and
node.func.attr in METHODS
):
for kwarg in node.keywords:
if (
kwarg.arg == 'timeout' and
isinstance(kwarg.value, ast.Constant) and
kwarg.value.value is not None
):
break
else:
self.assignments.append((node.lineno, node.col_offset))
elif (
isinstance(node, ast.Call) and
isinstance(node.func, ast.Attribute) and
isinstance(node.func.value, ast.Attribute) and
isinstance(node.func.value.value, ast.Name) and
node.func.value.value.id == 'urllib' and
node.func.value.attr == 'request' and
node.func.attr == 'urlopen'
):
for kwarg in node.keywords:
if (
kwarg.arg == 'timeout' and
isinstance(kwarg.value, ast.Constant) and
kwarg.value.value is not None
):
break
else:
# check if it was passed as a positional argument instead
# args are: (url, data=None, [timeout, ]*, cafile=None ...
if len(node.args) < 3:
self.assignments.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):
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.assignments:
yield line, col, MSG, type(self)