8000 Merge pull request #158 from jakkdl/autofix_91x_forealthistime · python-trio/flake8-async@033317a · GitHub
[go: up one dir, main page]

Skip to content

Commit 033317a

Browse files
authored
Merge pull request #158 from jakkdl/autofix_91x_forealthistime
implement autofix for 91x
2 parents dc83998 + 220c405 commit 033317a

19 files changed

+3656
-67
lines changed

CONTRIBUTING.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ To check that all codes are tested and documented there's a test that error code
3232
## Test generator
3333
Tests are automatically generated for files in the `tests/eval_files/` directory, with the code that it's testing interpreted from the file name. The file extension is split off, if there's a match for for `_py\d*` it strips that off and uses it to determine if there's a minimum python version for which the test should only run.
3434

35+
### autofix files
36+
Checks that have autofixing can have a file in the `tests/autofix_files` directory matching the filename in `tests/eval_files`. The result of running the checker on the eval file with autofix enabled will then be compared to the content of the autofix file and will print a diff (if `-s` is on) and assert that the content is the same. `--generate-autofix` is added as a pytest flag to ease development, which will print a diff (with `-s`) and overwrite the content of the autofix file. Also see the magic line marker `pass # AUTOFIX_LINE ` below
37+
3538
### `error:`
3639
Lines containing `error:` are parsed as expecting an error of the code matching the file name, with everything on the line after the colon `eval`'d and passed as arguments to `flake8_trio.Error_codes[<error_code>].str_format`. The `globals` argument to `eval` contains a `lineno` variable assigned the current line number, and the `flake8_trio.Statement` namedtuple. The first element after `error:` *must* be an integer containing the column where the error on that line originates.
3740
#### `TRIOxxx:`

flake8_trio/runner.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313

1414
import libcst as cst
1515

16-
from .visitors import ERROR_CLASSES, ERROR_CLASSES_CST, utility_visitors
16+
from .visitors import (
17+
ERROR_CLASSES,
18+
ERROR_CLASSES_CST,
19+
utility_visitors,
20+
utility_visitors_cst,
21+
)
1722

1823
if TYPE_CHECKING:
1924
from argparse import Namespace
@@ -104,6 +109,13 @@ def __init__(self, options: Namespace, module: Module):
104109
super().__init__()
105110
self.state = SharedState(options)
106111
self.options = options
112+
113+
# Could possibly enable/disable utility visitors here, if visitors declared
114+
# dependencies
115+
self.utility_visitors: tuple[Flake8TrioVisitor_cst, ...] = tuple(
116+
v(self.state) for v in utility_visitors_cst
117+
)
118+
107119
self.visitors: tuple[Flake8TrioVisitor_cst, ...] = tuple(
108120
v(self.state) for v in ERROR_CLASSES_CST if self.selected(v.error_codes)
109121
)
@@ -112,7 +124,7 @@ def __init__(self, options: Namespace, module: Module):
112124
def run(self) -> Iterable[Error]:
113125
if not self.visitors:
114126
return
115-
for v in self.visitors:
127+
for v in (*self.utility_visitors, *self.visitors):
116128
self.module = cst.MetadataWrapper(self.module).visit(v)
117129
yield from self.state.problems
118130

flake8_trio/visitors/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,18 @@
1212
if TYPE_CHECKING:
1313
from .flake8triovisitor import Flake8TrioVisitor, Flake8TrioVisitor_cst
1414

15-
__all__ = ["ERROR_CLASSES", "default_disabled_error_codes", "utility_visitors"]
15+
__all__ = [
16+
"ERROR_CLASSES",
17+
"ERROR_CLASSES_CST",
18+
"default_disabled_error_codes",
19+
"utility_visitors",
20+
"utility_visitors_cst",
21+
]
1622
ERROR_CLASSES: set[type[Flake8TrioVisitor]] = set()
1723
ERROR_CLASSES_CST: set[type[Flake8TrioVisitor_cst]] = set()
18-
utility_visitors: set[type[Flake8TrioVisitor]] = set()
1924
default_disabled_error_codes: list[str] = []
25+
utility_visitors: set[type[Flake8TrioVisitor]] = set()
26+
utility_visitors_cst: set[type[Flake8TrioVisitor_cst]] = set()
2027

2128
# Import all visitors so their decorators run, filling the above containers
2229
# This has to be done at the end to avoid circular imports

flake8_trio/visitors/flake8triovisitor.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,13 @@ def error(
227227
*args,
228228
)
229229
)
230+
231+
@property
232+
def library(self) -> tuple[str, ...]:
233+
return self.__state.library if self.__state.library else ("trio",)
234+
235+
# library_str not used in cst yet
236+
237+
def add_library(self, name: str) -> None:
238+
if name not in self.__state.library:
239+
self.__state.library = self.__state.library + (name,)

flake8_trio/visitors/helpers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
ERROR_CLASSES_CST,
2020
default_disabled_error_codes,
2121
utility_visitors,
22+
utility_visitors_cst,
2223
)
2324

2425
if TYPE_CHECKING:
@@ -58,6 +59,13 @@ def utility_visitor(c: type[T]) -> type[T]:
5859
return c
5960

6061

62+
def utility_visitor_cst(c: type[T_CST]) -> type[T_CST]:
63+
assert not hasattr(c, "error_codes")
64+
c.error_codes = {}
65+
utility_visitors_cst.add(c)
66+
return c
67+
68+
6169
def _get_identifier(node: ast.expr) -> str:
6270
if isinstance(node, ast.Name):
6371
return node.id

0 commit comments

Comments
 (0)
0