-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Warn about missing await when calling async functions #1317
Comments
The challenge is that there is no need to immediately "await" an Awaitable. There are legitimate reasons why one might not want to do this. Pyright would generate many false positives if it assumed that all calls that returned an Awaitable required an "await" immediately. Perhaps we could say that any Awaitable object that is dropped on the floor (not assigned to a variable, passed as an argument, returned from the function, etc.) should always require an |
Interesting idea! Your suggestion seems safe and it should help in two of the three cases mentioned in python-trio/trio#671 description. It probably wouldn't help for the remaining case - broken cpython bot example: But of course, some protection is better than no protection at all. |
Yeah, that's why the issue over on the trio tracker mentions this as something for a pylint-trio/mypy-trio/etc. plugin to do... if you're using asyncio, then passing around Awaitables is common, but in trio programs, it's always an error.
It's definitely an error to drop a coroutine object on the floor, for all users. This isn't true of all Awaitables – in particular it's not necessarily an error to drop an It would also be nice if there was some kind of configuration option to enable "strict" mode, where it becomes an error to do anything with a coroutine object besides |
The type system doesn't distinguish between a Coroutine that originated from an There is an existing diagnostic check in pyright called |
Sure it does :-). Python's Sometimes it's okay to drop an That part is true for all Python users. The part that's specific to Trio users is that we could go one step further and eliminate the false negatives as well, by reporting an error whenever a |
Ah, I didn't realize that Pyright has two type checking modes: basic and strict. These modes affect which diagnostic rules are enabled and their resulting severity (warning vs. error). (Defaults in both modes can be overridden individually.) Basic mode is usually more conservative than strict, but if we're confident that we won't get any false positives, I think it would be fine to enable |
I've added the new diagnostic rule and enabled it by default for both basic and strict modes. Here's the PR. This will be included in the next release of pyright and pylance. Thanks for the suggestion and the detailed advice. |
Thank you @erictraut! This is very nice 👍 I wonder if this would cover most problems now. Some other forgotten async def coro(): return 1
def addone(n: int): return n + 1
async def main():
# "Coroutine[Any, Any, Literal[1]]" is incompatible with "int"
result = addone(coro())
# Operator "+" not supported for types "Coroutine[Any, Any, Literal[1]]" and "Literal[1]"
result = coro() + 1 In another case it seems impossible to implement without failing for commonly used patterns: async def coro(): return 1
async def main():
n = coro()
# No error, but it's useful to debug print things in Python, including coroutines
print(f'n={n}') (I'm guessing the last example is similar to what happened above with cpython bot.) |
This is now addressed in Pyright 1.1.98, which I just published. It will also be in the next release of Pylance. |
Pyright doesn't help me when I forget to put
await
in front of async functions. This is relevant in all async frameworks likeasyncio
and Tornado. But this is especially interesting when using Trio, because there are no cases where you want to leave outawait
in front of async calls, including user defined functions.Possible solution
Pyright should warn about missing
await
when calling async functions liketrio.sleep
, potentially behind a configuration option.Additional context
await
gotcha: https://trio.readthedocs.io/en/stable/tutorial.html#warning-don-t-forget-that-awaitThe text was updated successfully, but these errors were encountered: