sqlalchemy-stubs
sqlalchemy-stubs copied to clipboard
Stubs for Flask-SQLAlchemy
Thank you for this project. Is it possible to provides stubs for Flask-SQLAlchemy as well?
Theoretically, there are no obstacles in generating some basic stubs and writing a mypy plugin for Flask-SQLAlchemy
. But this is a large piece of work, and it is unlikely I will work on this soon. However, if there are volunteers to help with this, I can explain what to do and give some advices.
I can try if you can give me some guidance
The first step would be to generate stubs. This is the process:
- Install the latest mypy version
- Clone the Flask-SQLAlchemy repo
- Run
stubgen flask_sqlalchemy
, output the result toflask_sqlalchemy-stubs
- Manually add some types following the docs
- Try type-checking some code using these stubs (you might need to update
setup.py
accordingly) - Write a mypy plugin for
flask_sqlalchemy
. You can look at what @bryanforbes is doing forGino
, it looks very similar. - Note this might need some fixes to mypy plugin hooks (I can also help with this if it will be necessary).
The ultimate question is where do we put all this, should this be a separate package or we can merge this here? @bryanforbes what is your opinion?
You can use these stubs with flask_sqlalchemy right now with just a small amount of legwork. This article sums it up excellently but the basic gist is that you define your own custom base model separate from the flask implementation and use sqlalchemys declarative_base. That way when you define models you import that as your base instead of flask's db:
from .models.base import Model
and when you initialize the flask app, you import that same Model to tie it to flasks session and session management. It was really quite a simple process to setup and I'm now consuming these stubs in my flask app!
I'm sure this doesn't cover anything and there will be a few gotchas, but at the very least this allows all your SQLAlchemy models to by typed.
@rmilejcz Could you please give some more details? Are you using if typing.TYPE_CHECKING: ...
? Can you show some code that uses Flask-SQLAlchemy
and has a type error caught by mypy using these stubs?
Would be nice!
@rmilejcz Can you give a little example please? I randomly already had my flask_sqlalchemy Models setup like that doc, but it doesn't seem to work fully.
My attr instance types read Union[Column[_T], T]
, instead of just T
.
Hi folks: Can I PR?
@tapaswenipathak Sure, but be aware this is not a small issue.
@tapaswenipathak I will love you forever
Any update on this? Any recommended work-around?
At the moment, I get:
error: Name 'db.Model' is not defined
They are of type sqlalchemy.ext.declarative.api.Model
, but I'm not even sure why it says that it is not defined.
They are of type
sqlalchemy.ext.declarative.api.Model
, but I'm not even sure why it says that it is not defined.
there is no such type. in fact, sqlalchemy does not even use the term model at all.
For any one looking for a workaround, this is what I went with for now.
from app import db
from sqlalchemy.ext.declarative import DeclarativeMeta
BaseModel: DeclarativeMeta = db.Model
class MyModel(BaseModel): ...
If you are using flask_sqlalchemy
then you can use from flask_sqlalchemy.model import DefaultMeta
instead of DeclarativeMeta
.
I tried @holdenrehg 's workaround for flask_sqlalchemy
but then got this error:
error: Class cannot subclass 'BaseModel' (has type 'Any')
Hello, I have just created one, could anyone have a try? It's just for testing now.
https://github.com/ssfdust/flask-sqlalchemy-stubs
Hello, I have just created one, could anyone have a try? It's just for testing now.
https://github.com/ssfdust/flask-sqlalchemy-stubs
I am getting a db.Model is not defined even after setting up flask-sqlalchemy-stubs
. It me be that I haven't configured it correctly though, because we are using custom classes for models/queries.
Hello, I have just created one, could anyone have a try? It's just for testing now. https://github.com/ssfdust/flask-sqlalchemy-stubs
I am getting a db.Model is not defined even after setting up
flask-sqlalchemy-stubs
. It me be that I haven't configured it correctly though, because we are using custom classes for models/queries.
I have updated the repo recently, do you upgrade to the latest version? On the other hand, if you are using pycharm, could you provide more detail information, for example, which plugin reports the error or the source code.
Hello, I have just created one, could anyone have a try? It's just for testing now. https://github.com/ssfdust/flask-sqlalchemy-stubs
I am getting a db.Model is not defined even after setting up
flask-sqlalchemy-stubs
. It me be that I haven't configured it correctly though, because we are using custom classes for models/queries.I have updated the repo recently, do you upgrade to the latest version? On the other hand, if you are using pycharm, could you provide more detail information, for example, which plugin reports the error or the source code.
Thanks for the reply. We got it working actually. The thing is, we have typed our implementations of Models and Queries ourselves, so there were conflicts between our implementation and this library. But after a little bit work the typing works on Models, Columns and Queries.
One thing that was a causing some issues is that the declared_attr
's first argument is typed as self
, but this is a class method and it should be cls
. I was thinking that maybe I should open a separate issue for this.
https://github.com/dropbox/sqlalchemy-stubs/blob/55470ceab8149db983411d5c094c9fe16343c58b/sqlalchemy-stubs/ext/declarative/api.pyi#L16