Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add db.Model typing workaround in docs? #1186

Closed
TitaniumHocker opened this issue Apr 3, 2023 · 2 comments
Closed

Add db.Model typing workaround in docs? #1186

TitaniumHocker opened this issue Apr 3, 2023 · 2 comments

Comments

@TitaniumHocker
Copy link

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.

@davidism
Copy link
Member

davidism commented Apr 3, 2023

I don't plan to add MyPy workarounds to the docs at this time. Also, this isn't correct from a typing perspective, the class used during type checking is not a SQLAlchemy declarative base. I'd suggest adding an ignore rule for the name-defined error in model files to your mypy config.

I suggest anyone having an issue with this add their vote to the linked issue python/mypy#8603.

@davidism davidism closed this as not planned Won't fix, can't repro, duplicate, stale Apr 3, 2023
@TitaniumHocker

This comment was marked as off-topic.

@pallets-eco pallets-eco locked as resolved and limited conversation to collaborators Apr 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

2 participants