python / typeshed

Collection of library stubs for Python, with static types
Other
4.26k stars 1.72k forks source link

Flask-SQLAlchemy-Lite support for flask-migrate #12121

Open thomthom opened 3 months ago

thomthom commented 3 months ago

Flask-SQLAlchemy-Lite was just released which is intended to be a replacement for Flask-SQLAlchemy. https://flask-sqlalchemy-lite.readthedocs.io/en/stable/

With that there is then a type conflict with flask_migrate: https://github.com/python/typeshed/blob/main/stubs/Flask-Migrate/flask_migrate/__init__.pyi#L52

It expects flask_sqlalchemy.extension.SQLAlchemy, but flask_sqlalchemy_lite._extension.SQLAlchemy is now a compatible alternative

I'm pretty new to python so I'm not sure what the best approach to allow the flask_migrate typings to allow both.

Would it work to:

# ...
from flask_sqlalchemy import SQLAlchemy
from flask_sqlalchemy_lite import SQLAlchemy as SQLAlchemyLite
# ...
class Migrate:
    configure_callbacks: list[_ConfigureCallback]
    db: SQLAlchemy | SQLAlchemyLite | None
    # ...

Or is there a better approach?

srittau commented 2 months ago

As I understand, flask-sqlalchemy-lite is already typed. Importing both flask_sqlalchemy and flask_sqlalchemy_lite in the stubs would be problematic, since that means that users would need to install both with the stubs or using conditional imports in the stubs.

I think the best solution here is to define a protocol in the flask-migrate stubs that describes the common interface used by flask migrate and import and depend on neither in the migrate stubs.