Skip to content

Commit 1195f00

Browse files
committed
initial support for asyncio. 106 and 9xx are working
1 parent 6281cb3 commit 1195f00

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+114
-29
lines changed

README.md

Lines changed: 1 addition & 1 deletion

flake8_async/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,19 @@ def add_options(option_manager: OptionManager | ArgumentParser):
298298
" suggestions with [anyio|trio]."
299299
),
300300
)
301+
add_argument(
302+
"--asyncio",
303+
# action=store_true + parse_from_config does seem to work here, despite
304+
# https://github.com/PyCQA/flake8/issues/1770
305+
action="store_true",
306+
required=False,
307+
default=False,
308+
help=(
309+
"Change the default library to be asyncio instead of trio."
310+
" If anyio/trio is imported it will assume that is also available and"
311+
" print suggestions with [asyncio|anyio/trio]."
312+
),
313+
)
301314

302315
@staticmethod
303316
def parse_options(options: Namespace):
@@ -342,6 +355,7 @@ def get_matching_codes(
342355
startable_in_context_manager=options.startable_in_context_manager,
343356
trio200_blocking_calls=options.trio200_blocking_calls,
344357
anyio=options.anyio,
358+
asyncio=options.asyncio,
345359
disable_noqa=options.disable_noqa,
346360
)
347361

flake8_async/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Options:
3232
startable_in_context_manager: Collection[str]
3333
trio200_blocking_calls: dict[str, str]
3434
anyio: bool
35+
asyncio: bool
3536
disable_noqa: bool
3637

3738

flake8_async/visitors/visitor_utility.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,13 @@ def __init__(self, *args: Any, **kwargs: Any):
117117
# see imports
118118
if self.options.anyio:
119119
self.add_library("anyio")
120+
if self.options.asyncio:
121+
self.add_library("anyio")
120122

121123
def visit_Import(self, node: ast.Import):
122124
for alias in node.names:
123125
name = alias.name
124-
if name in ("trio", "anyio") and alias.asname is None:
126+
if name in ("trio", "anyio", "asyncio") and alias.asname is None:
125127
self.add_library(name)
126128

127129

flake8_async/visitors/visitors.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
if TYPE_CHECKING:
1212
from collections.abc import Mapping
1313

14+
LIBRARIES = ("trio", "anyio", "asyncio")
15+
1416

1517
@error_class
1618
class Visitor106(Flake8AsyncVisitor):
@@ -19,12 +21,12 @@ class Visitor106(Flake8AsyncVisitor):
1921
}
2022

2123
def visit_ImportFrom(self, node: ast.ImportFrom):
22-
if node.module in ("trio", "anyio"):
24+
if node.module in LIBRARIES:
2325
self.error(node, node.module)
2426

2527
def visit_Import(self, node: ast.Import):
2628
for name in node.names:
27-
if name.name in ("trio", "anyio") and name.asname is not None:
29+
if name.name in LIBRARIES and name.asname is not None:
2830
self.error(node, name.name)
2931

3032

tests/autofix_files/async100.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# type: ignore
22
# AUTOFIX
3+
# NOASYNCIO
34

45
import trio
56

tests/autofix_files/async100_simple_autofix.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# NOASYNCIO
12
# AUTOFIX
23
import trio
34

tests/autofix_files/noqa.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# AUTOFIX
22
# NOANYIO # TODO
3+
# NOASYNCIO
34
# ARG --enable=ASYNC100,ASYNC911
45
from typing import Any
56

tests/eval_files/anyio_trio.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# type: ignore
22
# ARG --enable=ASYNC220
33
# NOTRIO
4+
# NOASYNCIO
5+
# BASE_LIBRARY anyio
46

57
# anyio eval will automatically prepend this test with `--anyio`
68
import trio # isort: skip

tests/eval_files/async100.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# type: ignore
22
# AUTOFIX
3+
# NOASYNCIO
34

45
import trio
56

0 commit comments

Comments
 (0)