Skip to content

ChoiceField's choices and Enum produces arg-type mypy error #795

@sshishov

Description

@sshishov

Bug report

Hi @sobolevn @asottile-sentry , This is more like a question.

After some change mypy start producing some error and this error does make sense. Therefore I need the proper suggestion from the experienced guys!

What's wrong

Before this change (#454) we were doing this:

class MyEnum(enum.StrEnum):
    VALUE_1 = 'value_1'
    VALUE_2 = 'value_2'


MySerializer(drf_serializers.Serializer):
    my_choices = drf_serializers.ChoiceField(choices=MyEnum, ...)

But after this change we are receiving this mypy error:

error: Argument "choices" to "ChoiceField" has incompatible type "type[MyEnum]"; expected "Sequence[Any]"  [arg-type]

How is that should be

What is the suggested way to fix this issue? Should we somehow convert the value to "sequence"?

    my_choices = drf_serializers.ChoiceField(choices=tuple(MyEnum), ...)

System information

  • OS:
  • python version: 3.13.5
  • django version: 5.2.4
  • mypy version: 1.17.1
  • django-stubs version: 5.2.2

It would nice to hear your suggestions, guys!

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions