Skip to content
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

Detecting unneeded async keywords on functions #9951

Closed
Greesb opened this issue Feb 12, 2024 · 4 comments · Fixed by #9966
Closed

Detecting unneeded async keywords on functions #9951

Greesb opened this issue Feb 12, 2024 · 4 comments · Fixed by #9966
Labels
rule Implementing or modifying a lint rule

Comments

@Greesb
Copy link

Greesb commented Feb 12, 2024

Hello,

I refactored a bunch of code and thought about a rule that could be useful: detecting function defined with the async keywords not using any await inside it, example:

async def unneeded_async() -> None:  # should raise the error
    print("lol")

async def legit_async() -> None:  # should not raise the error
    await some_stuff()

There's probably some use cases where the async is needed, but, imo, most of the time those async can be removed.

What do you think ?

@Greesb Greesb changed the title Detected unneeded async keywords` Detected unneeded async keywords on functions Feb 12, 2024
@zanieb
Copy link
Member

zanieb commented Feb 12, 2024

We like this idea :) this is a duplicate of #9833 though

@zanieb zanieb closed this as not planned Won't fix, can't repro, duplicate, stale Feb 12, 2024
@Greesb
Copy link
Author

Greesb commented Feb 12, 2024

Doesn't look like it is, the issue you mentioned is for detecting missing await keywords when calling async functions.

What i'm talking about is to detect functions that are defined with the async keyword but do not need it because there's no await inside them. @zanieb

@zanieb
Copy link
Member

zanieb commented Feb 12, 2024

Yeah I see, it's a little mixed up over there as we're discussing both things.

We can track this separately.

@plredmond I'll move you over to this issue.

@zanieb zanieb reopened this Feb 12, 2024
@zanieb zanieb added the rule Implementing or modifying a lint rule label Feb 12, 2024
@Greesb Greesb changed the title Detected unneeded async keywords on functions Detecting unneeded async keywords on functions Feb 12, 2024
plredmond added a commit to plredmond/ruff that referenced this issue Feb 12, 2024
@plredmond
Copy link
Contributor

In progress implementation: main...plredmond:ruff:issue9951-spurious_async

plredmond added a commit to plredmond/ruff that referenced this issue Feb 12, 2024
plredmond added a commit to plredmond/ruff that referenced this issue Feb 12, 2024
plredmond added a commit to plredmond/ruff that referenced this issue Feb 12, 2024
plredmond added a commit to plredmond/ruff that referenced this issue Feb 12, 2024
plredmond added a commit to plredmond/ruff that referenced this issue Feb 12, 2024
plredmond added a commit to plredmond/ruff that referenced this issue Feb 13, 2024
plredmond added a commit to plredmond/ruff that referenced this issue Apr 15, 2024
plredmond added a commit to plredmond/ruff that referenced this issue Apr 15, 2024
plredmond added a commit to plredmond/ruff that referenced this issue Apr 15, 2024
plredmond added a commit to plredmond/ruff that referenced this issue Apr 15, 2024
… the reformatted RUF029 fixture output due to ff5023e
plredmond added a commit to plredmond/ruff that referenced this issue Apr 15, 2024
…argument to avoid the posibility of wrong inputs per charlie
plredmond added a commit to plredmond/ruff that referenced this issue Apr 15, 2024
plredmond added a commit to plredmond/ruff that referenced this issue Apr 15, 2024
plredmond added a commit to plredmond/ruff that referenced this issue Apr 15, 2024
plredmond added a commit to plredmond/ruff that referenced this issue Apr 15, 2024
… yields to a fail-case per @carljm (yield does not require async)
plredmond added a commit to plredmond/ruff that referenced this issue Apr 15, 2024
plredmond added a commit to plredmond/ruff that referenced this issue Apr 15, 2024
plredmond added a commit to plredmond/ruff that referenced this issue Apr 16, 2024
plredmond added a commit to plredmond/ruff that referenced this issue Apr 16, 2024
plredmond added a commit to plredmond/ruff that referenced this issue Apr 16, 2024
plredmond added a commit to plredmond/ruff that referenced this issue Apr 16, 2024
plredmond added a commit to plredmond/ruff that referenced this issue Apr 16, 2024
…cs inside asyncs (fail_4a, fail_4b) with tests having asyncs inside of normal functions (pass_5, fail_4c)
plredmond added a commit to plredmond/ruff that referenced this issue Apr 16, 2024
…nctionDef and StmtClass def that do not traverse the body, use them in spurious async to avoid attributing correct /inner/ async functions to incorrect outer functions
plredmond added a commit to plredmond/ruff that referenced this issue Apr 16, 2024
plredmond added a commit to plredmond/ruff that referenced this issue Apr 16, 2024
plredmond added a commit to plredmond/ruff that referenced this issue Apr 16, 2024
plredmond added a commit to plredmond/ruff that referenced this issue Apr 16, 2024
plredmond added a commit to plredmond/ruff that referenced this issue Apr 16, 2024
…unctions in class definitions using semantic scope info (per @charliermarsh, and thanks to @MichaReiser)
plredmond added a commit to plredmond/ruff that referenced this issue Apr 16, 2024
plredmond added a commit to plredmond/ruff that referenced this issue Apr 16, 2024
plredmond added a commit to plredmond/ruff that referenced this issue Apr 16, 2024
plredmond added a commit to plredmond/ruff that referenced this issue Apr 16, 2024
plredmond added a commit to plredmond/ruff that referenced this issue Apr 16, 2024
plredmond added a commit that referenced this issue Apr 16, 2024
## Summary

This change adds a rule to detect functions declared `async` but lacking
any of `await`, `async with`, or `async for`. This resolves #9951.

## Test Plan

This change was tested by following
https://docs.astral.sh/ruff/contributing/#rule-testing-fixtures-and-snapshots
and adding positive and negative cases for each of `await` vs nothing,
`async with` vs `with`, and `async for` vs `for`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants