VSCODE doesn't recognize subclassing of db.Model
Question:
I have a Flask Project with Flask-SQLAlchemy and a custom Model class.
For type hinting I modified the SQLAlchemy class like this:
class CustomSQLAlchemy(flask_sqlalchemy.SQLAlchemy):
Model: CustomModel
After that, VSCODE does show db.Model as a CustomModel:
However, when adding attributes I don’t get any suggestions for attributes from db.Model when using super for instance. I think it has something to do with the fact that db.Model isn’t hinted as a class like ToolTimeMixIn. I know I can use db.Model instead of super(), but I personally find it inconvenient.
I am using Pylance and Visual Studio IntelliCode with a Python 3.9 venv.
I know it is unlikely, but does someone have experience with this?
Answers:
After some research I have found the solution:
from typing import Type
class CustomSQLAlchemy(flask_sqlalchemy.SQLAlchemy):
Model: Type[CustomModel]
Query: Type[CustomQueryObject]
db = CustomSQLAlchemy(query_class=CustomQueryObject, model_class=CustomModel, session_options={"expire_on_commit": False})
With this VSCODE will recognize db.Model as a class and give proper suggestions.
I found these while organizing my note. They are my current solution and I hope they can help
- ssfdust/flask-sqlalchemy-stubs better typed stubs.
- a working stubs ryanwang520/flask_sqlalchemy-s tubs. It’s quality is not as good as the
ssfdust
above, but it helps pylance to understand the code.
- To enable stubs in VSCode (pylance), create a
typings
folder in the root of your project, and put the stubs in it like this instruction in pylance-release#187. Or, to use a custom folder, see pylance-release#285
- Note all stubs are for flask-sql-alchemy based on sql-alchemy 1.4.0, which is subject to update as the new version of sql-alchemy release candidates rc2 are out.
I have a Flask Project with Flask-SQLAlchemy and a custom Model class.
For type hinting I modified the SQLAlchemy class like this:
class CustomSQLAlchemy(flask_sqlalchemy.SQLAlchemy):
Model: CustomModel
After that, VSCODE does show db.Model as a CustomModel:
However, when adding attributes I don’t get any suggestions for attributes from db.Model when using super for instance. I think it has something to do with the fact that db.Model isn’t hinted as a class like ToolTimeMixIn. I know I can use db.Model instead of super(), but I personally find it inconvenient.
I am using Pylance and Visual Studio IntelliCode with a Python 3.9 venv.
I know it is unlikely, but does someone have experience with this?
After some research I have found the solution:
from typing import Type
class CustomSQLAlchemy(flask_sqlalchemy.SQLAlchemy):
Model: Type[CustomModel]
Query: Type[CustomQueryObject]
db = CustomSQLAlchemy(query_class=CustomQueryObject, model_class=CustomModel, session_options={"expire_on_commit": False})
With this VSCODE will recognize db.Model as a class and give proper suggestions.
I found these while organizing my note. They are my current solution and I hope they can help
- ssfdust/flask-sqlalchemy-stubs better typed stubs.
- a working stubs ryanwang520/flask_sqlalchemy-s tubs. It’s quality is not as good as the
ssfdust
above, but it helps pylance to understand the code. - To enable stubs in VSCode (pylance), create a
typings
folder in the root of your project, and put the stubs in it like this instruction in pylance-release#187. Or, to use a custom folder, see pylance-release#285 - Note all stubs are for flask-sql-alchemy based on sql-alchemy 1.4.0, which is subject to update as the new version of sql-alchemy release candidates rc2 are out.