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

Rule for async without await #339

Open
ryanc-bs opened this issue Jan 21, 2025 · 5 comments
Open

Rule for async without await #339

ryanc-bs opened this issue Jan 21, 2025 · 5 comments
Labels
enhancement New feature or request

Comments

@ryanc-bs
Copy link

Hi, I'm wondering what you think about adding a rule to catch async functions that have no await statements - and which could therefore be changed into sync functions for a small performance and simplicity gain.

It could work in the same way as this no-async-without-await rule in TSLint: https://palantir.github.io/tslint/rules/no-async-without-await/#:~:text=Marking%20a%20function%20as%20async,keyword%20is%20there%20by%20mistake.

Of course there may be instances where a function needs to be async to satisfy some external interface. But in all other cases, it would be useful to have a way to scan for pointless async functions.

I don't think an auto-fix would be very simple since the call sites also need to be updated, but flagging as a warning for manual fixing would be helpful.

@jakkdl
Copy link
Member

jakkdl commented Jan 21, 2025

This kind of exists in the form of ASYNC910, though that will flag a lot more than just functions without await and so might not be relevant for you.
Ruff does have unused-async, though you'll need to run with --preview to get it.

Agree that autofix is not really on the table.

But I'm not opposed to adding it to flake8-async itself, and even for ruff it kind of fits better among the other async rules than randomly among their own. Implementing it should be straightforward, only complication would be if we want it to interact with ASYNC9xx and suppress ASYNC9xx if async-no-await is raised.
@Zac-HD for a second opinion

@jakkdl jakkdl added the enhancement New feature or request label Jan 21, 2025
@ryanc-bs
Copy link
Author

Thanks, I did not know about RUF029. Something I noticed while trying that is that is only seems to work for simple functions and not for methods on a class - would be great to be able to lint both of these.

@Zac-HD
Copy link
Member

Zac-HD commented Jan 21, 2025

  • I think this would need to be an opt-in rule, and thus doesn't need to interact with others at all (users can be responsible for choosing a satisfiable configuration, so we can just note "conflicts with $rule and $rule, choose one")
  • I'd bet it skips methods on a class because it assumes that those are satisfying some external interface, which is my experience - having to manually ignore those would be very annoying.

@jakkdl
Copy link
Member

jakkdl commented Jan 22, 2025

I suppose we could have two rules then, one for functions and one for class methods, with accompanying blurbs about their optional-ness.

@TeamSpen210
Copy link

One other situation that might be worth excluding is functions with decorators applied, since those probably also expect a specific interface. Although of course many don't or are adaptive between sync/async.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants