-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathflake8asyncvisitor.py
More file actions
262 lines (216 loc) · 8.9 KB
/
flake8asyncvisitor.py
File metadata and controls
262 lines (216 loc) · 8.9 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
"""Contains the base class that all error classes inherit from."""
from __future__ import annotations
import ast
from abc import ABC
from typing import TYPE_CHECKING, Any, Union
import libcst as cst
from libcst.metadata import PositionProvider
from ..base import Error, Statement, strip_error_subidentifier
if TYPE_CHECKING:
from collections.abc import Iterable, Mapping
from ..runner import SharedState
HasLineCol = Union[ast.expr, ast.stmt, ast.arg, ast.excepthandler, Statement]
class Flake8AsyncVisitor(ast.NodeVisitor, ABC):
# abstract attribute by not providing a value
error_codes: Mapping[str, str]
def __init__(self, shared_state: SharedState):
super().__init__()
self.outer: dict[ast.AST, dict[str, Any]] = {}
self.novisit = False
self.__state = shared_state
self.options = self.__state.options
self.typed_calls = self.__state.typed_calls
# mark variables that shouldn't be saved/loaded in self.get_state
self.nocopy = {
"_Flake8AsyncVisitor__state",
"error_codes",
"nocopy",
"novisit",
"options",
"outer",
"typed_calls",
}
# `variables` can be saved/loaded, but need a setter to not clear the reference
@property
def variables(self) -> dict[str, str]:
return self.__state.variables
@variables.setter
def variables(self, value: dict[str, str]) -> None:
self.__state.variables.clear()
self.__state.variables.update(value)
def visit(self, node: ast.AST):
"""Visit a node."""
# construct visitor for this node type
visitor = getattr(self, "visit_" + node.__class__.__name__, None)
# if we have a visitor for it, visit it
# it will set self.novisit if it manually visits children
self.novisit = False
if visitor is not None:
visitor(node)
# if it doesn't set novisit, let the regular NodeVisitor iterate through
# subfields
if not self.novisit:
super().generic_visit(node)
# if an outer state was saved in this node restore it after visiting children
self.set_state(self.outer.pop(node, {}))
# set novisit so external runner doesn't visit this node with this class
self.novisit = True
def visit_nodes(self, *nodes: ast.AST | Iterable[ast.AST]):
for arg in nodes:
if isinstance(arg, ast.AST):
self.visit(arg)
else:
for node in arg:
self.visit(node)
def error(
self,
node: HasLineCol,
*args: str | Statement | int,
error_code: str | None = None,
):
if error_code is None:
assert (
len(self.error_codes) == 1
), "No error code defined, but class has multiple codes"
error_code = next(iter(self.error_codes))
# don't emit an error if this code is disabled in a multi-code visitor
elif (
(ec_no_sub := strip_error_subidentifier(error_code))
not in self.options.enabled_codes
and ec_no_sub not in self.options.autofix_codes
):
return
self.__state.problems.append(
Error(
strip_error_subidentifier(error_code),
node.lineno,
node.col_offset,
self.error_codes[error_code],
*args,
)
)
def get_state(self, *attrs: str, copy: bool = False) -> dict[str, Any]:
if not attrs:
# get all attributes, unless they're marked as nocopy
attrs = tuple(set(self.__dict__.keys()) - self.nocopy)
res: dict[str, Any] = {}
for attr in attrs:
value = getattr(self, attr)
if copy and hasattr(value, "copy"):
value = value.copy()
res[attr] = value
return res
def set_state(self, attrs: dict[str, Any], copy: bool = False):
for attr, value in attrs.items():
setattr(self, attr, value)
def save_state(self, node: ast.AST, *attrs: str, copy: bool = False):
state = self.get_state(*attrs, copy=copy)
if node in self.outer:
# not currently used, and not gonna bother adding dedicated test
# visitors atm
self.outer[node].update(state) # pragma: no cover
else:
self.outer[node] = state
def walk(self, *body: ast.AST) -> Iterable[ast.AST]:
for b in body:
yield from ast.walk(b)
@property
def library(self) -> tuple[str, ...]:
return self.__state.library if self.__state.library else ("trio",)
@property
def library_str(self) -> str:
if len(self.library) == 1:
return self.library[0]
return "[" + "/".join(self.library) + "]"
def add_library(self, name: str) -> None:
if name not in self.__state.library:
self.__state.library = (*self.__state.library, name)
class Flake8AsyncVisitor_cst(cst.CSTTransformer, ABC):
# abstract attribute by not providing a value
error_codes: Mapping[str, str]
METADATA_DEPENDENCIES = (PositionProvider,)
def __init__(self, shared_state: SharedState):
super().__init__()
self.outer: dict[cst.CSTNode, dict[str, Any]] = {}
self.__state = shared_state
self.options = self.__state.options
self.noqas = self.__state.noqas
def get_state(self, *attrs: str, copy: bool = False) -> dict[str, Any]:
# require attrs, since we inherit a *ton* of stuff which we don't want to copy
assert attrs
res: dict[str, Any] = {}
for attr in attrs:
value = getattr(self, attr)
if copy and hasattr(value, "copy"):
value = value.copy()
res[attr] = value
return res
def set_state(self, attrs: dict[str, Any], copy: bool = False):
for attr, value in attrs.items():
if copy and hasattr(value, "copy"): # pragma: no cover
# not used by visitors yet
value = value.copy()
setattr(self, attr, value)
def save_state(self, node: cst.CSTNode, *attrs: str, copy: bool = False):
state = self.get_state(*attrs, copy=copy)
if node in self.outer:
self.outer[node].update(state)
else:
self.outer[node] = state
def restore_state(self, node: cst.CSTNode):
self.set_state(self.outer.pop(node, {}))
def is_noqa(self, node: cst.CSTNode, code: str):
if self.options.disable_noqa:
return False
# pyright + get_metadata is acting up
pos = self.get_metadata(PositionProvider, node).start # type: ignore
noqas = self.noqas.get(pos.line) # type: ignore
return noqas is not None and (noqas == set() or code in noqas)
def error(
self,
node: cst.CSTNode,
*args: str | Statement | int,
error_code: str | None = None,
) -> bool:
if error_code is None:
assert (
len(self.error_codes) == 1
), "No error code defined, but class has multiple codes"
error_code = next(iter(self.error_codes))
# don't emit an error if this code is disabled in a multi-code visitor
# TODO: write test for only one of 910/911 enabled/autofixed
elif (
(ec_no_sub := strip_error_subidentifier(error_code))
not in self.options.enabled_codes
and ec_no_sub not in self.options.autofix_codes
):
return False # pragma: no cover
if self.is_noqa(node, error_code):
return False
# pyright + get_metadata is acting up
pos = self.get_metadata(PositionProvider, node).start # type: ignore
self.__state.problems.append(
Error(
strip_error_subidentifier(error_code),
pos.line, # type: ignore
pos.column, # type: ignore
self.error_codes[error_code],
*args,
)
)
return True
def should_autofix(self, node: cst.CSTNode, code: str | None = None) -> bool:
if code is None: # pragma: no cover
assert len(self.error_codes) == 1
code = next(iter(self.error_codes))
# this does not currently need to check for `noqa`s, as error() does that
# and no codes tries to autofix if there's no error. This might change if/when
# "printing an error" and "autofixing" becomes independent. See issue #192
return code in self.options.autofix_codes
@property
def library(self) -> tuple[str, ...]:
return self.__state.library if self.__state.library else ("trio",)
# library_str not used in cst yet
def add_library(self, name: str) -> None:
if name not in self.__state.library:
self.__state.library = (*self.__state.library, name)