Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask SQL Alchemy vs MyPy - error with Model type

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?

like image 214
Marcin Możejko Avatar asked Jun 26 '19 13:06

Marcin Możejko


1 Answers

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
like image 65
Mks Avatar answered Nov 02 '22 06:11

Mks