Search code examples
pythonsqlalchemymypy

Error importing plugin "sqlalchemy.ext.mypy.plugin": cannot import name 'Optional' from 'mypy.plugin'


Following the documentation here https://docs.sqlalchemy.org/en/14/orm/extensions/mypy.html I got an error while running mypy on any file using sqlalchemy mypy plugin.

To Reproduce

Create a new virtualenv using Python 3.8

pip install sqlalchemy[mypy]==1.4

Create a simple mypy config (mypy.ini)

[mypy] plugins = sqlalchemy.ext.mypy.plugin

Run mypy on any file

mypy --config-file=mypy.ini

Error

Error importing plugin "sqlalchemy.ext.mypy.plugin": cannot import name 'Optional' from 'mypy.plugin'

Versions.

OS: Linux Ubuntu 22.04

Python: 3.8

SQLAlchemy: 1.4

mypy: 0.991


Solution

  • Following the issue on GitHub the problem is the SQLAlchemy version. SQLAlchemy 1.4.0 is not supported.

    Using SQLAlchemy 1.4.44 solved the problem.