From f5f3da22019df7af30c8564c0c95b0620dcb0c54 Mon Sep 17 00:00:00 2001 From: ahmedasar00 Date: Mon, 23 Mar 2026 21:45:33 +0200 Subject: [PATCH 1/2] Move IntegerChoices/TextChoices stubs from __init__ to __new__ --- django-stubs/db/models/enums.pyi | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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: ... From 37618186b5cf12623970435b212b10e21c949fbd Mon Sep 17 00:00:00 2001 From: ahmedasar00 Date: Mon, 23 Mar 2026 22:01:33 +0200 Subject: [PATCH 2/2] new__ parameter names and allowlist stubtest errors --- scripts/stubtest/allowlist.txt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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