I have the following code (snippet):
from sqlalchemy.orm import declarative_base
Base = declarative_base()
with pip I installed
$ pip install -U sqlalchemy[mypy] sqlalchemy-stubs mypy
$ pip list | egrep -i '(sqlal|mypy)'
mypy 0.982
mypy-extensions 0.4.3
SQLAlchemy 1.4.42
sqlalchemy-stubs 0.4
SQLAlchemy-Utils 0.38.3
sqlalchemy2-stubs 0.0.2a29
Still, when running mypy file.py
, I'm getting:
$ python3 -mmypy test.py
test.py:1: error: Module "sqlalchemy.orm" has no attribute "declarative_base"
Found 1 error in 1 file (checked 1 source file)
I'm using Python 3.10.5 from within a virtualenv.
What can I do to debug more?
SQLAlchemy 2.0 was completely overhauled for native compatibility with static type checking. You will have to refactor existing code following the migration guide. There is a deprecated Mypy plugin that can be used in the interim, but I found it unstable (several months ago) so I gave up on it and refactored my code.
Install sqlalchemy with sqlalchemy2-stubs
poetry add "sqlalchemy[mypy]"
And then update mypy.ini
with
[mypy]
plugins = sqlalchemy.ext.mypy.plugin
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With