I came across the following problem with the combination of flask_sqlalchemy
and mypy
. When I define a new ORM object like:
class Foo(db.Model):
pass
where db
is a database created using SQL Alchemy applied to flask
app, mypy
type check produces the following error:
error: Class cannot subclass 'Model' (has type 'Any')
I would like to mention that I have sqlalchemy-stubs
installed. Can someone help me with this error?
Until stubs for flask_sqlalchemy
are officially supported, you can instead use sqlalchemy.orm.DeclarativeMeta
to 'alias' to db.Model
as pointed out in this response:
from sqlalchemy.ext.declarative import DeclarativeMeta
BaseModel: DeclarativeMeta = db.Model
class Foo(BaseModel):
pass
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With