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?
sqlalchemy-stubs
contains type hints for just the sqlalchemy library, but not flask_sqlalchemy. There's an open issue asking for support for flask_sqlalchemy, but it doesn't look like anybody's started work on it yet. Perhaps you could try making your own basic stubs, taking inspiration from the existing ones? Somebody in the issue also mentioned they were able to use models from sqlalchemy directly and hook them into Flask, which let them be properly typed. Perhaps you could investigate that approach? – Electrolysis