Marcin Możejko
Marcin Możejko

Reputation: 40516

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?

Upvotes: 9

Views: 4905

Answers (2)

Mks
Mks

Reputation: 124

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

Upvotes: 6

Untill the issue is close you can set more loyal configuration MyPy for your project through mypy.ini

[mypy]
ignore_errors = True

This will ignore all non-fatal errors

Upvotes: -4

Related Questions