-
-
Notifications
You must be signed in to change notification settings - Fork 426
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
"TextChoices" has no attribute "choices" #1858
Comments
Duplicate of #729. The enum functional API is too dynamic for static type checking. In this case Under the hood this will effectively look like an enum with no members to 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. |
@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. |
This issue isn't about the dynamic enum members. The report is about the Even if we can't teach mypy about the dynamically generated members of enum, fixing |
But +1 for adding a FAQ entry about the enum members. |
It is a duplicate as it stems from the same issue. I delved into it a little more and the problem is down to how Try the following: from django.db import models
Functional = models.TextChoices("Functional", "A B C") # error: Too many arguments for "Functional" [call-arg]
class Standard(models.TextChoices):
A = "A"
B = "B"
C = "C"
Functional.choices # error: "TextChoices" has no attribute "choices" [attr-defined]
Standard.choices # ok As referred to in #729 (comment), this is because def check_enum_call(
self, node: Expression, var_name: str, is_func_scope: bool
) -> TypeInfo | None:
...
if fullname not in ENUM_BASES:
return None
... And looking at the definition for # Note: 'enum.EnumMeta' is deliberately excluded from this list. Classes that directly use
# enum.EnumMeta do not necessarily automatically have the 'name' and 'value' attributes.
ENUM_BASES: Final = frozenset(
("enum.Enum", "enum.IntEnum", "enum.Flag", "enum.IntFlag", "enum.StrEnum")
) Now we cannot add What you are proposing is adding something for IMO it's just easier to state that use of the functional API is not supported until that issue is addressed in Footnotes
|
I would prefer a pragmatic approach and introduce small hacks as needed (within reason of course, taking into account maintenance complexity). There are lots of things in the Django API that are imprecisely typed or only half-supported due to mypy/Python type system limitations. I'd rather avoid declaring parts of the Django API as unusable in
You're right, I didn't consider that I think this issue could also be solved, by defining a Of course it could be that I'm missing something and this doesn't work out. |
Bug report
What's wrong
When using an enum created from the functional enum API, mypy complains that the enum doesn't have an attribute
choices
, however it does.How is that should be
choices
should be a supported attribute when the enum is instantiated.System information
python
version: 3.10.12django
version: 4.2.8mypy
version: 1.7.1django-stubs
version: 4.2.7django-stubs-ext
version: 4.2.7Repro
If you run the script you see the choices are printed correctly:
The text was updated successfully, but these errors were encountered: