Pycharm SqlAlchemy autocomplete not working Pycharm SqlAlchemy autocomplete not working python python

Pycharm SqlAlchemy autocomplete not working


PyCharm (or any other IDE) can't find most methods (query(), add(), commit() etc) as they are not defined in flask_sqlalchemy.SQLAlchemy class. SQLAlchemy methods are added to flask_sqlalchemy.SQLAlchemy objects dynamically during initialization. You can find this initialization function in flask_sqlalchemy module:

def _include_sqlalchemy(obj):    for module in sqlalchemy, sqlalchemy.orm:        for key in module.__all__:            if not hasattr(obj, key):                setattr(obj, key, getattr(module, key))

Just for the testing: you can type from sqlalchemy.orm import Query. I think PyCharm will find it's objects just fine.

I can't suggest any solution, maybe someone else here know about possibility of dynamically added attributes autocompletion.


class User(db.Model):    query: db.Query # Type hint here    __tablename__ = 'users'    id = db.Column(db.Integer, primary_key=True)    username = db.Column(db.String(16), index=True, unique=True)

adding type hint query: db.Queryenter image description here