-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
error message when __aexit__ is not async #74108
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
Comments
When creating a asynchronous context manager if the __aexit__ method is not labeled as async (so it returns None instead of a coroutine) the error has a generic error message: TypeError: object NoneType can't be used in 'await' expression Would it be possible to change this so it indicates that it was the context that was invalid not an Example: import asyncio
class Test():
async def __aenter__(self):
print("aenter used")
value = asyncio.Future()
value.set_result(True)
return value
#FORGOT TO MARK AS async !!
def __aexit__(self, *errors):
print("aexit used")
return None
async def my_test():
async with Test() as x:
print("inside async with, now awaiting on", x)
await x my_test().send(None) Give the output: aenter used
inside async with, now awaiting on <Future finished result=True>
aexit used
Traceback (most recent call last):
File ".../test.py", line 19, in <module>
my_test().send(None)
File ".../test.py", line 16, in my_test
await x
TypeError: object NoneType can't be used in 'await' expression Which indicates to me that |
This is a specific example of the general problem reported in bpo-25538. |
It's a bit different code path/problem. But I agree, we should work on making both
I'm not sure if we can do anything about this. We compile 'async with' into a set of opcodes. The first ones resolve __aexit__, the latter ones await on it. The one that prepares to await on the aexit (GET_AWAITABLE) is the same that 'await' expression compiles to, and that opcode has no idea what exactly it awaits on. We could probably add GET_AEXIT_AWAITABLE opcode specifically to improve the error message (other than that it would be a copy of GET_AWAITABLE). I'll think about it. |
Definitely related, although the part of giving an error "... can't be used in 'await' expression" when no await expression is used isn't covered by that thread so I'm not sure I'd call this a direct duplicate. Currently when __anext__ return a non-awaitable this error is shown: TypeError: 'async for' received an invalid object from __anext__: NoneType So having __aenter__ and __aexit__ have similar error messages would be nice. Would it maybe make sense to implement this as adding an argument to GET_AWAITABLE to indicate whether it was called from await, __anext__, __aenter__ or __aexit__? (or others that may exist in future) I don't know enough about bytecode to know how it'd compare to an instruction for each case. |
Yes, but it will make it a tad slower (which will also affect await performance). I'll run some benchmarks. |
It's Interpreter Core problem, not specific to asyncio bug. |
This issue looks unrelated to bpo-25538. PR 6352 improves error messages without changing bytecode. It is so simple that could be backported to 3.7 if Ned approves this. But it conflicts with bpo-32949. This way will not work after merging any of current bpo-32949 PRs. We will have to find other way for bpo-32949. |
PR 6352 smells like a bug fix to me and I think it should be OK for 3.7.0b4. |
Can we backport it to 3.6 too? |
I suppose so. (Though you'll owe me if you break anything.) |
It would be hard to backport this to 3.5. Wordcode allows scanning back. |
3.5 is in security fixes only, so don't bother. |
Since BEFORE_ASYNC_WITH always is followed by GET_AWAITABLE, in future they can be merged into a single instruction (like GET_ANEXT or GET_YIELD_FROM_ITER). |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: