Skip to content

Support mypy #129

@bwo

Description

@bwo

I have this code:

class E(Enum):
    a = "a"
    b = "b"


class Blah(models.Model):
    e = EnumField(E, null=False)


def test(b: Blah) -> int:
    reveal_type(b.e)
    if b.e is E.a:
        return 1
    else:
        assert_never(b.e)


def mkb() -> Blah:
    return Blah.objects.create(e=E.a)

when I run mypy over it:

  • the reveal_type call prints Revealed type is "django_enum.fields.EnumField[Any]"
  • there is no complaint about assert_never, even though not all cases are handled
  • the Blah.objects.create call complains: error: Incompatible type for "e" of "Blah" (got "E", expected "str | int | Combinable") [misc]

this is mypy 1.15.0, django-stubs 5.1.3

The same thing happens if E inherits from TextChoices, fwiw

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions