django-types icon indicating copy to clipboard operation
django-types copied to clipboard

Incompatible with djang-enums

Open syserr0r opened this issue 2 years ago • 1 comments

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

syserr0r avatar Nov 07 '23 17:11 syserr0r

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

sbdchd avatar Nov 10 '23 17:11 sbdchd