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
Worse error from asynccontextmanager in Python 3.10 #90154
Comments
Consider this illegal code: import logging
from asyncio import sleep, gather, run
from contextlib import asynccontextmanager @asynccontextmanager async def test():
f = foo()
await gather(f.__aenter__(), f.__aenter__())
run(test()) If it's ran with Python 3.9, user gets a sensible error: File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/contextlib.py", line 175, in __aenter__ However, if it's ran with Python 3.10, user gets a cryptic error: File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/contextlib.py", line 197, in __aenter__ Which makes it harder to pinpoint what's wrong when the stack is complex. |
I think consider the sync version:
|
or consider the trio version:
|
ah I can repeat this on python3.8.10 trio but not python3.9.9 trio:
|
I see the change here: 1c5c9c8#diff-e00601a380ba6c916ba4333277fe6ea43d2477804002ab1ae64480f80fec8e3aR177-R179 this is intentionally implementing https://bugs.python.org/issue30306 for asynccontextmanagers that was initially missing |
you can see the analogous sync contextmanager issue on python3.6 with:
on python3.7+ you get the bpo-30306 behaviour
and python3.6 you get the same sort of error you see now for asynccontextmanagers:
|
Andrew, I see that you've closed this issue as "fixed". I'm a little confused by that. If you mean that 3.10 behaviour is better than 3.9, than perhaps "not a bug / rejected / wont fix" would make more sense. Actually I don't agree with Thomas's logic... his argument feels like consistency for its own sake. I agree with the intention of the change for the arguments to be released (https://bugs.python.org/issue30306) but I disagree with the cryptic error. I'm sure we could have both argument release and readable exception, e.g.: try: |
Do you expect sync and async contextmanagers to act differently? Why would sync contextmanagers raise AttributeError and async contextmanagers raise a RuntimeError? If it's sensible to guard against invalid re-entry for async contextmanagers then I think it's sensible to apply the same guard to sync contextmanagers. |
I'm fine with guarding both. |
Sorry, I closed it because async behavior reflects sync version now. If you want to improve both -- you are welcome! Perhaps it is worth another issue with another problem description. |
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: