Skip to content

Add db.Model typing workaround in docs? #1186

@TitaniumHocker

Description

@TitaniumHocker

mypy currently does not correctly handle inheritance from a class attribute with a model - db.Model:

error: Name "db.Model" is not defined  [name-defined]

According on the related bugreport, this issue will not be resolved anytime soon.

So, there is working workaround:

import typing as t
from flask_sqlalchemy import SQLAlchemy
from flask_sqlalchemy.model import Model as BaseModel

db = SQLAlchemy()

if t.TYPE_CHECKING:
    class Model(BaseModel):
        pass
else:
    Model = db.Model

class MyModel(Model):
    pass

It will be useful if this workaround will be mentioned in documentation.
I can make changes to the documentation myself if I get the approval.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions