-
Notifications
You must be signed in to change notification settings - Fork 25
/
_rules.py
217 lines (186 loc) 路 6.18 KB
/
_rules.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# built-in
import ast
import enum
from types import MappingProxyType
from typing import Iterator
# app
from .._decorators import Has
from ._contract import Category, Contract
from ._error import Error
from ._extractors import (
get_asserts, get_exceptions, get_imports, get_markers,
get_pre, get_returns, get_value, has_returns,
)
from ._func import Func
from ._stub import StubsManager
rules = []
class Required(enum.Enum):
FUNC = 'func'
MODULE = 'module'
def register(rule):
rules.append(rule())
return rule
@register
class CheckImports:
__slots__ = ()
code = 1
message = 'do not use `from deal import ...`, use `import deal` instead'
required = Required.MODULE
def __call__(self, tree: ast.Module) -> Iterator[Error]:
for token in get_imports(tree.body):
if token.value != 'deal':
continue
yield Error(
code=self.code,
text=self.message,
row=token.line,
col=token.col,
)
@register
class CheckPre:
__slots__ = ()
code = 11
message = 'pre contract error'
required = Required.FUNC
def __call__(self, func: Func, stubs: StubsManager = None) -> Iterator[Error]:
# We test only contracted functions because of poor performance.
# Inferring every called function in the whole project
# is a really expensive operation.
if not func.contracts:
return
for token in get_pre(body=func.body):
yield Error(
code=self.code,
text=self.message,
value=token.value, # type: ignore
row=token.line,
col=token.col,
)
@register
class CheckReturns:
__slots__ = ()
code = 12
message = 'post contract error'
required = Required.FUNC
def __call__(self, func: Func, stubs: StubsManager = None) -> Iterator[Error]:
for contract in func.contracts:
if contract.category != Category.POST:
continue
yield from self._check(func=func, contract=contract)
def _check(self, func: Func, contract: Contract) -> Iterator[Error]:
for token in get_returns(body=func.body):
try:
result = contract.run(token.value)
except NameError:
# cannot resolve contract dependencies
return
error_info = dict(
row=token.line,
col=token.col,
code=self.code,
value=str(token.value),
)
if isinstance(result, str):
yield Error(text=result, **error_info) # type: ignore
continue
if not result:
yield Error(text=self.message, **error_info) # type: ignore
@register
class CheckRaises:
__slots__ = ()
code = 21
message = 'raises contract error'
required = Required.FUNC
def __call__(self, func: Func, stubs: StubsManager = None) -> Iterator[Error]:
for contract in func.contracts:
if contract.category != Category.RAISES:
continue
yield from self._check(func=func, contract=contract, stubs=stubs)
def _check(self, func: Func, contract: Contract, stubs: StubsManager = None) -> Iterator[Error]:
allowed = contract.exceptions
allowed_types = tuple(exc for exc in allowed if type(exc) is not str)
for token in get_exceptions(body=func.body, stubs=stubs):
if token.value in allowed:
continue
exc = token.value
if isinstance(exc, type):
if issubclass(exc, allowed_types):
continue
exc = exc.__name__
yield Error(
code=self.code,
text=self.message,
value=str(exc),
row=token.line,
col=token.col,
)
@register
class CheckAsserts:
__slots__ = ()
code = 31
message = 'assert error'
required = Required.FUNC
def __call__(self, func: Func, stubs: StubsManager = None) -> Iterator[Error]:
# do not validate asserts in tests
if func.name.startswith('test_'):
return
for token in get_asserts(body=func.body):
yield Error(
code=self.code,
text=self.message,
value=str(token.value),
row=token.line,
col=token.col,
)
@register
class CheckMarkers:
__slots__ = ()
code = 40
message = 'missed marker'
required = Required.FUNC
codes = MappingProxyType({
'global': 41,
'import': 42,
'io': 43,
'read': 44,
'write': 45,
'stdout': 46,
'stderr': 47,
'network': 48,
})
def __call__(self, func: Func, stubs: StubsManager = None) -> Iterator[Error]:
for contract in func.contracts:
markers = None
if contract.category == Category.HAS:
markers = [get_value(arg) for arg in contract.args]
elif contract.category == Category.PURE:
markers = []
if markers is None:
continue
yield from self._check(func=func, has=Has(*markers))
return
@classmethod
def _check(cls, func: Func, has: Has) -> Iterator[Error]:
# function without IO must return something
if not has.has_io and not has_returns(body=func.body):
yield Error(
code=cls.codes['io'],
text=cls.message,
value='io',
row=func.line,
col=func.col,
)
for token in get_markers(body=func.body):
assert token.marker
has_marker = getattr(has, 'has_{}'.format(token.marker), None)
if has_marker is None:
has_marker = token.marker in has.markers
if has_marker:
continue
yield Error(
code=cls.codes.get(token.marker, 40),
text=cls.message,
value=token.marker,
row=token.line,
col=token.col,
)