Here is the idea for that warning if the code will await a literal:
async def a():
await ()
will warning that tuple object can’t be awaited.
Reason
- The python has warning for
1(),1[2]. The analysis for whether an operation can do on one type object for literal is easy. - Unlike
raise,assert, theawaitis an expression-level keyword so that it can appear anywhere. I just searched for that the python codes withawait (. Only a half appeared at the start of the line. This will make it more difficult for maintainers to review. - Unlike
1+"foo"(which python doesn’t and won’t warn), the objects afterawaitoften associate with system or internet operations. When the programmer write:
async def some_operation():
await (
# many codes
)
but it changes to a tuple literal due to wrong parentheses or commas, finally the interpreter create the object in the tuple but can’t continue because the code try to await a tuple, something doesn’t work, which cause the serious consequences (data corruption, system anomaly). If the warning can do at the compiling, programmer can kill the process before the problem code running and avoid the problem above. Meanwhile, adding the warning for “await a” is also easier than “a+b”. The warning for “a+b” need two types and their opposite magic methods returned NotImplement which is almost impossible, while “await a” just check whether the type of a has __await__. It means that this change is low risk and high return.
Effect
It will just effect the python test (with -W error). To fix it, the code like:
async def a():
await 1
change to:
async def a():
x = 1
await x
can fix the problem.
The normal project won’t be effect if the code is right at all.