[New-bugs-announce] [issue29922] error message when __aexit__ is not async

Tadhg McDonald-Jensen report at bugs.python.org
Mon Mar 27 13:24:22 EDT 2017


New submission from Tadhg McDonald-Jensen:

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.

----------
components: asyncio
messages: 290630
nosy: Tadhg McDonald-Jensen, yselivanov
priority: normal
severity: normal
status: open
title: error message when __aexit__ is not async
type: behavior
versions: Python 2.7, Python 3.3, Python 3.4, Python 3.5, Python 3.6, Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29922>
_______________________________________


More information about the New-bugs-announce mailing list