MyPy and sqlalchemy - "Session" has no attribute "__enter__"; maybe "__iter__"? Mypyattr-defined

47 Views Asked by At

I recently updated mypy configuration for my python 3.11 project, adding an .ini file to check my already working sql alchemy code. I'm using sqlalchemy == 2.0.29

It oddly started complaining about this line of code:

with Session(db) as session:
    ...

the 'db' variable is being used as per SQlalchemy 2.0 docs:

db = create_engine(db_string)

Here is the MyPy error in detail:

"Session" has no attribute "__enter__"; maybe "__iter__"? [attr-defined]
"Session" has no attribute "__exit__" [attr-defined]

While this is my current mypy.ini file:

[mypy]
python_version = 3.11
ignore_missing_imports = True
plugins = sqlmypy

Why is mypy complaining about that perfectly working line of code?

0

There are 0 best solutions below