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

Too many arguments for "TextChoices" #1857

Open
Kangaroux opened this issue Dec 5, 2023 · 3 comments
Open

Too many arguments for "TextChoices" #1857

Kangaroux opened this issue Dec 5, 2023 · 3 comments
Labels
bug Something isn't working good first issue Good for newcomers stubs Issues in stubs files (.pyi)

Comments

@Kangaroux
Copy link

Bug report

What's wrong

The stubs package does not support the functional enum API for choice enums.

How is that should be

Enum classes like TextChoices support the functional enum API (4th code block in that section).

System information

  • OS:
  • python version: 3.10.12
  • django version: 4.2.8
  • mypy version: 1.7.1
  • django-stubs version: 4.2.7
  • django-stubs-ext version: 4.2.7

Repro

from enum import Enum
from django.db.models import TextChoices

Enum("test", "A B C")
TextChoices("test", "A B C")
repro.py:5:1: error: Too many arguments for "TextChoices"  [call-arg]
@Kangaroux Kangaroux added the bug Something isn't working label Dec 5, 2023
@intgr intgr added stubs Issues in stubs files (.pyi) good first issue Good for newcomers labels Dec 5, 2023
@ngnpope
Copy link
Contributor

ngnpope commented Dec 13, 2023

Duplicate of #729.

The enum functional API is too dynamic for static type checking. In this case mypy would need to know how to split "A B C" and determine the types of each. Even as a list -- ["A", "B", "C"] -- it would need to iterate over that list.

Under the hood this will effectively look like an enum with no members to mypy and, as such, none of the other stuff built on top of that will work properly. (IIRC it's because mypy's enum plugin just gives up and bails out early.)

If you want to have type checking with enums/choicse, use the non-functional approach to defining enums.

See #729 (comment) for a previous response related to this.

@ngnpope
Copy link
Contributor

ngnpope commented Dec 13, 2023

@intgr I think it'd be worth adding a section to the FAQ to discourage use of the functional API as it seems as though it's not something that will ever be fixable.

@intgr
Copy link
Collaborator

intgr commented Dec 13, 2023

Similar to #1858 (comment), even if we can't teach mypy to understand the dynamically generated members of enum classes, I believe we can fix still typechecking of the constructor arguments?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers stubs Issues in stubs files (.pyi)
Development

No branches or pull requests

3 participants