8000 Merge pull request #123 from jakkdl/rename_107_108 · python-trio/flake8-async@c1a54eb · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit c1a54eb

Browse files
authored
Merge pull request #123 from jakkdl/rename_107_108
Rename 107/108 to 910/911, and make them optional
2 parents c531a3b + 7f5dc1e commit c1a54eb

File tree

12 files changed

+44
-29
lines changed

12 files changed

+44
-29
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22
*[CalVer, YY.month.patch](https://calver.org/)*
3+
4+
## Future
5+
- Rename TRIO107 to TRIO910, and TRIO108 to TRIO911, and making them optional by default.
6+
-
37
## 23.2.1
48
- TRIO103 and TRIO104 no longer triggers when `trio.Cancelled` has been handled in previous except handlers.
59
- Add TRIO117: Reference to deprecated `trio.[NonBase]MultiError`; use `[Base]ExceptionGroup` instead.

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ pip install flake8-trio
3030
- **TRIO104**: `Cancelled` and `BaseException` must be re-raised - when a user tries to `return` or `raise` a different exception.
3131
- **TRIO105**: Calling a trio async function without immediately `await`ing it.
3232
- **TRIO106**: trio must be imported with `import trio` for the linter to work.
33-
- **TRIO107**: exit or `return` from async function with no guaranteed checkpoint or exception since function definition.
34-
- **TRIO108**: exit, yield or return from async iterable with no guaranteed checkpoint since possible function entry (yield or function definition)
35-
Checkpoints are `await`, `async for`, and `async with` (on one of enter/exit).
33+
- **TRIO107**: Renamed to TRIO910
34+
- **TRIO108**: Renamed to TRIO911
3635
- **TRIO109**: Async function definition with a `timeout` parameter - use `trio.[fail/move_on]_[after/at]` instead
3736
- **TRIO110**: `while <condition>: await trio.sleep()` should be replaced by a `trio.Event`.
3837
- **TRIO111**: Variable, from context manager opened inside nursery, passed to `start[_soon]` might be invalidly accessed while in use, due to context manager closing before the nursery. This is usually a bug, and nurseries should generally be the inner-most context manager.
@@ -57,13 +56,16 @@ pip install flake8-trio
5756

5857
### Warnings disabled by default
5958
- **TRIO900**: Async generator without `@asynccontextmanager` not allowed.
59+
- **TRIO910**: exit or `return` from async function with no guaranteed checkpoint or exception since function definition.
60+
- **TRIO911**: exit, yield or return from async iterable with no guaranteed checkpoint since possible function entry (yield or function definition)
61+
Checkpoints are `await`, `async for`, and `async with` (on one of enter/exit).
6062

6163
## Configuration
6264
[You can configure `flake8` with command-line options](https://flake8.pycqa.org/en/latest/user/configuration.html),
6365
but we prefer using a config file. The file needs to start with a section marker `[flake8]` and the following options are then parsed using flake8's config parser, and can be used just like any other flake8 options.
6466

6567
### `no-checkpoint-warning-decorators`
66-
Specify a list of decorators to disable checkpointing checks for, turning off TRIO107 and TRIO108 warnings for functions decorated with any decorator matching any in the list. Matching is done with [fnmatch](https://docs.python.org/3/library/fnmatch.html). Defaults to disabling for `asynccontextmanager`.
68+
Specify a list of decorators to disable checkpointing checks for, turning off TRIO910 and TRIO911 warnings for functions decorated with any decorator matching any in the list. Matching is done with [fnmatch](https://docs.python.org/3/library/fnmatch.html). Defaults to disabling for `asynccontextmanager`.
6769

6870
Decorators-to-match must be identifiers or dotted names only (not PEP-614 expressions), and will match against the name only - e.g. `foo.bar` matches `foo.bar`, `foo.bar()`, and `foo.bar(args, here)`, etc.
6971

flake8_trio/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def add_options(option_manager: OptionManager):
6363
required=False,
6464
comma_separated_list=True,
6565
help=(
66-
"Comma-separated list of decorators to disable TRIO107 & TRIO108 "
66+
"Comma-separated list of decorators to disable TRIO910 & TRIO911 "
6767
"checkpoint warnings for. "
6868
"Decorators can be dotted or not, as well as support * as a wildcard. "
6969
"For example, ``--no-checkpoint-warning-decorators=app.route,"

flake8_trio/visitors/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818
# Import all visitors so their decorators run, filling the above containers
1919
# This has to be done at the end to avoid circular imports
20-
from . import visitor2xx # isort: skip
2120
from . import visitor102 # isort: skip
2221
from . import visitor103_104 # isort: skip
23-
from . import visitor107_108 # isort: skip
2422
from . import visitor111 # isort: skip
23+
from . import visitor2xx # isort: skip
24+
from . import visitor91x # isort: skip
2525
from . import visitors # isort: skip

flake8_trio/visitors/helpers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def disabled_by_default(error_class: type[T]) -> type[T]:
3131

3232

3333
# ignores module and only checks the unqualified name of the decorator
34-
# used in 101 and 107/108
34+
# used in 101 and 910/911
3535
def has_decorator(decorator_list: list[ast.expr], *names: str):
3636
return any(
3737
(isinstance(dec, ast.Name) and dec.id in names)
@@ -42,7 +42,7 @@ def has_decorator(decorator_list: list[ast.expr], *names: str):
4242

4343
# matches the fully qualified name against fnmatch pattern
4444
# used to match decorators and methods to user-supplied patterns
45-
# used in 107/108 and 200
45+
# used in 910/911 and 200
4646
def fnmatch_qualified_name(name_list: list[ast.expr], *patterns: str) -> str | None:
4747
for name in name_list:
4848
if isinstance(name, ast.Call):
@@ -56,7 +56,7 @@ def fnmatch_qualified_name(name_list: list[ast.expr], *patterns: str) -> str | N
5656
return None
5757

5858

59-
# used in 103/104 and 107/108
59+
# used in 103/104 and 910/911
6060
def iter_guaranteed_once(iterable: ast.expr) -> bool:
6161
# static container with an "elts" attribute
6262
if hasattr(iterable, "elts"):

flake8_trio/visitors/visitor103_104.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def visit_For(self, node: ast.For | ast.While):
154154
if not self.unraised:
155155
return
156156

157-
# the following block is duplicated in Visitor107_108
157+
# the following block is duplicated in Visitor91X
158158
infinite_loop = False
159159
if isinstance(node, ast.While):
160160
try:

flake8_trio/visitors/visitor107_108.py renamed to flake8_trio/visitors/visitor91x.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
"""Contains Visitor107_108.
1+
"""Contains Visitor91X.
22
3-
107 looks for async functions without guaranteed checkpoints (or exceptions), and 108 does
3+
910 looks for async functions without guaranteed checkpoints (or exceptions), and 911 does
44
the same except for async iterables - while also requiring that they checkpoint between
55
each yield.
66
"""
@@ -13,14 +13,15 @@
1313
from ..base import Statement
1414
from .flake8triovisitor import Flake8TrioVisitor
1515
from .helpers import (
16+
disabled_by_default,
1617
error_class,
1718
fnmatch_qualified_name,
1819
has_decorator,
1920
iter_guaranteed_once,
2021
)
2122

2223

23-
# used in 107/108
24+
# used in 910/911
2425
def empty_body(body: list[ast.stmt]) -> bool:
2526
# Does the function body consist solely of `pass`, `...`, and (doc)string literals?
2627
return all(
@@ -35,13 +36,14 @@ def empty_body(body: list[ast.stmt]) -> bool:
3536

3637

3738
@error_class
38-
class Visitor107_108(Flake8TrioVisitor):
39+
@disabled_by_default
40+
class Visitor91X(Flake8TrioVisitor):
3941
error_codes = {
40-
"TRIO107": (
42+
"TRIO910": (
4143
"{0} from async function with no guaranteed checkpoint or exception "
4244
"since function definition on line {1.lineno}"
4345
),
44-
"TRIO108": (
46+
"TRIO911": (
4547
"{0} from async iterable with no guaranteed checkpoint since {1.name} "
4648
"on line {1.lineno}"
4749
),
@@ -89,7 +91,7 @@ def check_function_exit(self, node: ast.Return | ast.AsyncFunctionDef):
8991
node,
9092
"return" if isinstance(node, ast.Return) else "exit",
9193
statement,
92-
error_code="TRIO108" if self.has_yield else "TRIO107",
94+
error_code="TRIO911" if self.has_yield else "TRIO910",
9395
)
9496

9597
def visit_Return(self, node: ast.Return):
@@ -140,7 +142,7 @@ def visit_Yield(self, node: ast.Yield):
140142
node,
141143
"yield",
142144
statement,
143-
error_code="TRIO108",
145+
error_code="TRIO911",
144146
)
145147

146148
# mark as requiring checkpoint after

tests/eval_files/trio107.py renamed to tests/eval_files/trio910.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def __() -> Any:
1010
...
1111

1212

13-
# ARG --enable-visitor-codes-regex=(TRIO107)|(TRIO108)
13+
# ARG --enable-visitor-codes-regex=(TRIO910)|(TRIO911)
1414

1515

1616
# function whose body solely consists of pass, ellipsis, or string constants is safe

tests/eval_files/trio108.py renamed to tests/eval_files/trio911.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
_: Any = ""
66

7-
# ARG --enable-visitor-codes-regex=(TRIO107)|(TRIO108)
7+
# ARG --enable-visitor-codes-regex=(TRIO910)|(TRIO911)
88

99

1010
async def foo() -> Any:
@@ -779,12 +779,12 @@ async def foo_loop_static():
779779
break
780780
yield
781781

782-
# will get caught by any number of linters, but trio108 will also complain
782+
# will get caught by any number of linters, but trio911 will also complain
783783
for _ in 5: # type: ignore
784784
await foo()
785785
yield # error: 4, "yield", Stmt("yield", line-5)
786786

787-
# range with constant arguments also handled, see more extensive tests in 107
787+
# range with constant arguments also handled, see more extensive tests in 910
788788
for i in range(5):
789789
await foo()
790790
yield

tests/test_changelog_and_version.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ def test_version_increments_are_correct():
5656
assert current == prev._replace(patch=prev.patch + 1), msg
5757

5858

59+
IGNORED_CODES = ("TRIO107", "TRIO108")
60+
61+
5962
class test_messages_documented(unittest.TestCase):
6063
def runTest(self):
6164
documented_errors: dict[str, set[str]] = {}
@@ -96,6 +99,10 @@ def runTest(self):
9699
documented_errors["eval_files"].add(m)
97100
break
98101

102+
for code in IGNORED_CODES:
103+
for errset in documented_errors.values():
104+
errset.discard(code)
105+
99106
unique_errors: dict[str, set[str]] = {}
100107
missing_errors: dict[str, set[str]] = {}
101108
for key, codes in documented_errors.items():

tests/test_decorator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from flake8_trio.base import Statement
1111
from flake8_trio.visitors.helpers import fnmatch_qualified_name
12-
from flake8_trio.visitors.visitor107_108 import Visitor107_108
12+
from flake8_trio.visitors.visitor91x import Visitor91X
1313

1414

1515
def dec_list(*decorators: str) -> ast.Module:
@@ -102,8 +102,8 @@ def test_command_line_1(capfd):
102102
break
103103

104104
expected_out = (
105-
f"{file_path}:{expected_lineno}:1: TRIO107 "
106-
+ Visitor107_108.error_codes["TRIO107"].format(
105+
f"{file_path}:{expected_lineno}:1: TRIO910 "
106+
+ Visitor91X.error_codes["TRIO910"].format(
107107
"exit", Statement("function definition", expected_lineno)
108108
)
109109
+ "\n"

tests/test_flake8_trio.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,8 @@ def info_tuple(error: Error):
416416
assert err_msg == info_tuple(exp)
417417

418418

419-
def test_107_permutations():
420-
"""Tests all possible permutations for TRIO107.
419+
def test_910_permutations():
420+
"""Tests all possible permutations for TRIO910.
421421
422422
Since each test is so fast, and there's so many permutations, manually doing
423423
the permutations in a single test is much faster than the permutations from using
@@ -441,7 +441,7 @@ async def foo():
441441
await foo() | ... | return | None
442442
"""
443443
plugin = Plugin(ast.AST())
444-
initialize_options(plugin, args=["--enable-visitor-codes-regex=TRIO107"])
444+
initialize_options(plugin, args=["--enable-visitor-codes-regex=TRIO910"])
445445

446446
check = "await foo()"
447447

0 commit comments

Comments
 (0)
0