Skip to content

Commit

Permalink
fix tests, add fixture logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jakkdl committed Oct 29, 2024
1 parent fcc89d2 commit d40b064
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 154 deletions.
2 changes: 1 addition & 1 deletion flake8_async/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@


# CalVer: YY.month.patch, e.g. first release of July 2022 == "22.7.1"
__version__ = "24.10.2"
__version__ = "24.10.3"


# taken from https://github.com/Zac-HD/shed
Expand Down
12 changes: 12 additions & 0 deletions flake8_async/visitors/visitor91x.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ def leave_FunctionDef(
original_node.asynchronous is not None
and not self.has_await
and not func_empty_body(original_node)
and not func_has_decorator(original_node, "overload")
# skip functions named 'text_xxx' with params, since they may be relying
# on async fixtures. This is esp bad as sync funcs relying on async fixtures
# is not well handled: https://github.com/pytest-dev/pytest/issues/10839
# also skip funcs with @fixtures and params
and not (
original_node.params.params
and (
original_node.name.value.startswith("test_")
or func_has_decorator(original_node, "fixture")
)
)
):
self.error(original_node)
self.restore_state(original_node)
Expand Down
87 changes: 0 additions & 87 deletions tests/autofix_files/async124.py

This file was deleted.

49 changes: 0 additions & 49 deletions tests/autofix_files/async124.py.diff

This file was deleted.

7 changes: 5 additions & 2 deletions tests/autofix_files/async910.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,12 @@ def foo_normal_func_1():
def foo_normal_func_2(): ...


# overload decorator
# overload decorator is skipped
# overload functions should always be empty, so the check is somewhat redundant,
# but making one non-empty to check the logic.
@overload
async def foo_overload_1(_: bytes): ...
async def foo_overload_1(_: bytes):
raise NotImplementedError


@typing.overload
Expand Down
44 changes: 32 additions & 12 deletions tests/eval_files/async124.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
# 910/911 will also autofix async124, in the sense of adding a checkpoint. This is perhaps
# not what the user wants though, so this would be a case in favor of making 910/911 not
# trigger when async124 does.
# AUTOFIX
# ASYNCIO_NO_AUTOFIX
from typing import Any
# NOAUTOFIX # all errors get "fixed" except for foo_fix_no_subfix
from typing import Any, overload
from pytest import fixture


def condition() -> bool:
Expand Down Expand Up @@ -66,16 +66,36 @@ async def foo_empty_pass():
pass


# we could consider filtering out functions named `test_.*` to not give false alarms on
# tests that use async fixtures.
# For ruff and for running through flake8 we could expect users to use per-file-ignores,
# but running as standalone we don't currently support that. (though probs wouldn't be
# too bad to add support).
# See e.g. https://github.com/agronholm/anyio/issues/803 for why one might want an async
# test without awaits.
async def test_async_fixture(
my_async_fixture,
): # ASYNC910: 0, "exit", Statement("function definition", lineno)
assert my_async_fixture.setup_worked_correctly


async def test_async_fixture(
my_anyio_fixture,
): # ASYNC124: 0 # ASYNC910: 0, "exit", Statement("function definition", lineno)
assert my_anyio_fixture.setup_worked_correctly
# no params -> no async fixtures
async def test_no_fixture(): # ASYNC124: 0 # ASYNC910: 0, "exit", Statement("function definition", lineno)
print("blah")


# skip @overload. They should always be empty, but /shrug
@overload
async def foo_overload():
print("blah")


async def foo_overload(): ...


# skip @[pytest.]fixture if they have any params, since they might depend on other
# async fixtures
@fixture
async def foo_fix(my_async_fixture):
print("blah")


# but @fixture with no params can be converted to sync
@fixture
async def foo_fix_no_subfix(): # ASYNC124: 0
print("blah")
7 changes: 5 additions & 2 deletions tests/eval_files/async910.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,12 @@ def foo_normal_func_1():
def foo_normal_func_2(): ...


# overload decorator
# overload decorator is skipped
# overload functions should always be empty, so the check is somewhat redundant,
# but making one non-empty to check the logic.
@overload
async def foo_overload_1(_: bytes): ...
async def foo_overload_1(_: bytes):
raise NotImplementedError


@typing.overload
Expand Down
9 changes: 8 additions & 1 deletion tests/trio_options.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
"""Test file used in tests/test_decator.py to check decorator and command line."""

import asyncio

app = None


def condition() -> bool:
return False


@app.route # type: ignore
async def f():
print("hello world")
if condition():
await asyncio.sleep(0)

0 comments on commit d40b064

Please sign in to comment.