pythonflaskpycharmflask-sqlalchemyflask-marshmallow

SQLALchemy .query: Unresolved attribute reference 'query' for class 'Car'


I have an issue that was already mentioned here https://youtrack.jetbrains.com/issue/PY-44557

But I couldn't find a solution yet. I'm using Python Flask with SQLAlchemy to create an API service.

Here are my imports:

from flask import Flask, jsonify, request
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import Column, Integer, String, Float
import os
from flask_marshmallow import Marshmallow


app = Flask(__name__)
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'+os.path.join(basedir, 'dealers.db')


db = SQLAlchemy(app)
ma = Marshmallow(app)

Here is my data model:

class Car(db.Model):
    __tablename__ = 'cars'
    car_id = Column(Integer, primary_key=True)
    car_name = Column(String)
    car_type = Column(String)
 

class CarSchema(ma.Schema):
    class Meta:
        fields = ('car_id', 'car_name', 'car_type')

car_schema = CarSchema()  
cars_schema = CarSchema(many=True)

I'm having an issue/Warning with my UDF when I use .query

def cars():
    car_list = Car.query.all()  # *Unresolved attribute reference 'query' for class 'Car'
    result = cars_schema.dump(car_list)
    return jsonify(result)

Solution

  • I've found out how to make a hacky and partial solution for this issue using type-hinting stubs.

    Create abstract python class derived from db.Model in a separate file and a "stub"-file with a same name

    Example:

    abstract.py:

    class AbstractBaseModel(db.Model):
        __abstract__ = True
    

    abstract.pyi:

    from flask_sqlalchemy import SQLAlchemy
    from sqlalchemy import orm
    from sqlalchemy.ext.declarative import DeclarativeMeta
    
    
    db: SQLAlchemy = SQLAlchemy()
    
    
    class _FBase(db.Model):
        query: orm.Query
    
    
    class AbstractBaseModel(_FBase, DeclarativeMeta):
        ...
    

    Then make models deriving from abstract.AbstractBaseModel instead of db.Model e.g:

    from abstract import AbstractBaseModel
    
    class Car(AbstractBaseModel):
        __tablename__ = 'cars'
        car_id = Column(Integer, primary_key=True)
        car_name = Column(String)
        car_type = Column(String)
    

    I've also created a sample project for testing this workaround