-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Detect unawaited coroutines #9833
Comments
I don't believe we check this now, but it looks like a reasonable rule to me. |
async
function without await
WIP |
Note that mypy already implements this rule with its That doesn't stop ruff implementing it as well -- but I feel like it might be a better fit for a type checker, as a type checker has the power to analyse multiple files at a time and track which calls return awaitable objects. Ruff doesn't (yet) have the power to do that (though it's obviously something we'd like to have). Still, it can be hard to configure mypy to enable just that warning without also enabling other warnings that might be unwelcome -- and maybe we can provide a valuable lint here even while only looking at one file at a time. |
@AlexWaygood - I guess the rule as implemented here is actually a bit different (unclear if better or worse) than what's described in the Mypy docs, since this raises whenever you have an |
Ah interesting -- I think what you describe is possibly what's originally being asked for in this issue, but the implementation that's been put forward for consideration in #9911 is more similar to mypy's |
Oh sorry, I hadn't looked at the code yet. I was imagining we'd look for |
Yeah it sounds like #9911 addresses a separate (but also valid) use case |
[Edit: I'll work on #9951, not this.] |
async
function without await
Do you know when this will be implemented? |
This will take a while. Ruff cannot currently resolve functions across files, which is a requirement for this rule to work reliable. |
I want to just add a strong +1 here for detecting non-awaited coroutines. Check out the discussion here: https://www.reddit.com/r/Python/comments/1gulzjt/rewriting_4000_lines_of_python_to_migrate_to/ One of the commenters says they have a huge code base but can not (never?) migrate to async because it's just too hard to make sure all the code works together (like no missed awaits). This feature would make Ruff a must have tool for such upgrades. However, I realize this is super hard. For example, this should be fine even though the coroutines are not directly awaited. t1 = async_thing_1()
t2 = async_thing_2()
await task.gather(t1, t2) |
Note that the (idiomatically, you'd call |
Hey @Zac-HD nice to see you here. I'm somewhat familiar with Trio and AnyIO. But how do you actually run two or more things concurrently without starting them before awaiting them? for x in xes:
await async_thing(x) still only runs one thing at a time. And await gather(f(x), g(x), ...) is fine but can be tricky if you have to build up the args for each one in a loop. |
@mikeckennedy I believe the canonical example would be using |
Hello. I fixed my code via ruff.
Applied fix and unsafe fixes.
But ruff did not find one error.
I forgot to write await when calling an asynchronous function.
Accordingly, I received RuntimeWarning never awaited...
e.g.
Is there any way to do this, and if not, will it be taken into account in the new versions of ruff?
The text was updated successfully, but these errors were encountered: