This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Tadhg McDonald-Jensen
Recipients Tadhg McDonald-Jensen, yselivanov
Date 2017-03-27.17:24:22
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1490635462.94.0.877144122619.issue29922@psf.upfronthosting.co.za>
In-reply-to
Content
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 <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 `x` was None when it was await-ed for.
History
Date User Action Args
2017-03-27 17:24:23Tadhg McDonald-Jensensetrecipients: + Tadhg McDonald-Jensen, yselivanov
2017-03-27 17:24:22Tadhg McDonald-Jensensetmessageid: <1490635462.94.0.877144122619.issue29922@psf.upfronthosting.co.za>
2017-03-27 17:24:22Tadhg McDonald-Jensenlinkissue29922 messages
2017-03-27 17:24:22Tadhg McDonald-Jensencreate