sbdchd / django-types

:doughnut: Type stubs for Django
MIT License
186 stars 62 forks source link

Incompatible with djang-enums #211

Open syserr0r opened 10 months ago

syserr0r commented 10 months ago

For a model Model with a field such as:

class TestModel(django.db.models.Model):
    bar = django_enum.EnumField(enums.Bar, default=enums.Bar.FOO)

and code such as:

foo = TestModel()
foo.bar = enums.Bar.FOO

gives the following in pyright:

Cannot assign member "bar" for type "TestModel"
  "Literal[Bar.FOO]" is incompatible with "_ST@Field"

The VSCode IDE which also uses pyright reports the same error however when inspecting the field type it is shown as _GT@Field

Forcing the type in the model definition as EnumMixin such as follows, makes no difference:

class TestModel(django.db.models.Model):
    bar: django_enum.fields.EnumMixin = django_enum.EnumField(enums.Bar, default=enums.Bar.FOO)

I'd rather not disable type checking on all django-enum assignments if I can avoid it. Should I be raising this with the djang-enum project instead?

Any help/advice greatly appreciated

sbdchd commented 10 months ago

Hmm this is tricky! I believe I used django-enum on a past project, but can't remember how we got it to work or if we stopped using it

It might make sense to create type stubs for django-enum (.pyi files) to get pyright / mypy to behave