-
Notifications
You must be signed in to change notification settings - Fork 750
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
W606 in async function #985
Comments
await isn't a valid identifier in Python 3.7+ which is what the check is telling you. Using it as such will break for you regardless of whether it's inside an async function or not |
Adding a little background here. I have CI was set up to use flake8. A contributor wrote something like the following in a PR:
It is obvious to experienced programmers that you cannot await on a list. But it took us a while to figure out why there was a warning about |
Agreed. I was just thinking that it'd be less likely for people to accidentally use await as an identifier in async functions, than in regular functions. People working with async stuff should probably already know the keywords and people working only with regular functions might be completely unaware of those. The example above #985 (comment) shows that await an un-awaitable gets interpreted as using await as identifier. |
this appears to be working as intended |
@asottile would you mind elaborating how this is working as intended? Let me lay down my observations:
In the case: async def foo():
await 1
import asyncio
asyncio.get_event_loop().run_until_complete(foo()) This code means the same thing across all python versions supporting
The problem I see here is:
Apologies if I got it all wrong from my observation#0. |
your example is garbage: $ python3 t.py
Traceback (most recent call last):
File "t.py", line 4, in <module>
asyncio.get_event_loop().run_until_complete(foo())
File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
return future.result()
File "t.py", line 2, in foo
await 1
TypeError: object int can't be used in 'await' expression garbage in, garbage out (in this case, a lint error) pycodestyle doesn't have access to the ast so it approximates "async / await not used properly" as "has an operator after it". this means it cannot know whether it is inside an async function or not silencing the warning you're asking to be silenced would have hidden a bug in your code, I don't think you want that it's ignored in versions prior to 3.7 because it was not an error then |
That's exactly the case and I think the check (for a TypeError, which is consistent across python3.5-3.9) belongs to a linter like pyflakes rather than a style checker. A linter would be more capable to report these kinds of errors accurately. But I'll respect your project goals here.
|
There isn't because async def foo():
await +1
async def foo():
await -1 Because pycodestyle will interpret that as an expression. |
No, it's a TypeError, at least for the particular example
So in Python3.6:
My bad, just looked into the code and there's no heuristic whether a async def foo():
await 1
def bar():
await 1 tokenize output
edit: specifically, Line 1675 in 76edc0a
|
I think
aw.py:2:5
should say:list object is not awaitable
, considering that the code is already in an async function.A user writing code in an async function might not understand which objects are awaitable, but I'd expect them to be aware of async/await keywords.
Maybe this semantic check can't be done in pycodestyle, and would be better lived in pyflakes.
W606 also pops out for cases like
await 3
, where it's still invalid syntax even ifawait
is intended as an identifier.await [3]
is tricky thus shown in the examples above, await is both valid as a keyword or as an identifier inawait [3]
. I believe using whetherawait
sits in an async function to enable/disable W606 is good enough.The text was updated successfully, but these errors were encountered: