TypeError: object NoneType can't be used in 'await' expression when closing connection

67 Views Asked by At

I am on MacOS running 8.0.28 (MySQL Community Server - GPL) with Python 3.9.12. I have a straight forward script to test aiomysql:

import asyncio
import aiomysql

async def test_db_connection():
    conn = None
    try:
        conn = await aiomysql.connect(
            host=host,
            port=3306,
            user=user,
            password=password,
            db=mydb
        )
    except Exception as e:
        print(f"Error during connection: {e}")
    finally:
        if conn is not None:
            await conn.close()


if __name__ == "__main__":
    asyncio.run(test_db_connection(), debug=True)

I get the following error on the conn.close():

TypeError: object NoneType can't be used in 'await' expression

I am running aiomysql v0.2.0. Can anyone suggest how I might narrow down the issue. I tried on my native python install and get the same error. The connection is made because if I print out the conn is returns a valid connection. Not sure why I'm getting this error.

0

There are 0 best solutions below