Issue 29922: error message when aexit is not async (original) (raw)

process

Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: yselivanov Nosy List: Chewy, Mark.Shannon, RazerM, Tadhg McDonald-Jensen, asvetlov, levkivskyi, miss-islington, ned.deily, r.david.murray, serhiy.storchaka, yselivanov
Priority: normal Keywords: patch

Created on 2017-03-27 17:24 by Tadhg McDonald-Jensen, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 6352 merged serhiy.storchaka,2018-04-02 21:36
PR 6353 merged miss-islington,2018-04-02 22:42
PR 6354 merged miss-islington,2018-04-02 22:42
PR 6370 merged serhiy.storchaka,2018-04-04 14:08
PR 6371 merged miss-islington,2018-04-04 15:46
PR 6373 merged miss-islington,2018-04-04 16:30
Messages (19)
msg290630 - (view) Author: Tadhg McDonald-Jensen (Tadhg McDonald-Jensen) * Date: 2017-03-27 17:24
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 `await` statement? Since the traceback points to the last statement of the with block it can create very confusing errors if the last statement was an await. 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 aexit used Traceback (most recent call last): File ".../test.py", line 19, in 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 `x` was None when it was await-ed for.
msg290631 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-03-27 17:33
This is a specific example of the general problem reported in issue 25538.
msg290633 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2017-03-27 17:46
> This is a specific example of the general problem reported in issue 25538. It's a bit different code path/problem. But I agree, we should work on making both `with` and `async with` a bit more usable. > Would it be possible to change this so it indicates that it was the context that was invalid not an `await` statement? 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.
msg290639 - (view) Author: Tadhg McDonald-Jensen (Tadhg McDonald-Jensen) * Date: 2017-03-27 19:53
> This is a specific example of the general problem reported in issue 25538. 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.
msg290640 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2017-03-27 19:56
> 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__? Yes, but it will make it a tad slower (which will also affect await performance). I'll run some benchmarks.
msg308797 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2017-12-20 20:02
It's Interpreter Core problem, not specific to asyncio bug.
msg314827 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-04-02 21:48
This issue looks unrelated to . 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 . This way will not work after merging any of current PRs. We will have to find other way for .
msg314831 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2018-04-02 22:23
PR 6352 smells like a bug fix to me and I think it should be OK for 3.7.0b4.
msg314832 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2018-04-02 22:28
> 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?
msg314834 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2018-04-02 22:31
> Can we backport it to 3.6 too? I suppose so. (Though you'll owe me if you break anything.)
msg314836 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-04-02 22:41
New changeset a68f2f0578bbf812fa2264d0e0bb388340d6e230 by Serhiy Storchaka in branch 'master': bpo-29922: Improve error messages in 'async with' (GH-6352) https://github.com/python/cpython/commit/a68f2f0578bbf812fa2264d0e0bb388340d6e230
msg314838 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-04-02 22:51
It would be hard to backport this to 3.5. Wordcode allows scanning back.
msg314839 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2018-04-02 22:56
3.5 is in security fixes only, so don't bother.
msg314932 - (view) Author: miss-islington (miss-islington) Date: 2018-04-04 14:09
New changeset fcd4e03e08a2d4ec1cde17beb66e2b22a052500f by Miss Islington (bot) in branch '3.7': bpo-29922: Improve error messages in 'async with' (GH-6352) https://github.com/python/cpython/commit/fcd4e03e08a2d4ec1cde17beb66e2b22a052500f
msg314933 - (view) Author: miss-islington (miss-islington) Date: 2018-04-04 14:09
New changeset 4fd6c27dc8ba7ca97aa70e1ab98729f2207bbe19 by Miss Islington (bot) in branch '3.6': bpo-29922: Improve error messages in 'async with' (GH-6352) https://github.com/python/cpython/commit/4fd6c27dc8ba7ca97aa70e1ab98729f2207bbe19
msg314939 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-04-04 15:45
New changeset 2eeac269dd1e04a2a179384576986c3e47895ee0 by Serhiy Storchaka in branch 'master': bpo-29922: Add more tests for error messages in 'async with'. (GH-6370) https://github.com/python/cpython/commit/2eeac269dd1e04a2a179384576986c3e47895ee0
msg314940 - (view) Author: miss-islington (miss-islington) Date: 2018-04-04 16:28
New changeset 785f36c876721c12f653371e9893527a25140624 by Miss Islington (bot) in branch '3.7': bpo-29922: Add more tests for error messages in 'async with'. (GH-6370) https://github.com/python/cpython/commit/785f36c876721c12f653371e9893527a25140624
msg314941 - (view) Author: miss-islington (miss-islington) Date: 2018-04-04 17:18
New changeset 1487cd14bb1e04de5b98fffc5ec41c6cf6b5d5f1 by Miss Islington (bot) in branch '3.6': bpo-29922: Add more tests for error messages in 'async with'. (GH-6370) https://github.com/python/cpython/commit/1487cd14bb1e04de5b98fffc5ec41c6cf6b5d5f1
msg314944 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-04-04 17:39
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).
History
Date User Action Args
2022-04-11 14:58:44 admin set github: 74108
2018-04-04 17:39:22 serhiy.storchaka set status: open -> closedresolution: fixedmessages: + stage: patch review -> resolved
2018-04-04 17🔞59 miss-islington set messages: +
2018-04-04 16:30:17 miss-islington set pull_requests: + <pull%5Frequest6084>
2018-04-04 16:28:51 miss-islington set messages: +
2018-04-04 15:46:30 miss-islington set pull_requests: + <pull%5Frequest6082>
2018-04-04 15:45:20 serhiy.storchaka set messages: +
2018-04-04 14:09:17 miss-islington set messages: +
2018-04-04 14:09:06 miss-islington set nosy: + miss-islingtonmessages: +
2018-04-04 14:08:30 serhiy.storchaka set pull_requests: + <pull%5Frequest6081>
2018-04-02 22:56:38 yselivanov set messages: +
2018-04-02 22:51:09 serhiy.storchaka set messages: +
2018-04-02 22:42:48 miss-islington set pull_requests: + <pull%5Frequest6063>
2018-04-02 22:42:04 miss-islington set pull_requests: + <pull%5Frequest6062>
2018-04-02 22:41:48 serhiy.storchaka set messages: +
2018-04-02 22:31:43 ned.deily set messages: + versions: + Python 3.6
2018-04-02 22:28:04 yselivanov set messages: +
2018-04-02 22:23:53 ned.deily set messages: + versions: + Python 3.7
2018-04-02 21:48:36 serhiy.storchaka set nosy: + ned.deily, serhiy.storchaka, Mark.Shannonsuperseder: Traceback from __exit__ method is misleading -> messages: +
2018-04-02 21:36:49 serhiy.storchaka set keywords: + patchstage: needs patch -> patch reviewpull_requests: + <pull%5Frequest6061>
2017-12-20 20:05:25 yselivanov set versions: + Python 3.8, - Python 2.7, Python 3.3, Python 3.4, Python 3.5, Python 3.6, Python 3.7
2017-12-20 20:02:43 asvetlov set nosy: + asvetlovmessages: + components: + Interpreter Core, - asyncio
2017-03-31 19:53:35 levkivskyi set nosy: + levkivskyi
2017-03-29 08:43:18 RazerM set nosy: + RazerM
2017-03-28 07:40:47 Chewy set nosy: + Chewy
2017-03-27 19:56:48 yselivanov set status: closed -> openresolution: duplicate -> (no value)stage: resolved -> needs patch
2017-03-27 19:56:30 yselivanov set messages: +
2017-03-27 19:53:36 Tadhg McDonald-Jensen set messages: +
2017-03-27 17:46:10 yselivanov set assignee: yselivanovmessages: +
2017-03-27 17:33:56 r.david.murray set status: open -> closedsuperseder: Traceback from __exit__ method is misleadingnosy: + r.david.murraymessages: + resolution: duplicatestage: resolved
2017-03-27 17:24:22 Tadhg McDonald-Jensen create