-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RUF029 (unneeded async) needs nuance for class methods #10980
Comments
When testing this rule to see if this rule should be enabled for my own projects, the current implementation of this rule needs more nuance that just classmethods. I understand that this is the point of preview rules, but there are too many cases where functions are async due to interaction with other code due to function coloring. Some simple examples of this included
|
Thanks, that's really helpful -- getting this kind of feedback is definitely one of the goals of |
Another false positive example is functions/methods that are passed as callables to other functions/methods. E.g. import asyncio
from collections.abc import Awaitable, Callable
async def foo() -> str: # RUF029 Function `foo` is declared `async`, but doesn't `await` or use `async` features.
return "foo"
async def foobar(foo_impl: Callable[[], Awaitable[str]]) -> str:
return await foo_impl() + "bar"
async def print_foobar() -> None:
print(await foobar(foo))
asyncio.run(print_foobar()) It doesn't handle
|
In addition, FastAPI recommends using unneeded async (unless blocking I/O) because not using async tells FastAPI to run the route handler inside a thread (assuming blocking I/O). => Being able to ignore decorated fonctions would be nice |
The summary of this PR says that it's about class methods, but the conversation has already expanded to mention decorators, so I'll add my cases here. I'm happy to open a new issue if that's more appropriate. Two more cases where we hit this:
@routes.get(r'/favicon.ico')
async def favicon_ico(request: web.Request) -> web.FileResponse:
return web.FileResponse('src/branding/default/favicon.ico')
@pytest.mark.asyncio
async def test_immediate_shutdown(rule): # noqa: RUF029
peer = rule.apply_rule({'payload': 'test'})
assert peer is not None
peer.close() where I feel like a good heuristic might be "disable rule on decorated functions". But note: we mark our async pytest test cases with the |
...and one counter-example to the "decorator" heuristic. This rule just found a |
Further feedback on RUF029 not specific to class methods, if a test relies on an async fixture it needs to be async. Raising an error about this is extra bad because async pytest plugins are currently inconsistent in how they handle it pytest-dev/pytest#10839, agronholm/anyio#803 I started attempting to add this to flake8-async before I found RUF029, and there I'm opting to disable the check if a function is called |
Issue #9951 lead to PR #9966 where we check for functions that are declared async, but do not use any async features, and flag them. However, this caused several false positives (e.g. for class methods implemented as
pass
intending to be overridden).Context discussing false positives
#9966 (comment)
#9966 (comment)
#9966 (comment)
In #9966 we opted for the simplest solution: The rule is disabled for methods of a class. However, we need to decide on a policy for them because the rule is still useful there (as #9966 (comment) indicates).
The text was updated successfully, but these errors were encountered: