diff --git a/django-stubs/db/models/enums.pyi b/django-stubs/db/models/enums.pyi index 96a41244c..f3385c840 100644 --- a/django-stubs/db/models/enums.pyi +++ b/django-stubs/db/models/enums.pyi @@ -61,9 +61,9 @@ class _IntegerChoicesType(ChoicesType): # They are omitted on purpose to avoid having convoluted stubs for these enums: class IntegerChoices(Choices, IntEnum, metaclass=_IntegerChoicesType): # type: ignore[misc] @overload - def __init__(self, x: ConvertibleToInt) -> None: ... + def __new__(cls, value: ConvertibleToInt) -> IntegerChoices: ... @overload - def __init__(self, x: ConvertibleToInt, label: _StrOrPromise) -> None: ... + def __new__(cls, value: ConvertibleToInt, label: _StrOrPromise) -> IntegerChoices: ... @enum_property @override def value(self) -> int: ... @@ -80,9 +80,9 @@ class _TextChoicesType(ChoicesType): class TextChoices(Choices, StrEnum, metaclass=_TextChoicesType): # type: ignore[misc] @overload - def __init__(self, object: str) -> None: ... + def __new__(cls, value: str) -> TextChoices: ... @overload - def __init__(self, object: str, label: _StrOrPromise) -> None: ... + def __new__(cls, value: str, label: _StrOrPromise) -> TextChoices: ... @enum_property @override def value(self) -> str: ... diff --git a/scripts/stubtest/allowlist.txt b/scripts/stubtest/allowlist.txt index 52aeb3a16..f257183d5 100644 --- a/scripts/stubtest/allowlist.txt +++ b/scripts/stubtest/allowlist.txt @@ -231,6 +231,21 @@ django.contrib.auth.views.UserModel # Limited stubtest support for enums (https://github.com/python/mypy/issues/16806) django.db.migrations.operations.base.OperationCategory.__new__ +# The `label` parameter in __new__ is a deliberate stub-only addition: +# Django's ChoicesType metaclass processes (value, label) tuples at class definition time, +# but the runtime __new__ (from enum.Enum) only accepts (cls, value). +# We keep the label overload so that defining enum members type-checks correctly. +django.db.models.enums.IntegerChoices.__new__ +django.db.models.enums.TextChoices.__new__ +django.db.models.IntegerChoices.__new__ +django.db.models.TextChoices.__new__ +django.contrib.gis.db.models.IntegerChoices.__new__ +django.contrib.gis.db.models.TextChoices.__new__ +django.tasks.TaskResultStatus.__new__ +django.tasks.base.TaskResultStatus.__new__ +django.tasks.base.TextChoices.__new__ +django.tasks.signals.TaskResultStatus.__new__ + # These are dynamically added when using `save(commit=False)` # See https://docs.djangoproject.com/en/stable/topics/forms/modelforms/#the-save-method django.forms.BaseModelForm.save_m2m