From cf7c4b38a800836bac73ec1d48ed04053fc4fa83 Mon Sep 17 00:00:00 2001 From: Adam Hitchcock Date: Tue, 15 Sep 2026 13:18:48 -0700 Subject: [PATCH 1/4] Let every field's choices= accept what Field.choices already does The per-field __new__ overloads listed only the pair iterable, and 17 of 49 named a Choices class, so a dict (Django 5.0), a callable, or a TextChoices class on a TextField was rejected at the constructor while the attribute itself allowed all three. One generic alias replaces the 49 hand-written unions and keeps the existing str/int precision: an IntegerField still refuses a TextChoices class. --- django-stubs/db/models/fields/__init__.pyi | 128 ++++++++++----------- tests/pyright/test_choices.py | 83 +++++++++++++ 2 files changed, 144 insertions(+), 67 deletions(-) create mode 100644 tests/pyright/test_choices.py diff --git a/django-stubs/db/models/fields/__init__.pyi b/django-stubs/db/models/fields/__init__.pyi index 8ef9e05b5..9fe2b8621 100644 --- a/django-stubs/db/models/fields/__init__.pyi +++ b/django-stubs/db/models/fields/__init__.pyi @@ -8,7 +8,7 @@ from typing import Any, ClassVar, Generic, Literal, TypeAlias, TypeVar, overload from django.core.checks import CheckMessage from django.core.exceptions import FieldDoesNotExist as FieldDoesNotExist from django.core.validators import _ValidatorCallable -from django.db.models import IntegerChoices, Model, TextChoices +from django.db.models import Choices, IntegerChoices, Model, TextChoices from django.db.models.expressions import Col, Combinable, Func from django.db.models.query_utils import Q, RegisterLookupMixin from django.forms import Widget @@ -22,6 +22,17 @@ _ChoicesMapping: TypeAlias = Mapping[Any, _StrOrPromise | Mapping[Any, _StrOrPro _LiteralFieldChoices: TypeAlias = Iterable[_Choice | _ChoiceNamedGroup] | _ChoicesMapping _FieldChoices: TypeAlias = _LiteralFieldChoices | Callable[[], _LiteralFieldChoices] +_CV = TypeVar("_CV") +_CC = TypeVar("_CC", bound=Choices) +# What a field's ``choices=`` argument accepts: the pair / named-group iterable, +# a mapping, a Choices class, or a zero-argument callable returning any of those. +_ChoicesFor: TypeAlias = ( + Iterable[tuple[_CV, _StrOrPromise] | tuple[str, Iterable[tuple[_CV, _StrOrPromise]]]] + | Mapping[_CV, _StrOrPromise | Mapping[_CV, _StrOrPromise]] + | type[_CC] + | Callable[[], _LiteralFieldChoices] +) + _LimitChoicesTo: TypeAlias = Q | dict[str, Any] _LimitChoicesToCallable: TypeAlias = Callable[[], _LimitChoicesTo] _AllLimitChoicesTo: TypeAlias = _LimitChoicesTo | _LimitChoicesToCallable | _ChoicesCallable # noqa: PYI047 @@ -138,8 +149,7 @@ class IntegerField(Field[_I | Combinable, _I], Generic[_I]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_I, _StrOrPromise] | tuple[str, Iterable[tuple[_I, _StrOrPromise]]]] - | type[IntegerChoices] = ..., + choices: _ChoicesFor[_I, IntegerChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -167,8 +177,7 @@ class IntegerField(Field[_I | Combinable, _I], Generic[_I]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_I, _StrOrPromise] | tuple[str, Iterable[tuple[_I, _StrOrPromise]]]] - | type[IntegerChoices] = ..., + choices: _ChoicesFor[_I, IntegerChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -201,8 +210,7 @@ class PositiveIntegerField(PositiveIntegerRelDbTypeMixin, IntegerField[_I]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_I, _StrOrPromise] | tuple[str, Iterable[tuple[_I, _StrOrPromise]]]] - | type[IntegerChoices] = ..., + choices: _ChoicesFor[_I, IntegerChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -230,8 +238,7 @@ class PositiveIntegerField(PositiveIntegerRelDbTypeMixin, IntegerField[_I]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_I, _StrOrPromise] | tuple[str, Iterable[tuple[_I, _StrOrPromise]]]] - | type[IntegerChoices] = ..., + choices: _ChoicesFor[_I, IntegerChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -261,8 +268,7 @@ class PositiveSmallIntegerField(PositiveIntegerRelDbTypeMixin, IntegerField[_I]) unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_I, _StrOrPromise] | tuple[str, Iterable[tuple[_I, _StrOrPromise]]]] - | type[IntegerChoices] = ..., + choices: _ChoicesFor[_I, IntegerChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -290,8 +296,7 @@ class PositiveSmallIntegerField(PositiveIntegerRelDbTypeMixin, IntegerField[_I]) unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_I, _StrOrPromise] | tuple[str, Iterable[tuple[_I, _StrOrPromise]]]] - | type[IntegerChoices] = ..., + choices: _ChoicesFor[_I, IntegerChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -321,8 +326,7 @@ class SmallIntegerField(IntegerField[_I]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_I, _StrOrPromise] | tuple[str, Iterable[tuple[_I, _StrOrPromise]]]] - | type[IntegerChoices] = ..., + choices: _ChoicesFor[_I, IntegerChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -350,8 +354,7 @@ class SmallIntegerField(IntegerField[_I]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_I, _StrOrPromise] | tuple[str, Iterable[tuple[_I, _StrOrPromise]]]] - | type[IntegerChoices] = ..., + choices: _ChoicesFor[_I, IntegerChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -381,8 +384,7 @@ class BigIntegerField(IntegerField[_I]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_I, _StrOrPromise] | tuple[str, Iterable[tuple[_I, _StrOrPromise]]]] - | type[IntegerChoices] = ..., + choices: _ChoicesFor[_I, IntegerChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -410,8 +412,7 @@ class BigIntegerField(IntegerField[_I]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_I, _StrOrPromise] | tuple[str, Iterable[tuple[_I, _StrOrPromise]]]] - | type[IntegerChoices] = ..., + choices: _ChoicesFor[_I, IntegerChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -441,8 +442,7 @@ class PositiveBigIntegerField(IntegerField[_I]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_I, _StrOrPromise] | tuple[str, Iterable[tuple[_I, _StrOrPromise]]]] - | type[IntegerChoices] = ..., + choices: _ChoicesFor[_I, IntegerChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -470,8 +470,7 @@ class PositiveBigIntegerField(IntegerField[_I]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_I, _StrOrPromise] | tuple[str, Iterable[tuple[_I, _StrOrPromise]]]] - | type[IntegerChoices] = ..., + choices: _ChoicesFor[_I, IntegerChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -503,7 +502,7 @@ class FloatField(Field[_F | Combinable, _F], Generic[_F]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_F, _StrOrPromise] | tuple[str, Iterable[tuple[_F, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_F, Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -531,7 +530,7 @@ class FloatField(Field[_F | Combinable, _F], Generic[_F]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_F, _StrOrPromise] | tuple[str, Iterable[tuple[_F, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_F, Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -568,7 +567,7 @@ class DecimalField(Field[_DEC | Combinable, _DEC], Generic[_DEC]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_DEC, _StrOrPromise] | tuple[str, Iterable[tuple[_DEC, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_DEC, Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -598,7 +597,7 @@ class DecimalField(Field[_DEC | Combinable, _DEC], Generic[_DEC]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_DEC, _StrOrPromise] | tuple[str, Iterable[tuple[_DEC, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_DEC, Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -631,8 +630,7 @@ class AutoField(AutoFieldMixin, IntegerField[int], metaclass=AutoFieldMeta): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[int, _StrOrPromise] | tuple[str, Iterable[tuple[int, _StrOrPromise]]]] - | type[IntegerChoices] = ..., + choices: _ChoicesFor[int, IntegerChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -662,8 +660,7 @@ class BigAutoField(AutoFieldMixin, BigIntegerField[int]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[int, _StrOrPromise] | tuple[str, Iterable[tuple[int, _StrOrPromise]]]] - | type[IntegerChoices] = ..., + choices: _ChoicesFor[int, IntegerChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -693,8 +690,7 @@ class SmallAutoField(AutoFieldMixin, SmallIntegerField[int]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[int, _StrOrPromise] | tuple[str, Iterable[tuple[int, _StrOrPromise]]]] - | type[IntegerChoices] = ..., + choices: _ChoicesFor[int, IntegerChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -727,8 +723,7 @@ class CharField(Field[_C | Combinable, _C], Generic[_C]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_C, _StrOrPromise] | tuple[str, Iterable[tuple[_C, _StrOrPromise]]]] - | type[TextChoices] = ..., + choices: _ChoicesFor[_C, TextChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -757,8 +752,7 @@ class CharField(Field[_C | Combinable, _C], Generic[_C]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_C, _StrOrPromise] | tuple[str, Iterable[tuple[_C, _StrOrPromise]]]] - | type[TextChoices] = ..., + choices: _ChoicesFor[_C, TextChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -791,7 +785,7 @@ class SlugField(CharField[_C]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_C, _StrOrPromise] | tuple[str, Iterable[tuple[_C, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_C, TextChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -821,7 +815,7 @@ class SlugField(CharField[_C]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_C, _StrOrPromise] | tuple[str, Iterable[tuple[_C, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_C, TextChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -853,7 +847,7 @@ class EmailField(CharField[_C]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_C, _StrOrPromise] | tuple[str, Iterable[tuple[_C, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_C, TextChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -882,7 +876,7 @@ class EmailField(CharField[_C]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_C, _StrOrPromise] | tuple[str, Iterable[tuple[_C, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_C, TextChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -913,7 +907,7 @@ class URLField(CharField[_C]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_C, _StrOrPromise] | tuple[str, Iterable[tuple[_C, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_C, TextChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -942,7 +936,7 @@ class URLField(CharField[_C]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_C, _StrOrPromise] | tuple[str, Iterable[tuple[_C, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_C, TextChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -973,7 +967,7 @@ class TextField(Field[_C | Combinable, _C], Generic[_C]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_C, _StrOrPromise] | tuple[str, Iterable[tuple[_C, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_C, TextChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -1002,7 +996,7 @@ class TextField(Field[_C | Combinable, _C], Generic[_C]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_C, _StrOrPromise] | tuple[str, Iterable[tuple[_C, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_C, TextChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -1035,7 +1029,7 @@ class BooleanField(Field[_B | Combinable, _B], Generic[_B]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_B, _StrOrPromise] | tuple[str, Iterable[tuple[_B, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_B, Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -1064,7 +1058,7 @@ class BooleanField(Field[_B | Combinable, _B], Generic[_B]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_B, _StrOrPromise] | tuple[str, Iterable[tuple[_B, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_B, Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -1096,7 +1090,7 @@ class IPAddressField(Field[_C | Combinable, _C], Generic[_C]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_C, _StrOrPromise] | tuple[str, Iterable[tuple[_C, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_C, TextChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -1124,7 +1118,7 @@ class IPAddressField(Field[_C | Combinable, _C], Generic[_C]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_C, _StrOrPromise] | tuple[str, Iterable[tuple[_C, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_C, TextChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -1159,7 +1153,7 @@ class GenericIPAddressField( unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_C, _StrOrPromise] | tuple[str, Iterable[tuple[_C, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_C, TextChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -1189,7 +1183,7 @@ class GenericIPAddressField( unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_C, _StrOrPromise] | tuple[str, Iterable[tuple[_C, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_C, TextChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -1228,7 +1222,7 @@ class DateField(DateTimeCheckMixin, Field[_DD | Combinable, _DD]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_DD, _StrOrPromise] | tuple[str, Iterable[tuple[_DD, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_DD, Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -1258,7 +1252,7 @@ class DateField(DateTimeCheckMixin, Field[_DD | Combinable, _DD]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_DD, _StrOrPromise] | tuple[str, Iterable[tuple[_DD, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_DD, Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -1295,7 +1289,7 @@ class TimeField(DateTimeCheckMixin, Field[_TM | Combinable, _TM], Generic[_TM]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_TM, _StrOrPromise] | tuple[str, Iterable[tuple[_TM, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_TM, Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -1325,7 +1319,7 @@ class TimeField(DateTimeCheckMixin, Field[_TM | Combinable, _TM], Generic[_TM]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_TM, _StrOrPromise] | tuple[str, Iterable[tuple[_TM, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_TM, Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -1362,7 +1356,7 @@ class DateTimeField(DateField[_DT]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_DT, _StrOrPromise] | tuple[str, Iterable[tuple[_DT, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_DT, Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -1392,7 +1386,7 @@ class DateTimeField(DateField[_DT]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_DT, _StrOrPromise] | tuple[str, Iterable[tuple[_DT, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_DT, Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -1424,7 +1418,7 @@ class UUIDField(Field[str | _U, _U], Generic[_U]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_U, _StrOrPromise] | tuple[str, Iterable[tuple[_U, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_U, Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -1452,7 +1446,7 @@ class UUIDField(Field[str | _U, _U], Generic[_U]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_U, _StrOrPromise] | tuple[str, Iterable[tuple[_U, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_U, Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -1492,7 +1486,7 @@ class FilePathField(Field[_C, _C], Generic[_C]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_C, _StrOrPromise] | tuple[str, Iterable[tuple[_C, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_C, TextChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -1525,7 +1519,7 @@ class FilePathField(Field[_C, _C], Generic[_C]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_C, _StrOrPromise] | tuple[str, Iterable[tuple[_C, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_C, TextChoices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -1557,7 +1551,7 @@ class BinaryField(Field[_BIN | bytearray | memoryview, _BIN], Generic[_BIN]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_BIN, _StrOrPromise] | tuple[str, Iterable[tuple[_BIN, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_BIN, Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -1585,7 +1579,7 @@ class BinaryField(Field[_BIN | bytearray | memoryview, _BIN], Generic[_BIN]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_BIN, _StrOrPromise] | tuple[str, Iterable[tuple[_BIN, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_BIN, Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -1617,7 +1611,7 @@ class DurationField(Field[_TD, _TD], Generic[_TD]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_TD, _StrOrPromise] | tuple[str, Iterable[tuple[_TD, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_TD, Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -1645,7 +1639,7 @@ class DurationField(Field[_TD, _TD], Generic[_TD]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_TD, _StrOrPromise] | tuple[str, Iterable[tuple[_TD, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_TD, Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., diff --git a/tests/pyright/test_choices.py b/tests/pyright/test_choices.py new file mode 100644 index 000000000..c254a940a --- /dev/null +++ b/tests/pyright/test_choices.py @@ -0,0 +1,83 @@ +from .base import run_pyright + + +def test_choices_accepts_a_mapping() -> None: + """A model declares choices as a dict, the way Django has accepted since 5.0. + + Field.choices already admitted a mapping through _ChoicesMapping, but every + per-field __new__ overload listed only the pair iterable, so the dict form + was rejected at the constructor. + """ + results = run_pyright( + """\ +from django.db import models + +class Foo(models.Model): + kind = models.CharField(max_length=8, choices={"a": "A", "b": "B"}) + size = models.IntegerField(choices={1: "one", 2: "two"}) +""" + ) + assert [r for r in results if r.type == "error"] == [] + + +def test_text_field_accepts_a_text_choices_class() -> None: + """A TextField takes a TextChoices class, exactly as a CharField does. + + CharField's overloads named type[TextChoices]; TextField's did not, so the + same enum was accepted on one string field and rejected on the other. + """ + results = run_pyright( + """\ +from django.db import models + +class Kind(models.TextChoices): + A = "a", "A" + +class Foo(models.Model): + body = models.TextField(choices=Kind) + slug = models.SlugField(choices=Kind) +""" + ) + assert [r for r in results if r.type == "error"] == [] + + +def test_choices_accepts_a_callable() -> None: + """A choices callable keeps a new enum member from needing a migration. + + Django serializes the reference rather than the values. Field.choices + allowed the callable; the overloads did not. + """ + results = run_pyright( + """\ +from django.db import models + +def flag_choices() -> list[tuple[str, str]]: + return [("a", "A")] + +class Foo(models.Model): + flag = models.CharField(max_length=64, choices=flag_choices) +""" + ) + assert [r for r in results if r.type == "error"] == [] + + +def test_integer_field_still_rejects_a_text_choices_class() -> None: + """Widening the overloads must not erase the precision they already had. + + An IntegerField's choices are IntegerChoices; handing it a TextChoices + class was an error before this change and stays one. + """ + results = run_pyright( + """\ +from django.db import models + +class Kind(models.TextChoices): + A = "a", "A" + +class Foo(models.Model): + size = models.IntegerField(choices=Kind) +""" + ) + errors = [r for r in results if r.type == "error"] + assert len(errors) == 1 + assert '"choices"' in errors[0].message From e8e4edb82da2f1a7603ece90b0399d932b4af7a8 Mon Sep 17 00:00:00 2001 From: Adam Hitchcock Date: Tue, 15 Sep 2026 13:43:33 -0700 Subject: [PATCH 2/4] Cover the choices= overloads outside fields/__init__.pyi too related, files, json, generated and the postgres ArrayField had the same pair-only choices parameter; they now use the alias as well. --- .../contrib/postgres/fields/array.pyi | 7 +-- django-stubs/db/models/fields/files.pyi | 7 ++- django-stubs/db/models/fields/generated.pyi | 8 +-- django-stubs/db/models/fields/json.pyi | 8 +-- django-stubs/db/models/fields/related.pyi | 15 +++--- tests/pyright/test_choices.py | 50 +++++++++++-------- 6 files changed, 55 insertions(+), 40 deletions(-) diff --git a/django-stubs/contrib/postgres/fields/array.pyi b/django-stubs/contrib/postgres/fields/array.pyi index cb8abebe8..3c9a5c996 100644 --- a/django-stubs/contrib/postgres/fields/array.pyi +++ b/django-stubs/contrib/postgres/fields/array.pyi @@ -2,8 +2,9 @@ from collections.abc import Callable, Iterable from typing import Any, Generic, Literal, TypeVar, overload from django.core.validators import _ValidatorCallable +from django.db.models import Choices from django.db.models.expressions import Combinable -from django.db.models.fields import Field, _ErrorMessagesMapping +from django.db.models.fields import Field, _ChoicesFor, _ErrorMessagesMapping from django.utils.functional import _StrOrPromise from .mixins import CheckFieldDefaultMixin @@ -37,7 +38,7 @@ class ArrayField(CheckFieldDefaultMixin, Field[_V | Combinable, _V], Generic[_V] unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[list[_V], str] | tuple[str, Iterable[tuple[list[_V], str]]]] = ..., + choices: _ChoicesFor[list[_V], Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_tablespace: str | None = ..., @@ -65,7 +66,7 @@ class ArrayField(CheckFieldDefaultMixin, Field[_V | Combinable, _V], Generic[_V] unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[list[_V], str] | tuple[str, Iterable[tuple[list[_V], str]]]] = ..., + choices: _ChoicesFor[list[_V], Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_tablespace: str | None = ..., diff --git a/django-stubs/db/models/fields/files.pyi b/django-stubs/db/models/fields/files.pyi index 303d7743c..9a5761716 100644 --- a/django-stubs/db/models/fields/files.pyi +++ b/django-stubs/db/models/fields/files.pyi @@ -5,11 +5,14 @@ from django.core.files.base import File from django.core.files.images import ImageFile from django.core.files.storage import FileSystemStorage, Storage from django.core.validators import _ValidatorCallable +from django.db.models import Choices from django.db.models.base import Model from django.db.models.fields import _GT, Field, _ErrorMessagesMapping from django.utils.functional import _StrOrPromise from typing_extensions import Self +from . import _ChoicesFor + class FieldFile(File): instance: Model = ... field: FileField = ... @@ -55,7 +58,7 @@ class FileField(Field[FileDescriptor, FileDescriptor]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_GT, _StrOrPromise] | tuple[str, Iterable[tuple[_GT, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_GT, Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -104,7 +107,7 @@ class ImageField(FileField): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_GT, _StrOrPromise] | tuple[str, Iterable[tuple[_GT, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_GT, Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., diff --git a/django-stubs/db/models/fields/generated.pyi b/django-stubs/db/models/fields/generated.pyi index b8d764980..20f42021f 100644 --- a/django-stubs/db/models/fields/generated.pyi +++ b/django-stubs/db/models/fields/generated.pyi @@ -2,12 +2,12 @@ from collections.abc import Iterable from typing import Any, Literal, TypeVar, overload from django.core.validators import _ValidatorCallable -from django.db.models import Expression, ForeignObjectRel +from django.db.models import Choices, Expression, ForeignObjectRel from django.db.models.expressions import Col, Combinable from django.utils.functional import _StrOrPromise from typing_extensions import Never -from . import Field, _ErrorMessagesMapping +from . import Field, _ChoicesFor, _ErrorMessagesMapping from .mixins import CheckFieldDefaultMixin _GT = TypeVar("_GT", bound=Any | None) @@ -39,7 +39,7 @@ class GeneratedField(CheckFieldDefaultMixin, Field[Never, _GT]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[Any, _StrOrPromise] | tuple[str, Iterable[tuple[Any, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[Any, Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_tablespace: str | None = ..., @@ -70,7 +70,7 @@ class GeneratedField(CheckFieldDefaultMixin, Field[Never, _GT]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[Any, _StrOrPromise] | tuple[str, Iterable[tuple[Any, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[Any, Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_tablespace: str | None = ..., diff --git a/django-stubs/db/models/fields/json.pyi b/django-stubs/db/models/fields/json.pyi index 48356cac4..53b4d5864 100644 --- a/django-stubs/db/models/fields/json.pyi +++ b/django-stubs/db/models/fields/json.pyi @@ -3,13 +3,13 @@ from collections.abc import Callable, Iterable from typing import Any, Literal, TypeVar, overload from django.core.validators import _ValidatorCallable -from django.db.models import lookups +from django.db.models import Choices, lookups from django.db.models.expressions import Combinable, Func from django.db.models.lookups import PostgresOperatorLookup, Transform from django.utils.functional import _StrOrPromise from typing_extensions import Self -from . import Field, _ErrorMessagesMapping +from . import Field, _ChoicesFor, _ErrorMessagesMapping from .mixins import CheckFieldDefaultMixin _A = TypeVar("_A", bound=Any | None) @@ -42,7 +42,7 @@ class JSONField(CheckFieldDefaultMixin, Field[_A | Combinable, _A]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[Any, _StrOrPromise] | tuple[str, Iterable[tuple[Any, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[Any, Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -72,7 +72,7 @@ class JSONField(CheckFieldDefaultMixin, Field[_A | Combinable, _A]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[Any, _StrOrPromise] | tuple[str, Iterable[tuple[Any, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[Any, Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., diff --git a/django-stubs/db/models/fields/related.pyi b/django-stubs/db/models/fields/related.pyi index 8a6395287..0e8848908 100644 --- a/django-stubs/db/models/fields/related.pyi +++ b/django-stubs/db/models/fields/related.pyi @@ -2,6 +2,7 @@ from collections.abc import Callable, Iterable, Sequence from typing import Any, Generic, Literal, Protocol, TypeAlias, TypeVar, overload from uuid import UUID +from django.db.models import Choices from django.db.models.base import Model from django.db.models.deletion import Collector from django.db.models.fields import _GT, _ST, Field @@ -20,6 +21,8 @@ from django.db.models.query_utils import PathInfo, Q from django.utils.functional import _StrOrPromise from typing_extensions import Self +from . import _ChoicesFor + class _DeleteProtocol(Protocol): def __call__( self, @@ -104,7 +107,7 @@ class ForeignObject(RelatedField[_M, _M], Generic[_M]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_M, _StrOrPromise] | tuple[str, Iterable[tuple[_M, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_M, Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -142,7 +145,7 @@ class ForeignObject(RelatedField[_M, _M], Generic[_M]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_M, _StrOrPromise] | tuple[str, Iterable[tuple[_M, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_M, Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -185,7 +188,7 @@ class ForeignKey(ForeignObject[_M], Generic[_M]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_M, _StrOrPromise] | tuple[str, Iterable[tuple[_M, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_M, Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -221,7 +224,7 @@ class ForeignKey(ForeignObject[_M], Generic[_M]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_M, _StrOrPromise] | tuple[str, Iterable[tuple[_M, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_M, Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -273,7 +276,7 @@ class OneToOneField(ForeignKey[_M], Generic[_M]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_M, _StrOrPromise] | tuple[str, Iterable[tuple[_M, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_M, Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -309,7 +312,7 @@ class OneToOneField(ForeignKey[_M], Generic[_M]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_M, _StrOrPromise] | tuple[str, Iterable[tuple[_M, _StrOrPromise]]]] = ..., + choices: _ChoicesFor[_M, Choices] = ..., help_text: _StrOrPromise = ..., db_column: str | None = ..., db_comment: str | None = ..., diff --git a/tests/pyright/test_choices.py b/tests/pyright/test_choices.py index c254a940a..1818ed486 100644 --- a/tests/pyright/test_choices.py +++ b/tests/pyright/test_choices.py @@ -2,12 +2,7 @@ def test_choices_accepts_a_mapping() -> None: - """A model declares choices as a dict, the way Django has accepted since 5.0. - - Field.choices already admitted a mapping through _ChoicesMapping, but every - per-field __new__ overload listed only the pair iterable, so the dict form - was rejected at the constructor. - """ + """A model declares choices as a dict, the way Django has accepted since 5.0.""" results = run_pyright( """\ from django.db import models @@ -21,11 +16,7 @@ class Foo(models.Model): def test_text_field_accepts_a_text_choices_class() -> None: - """A TextField takes a TextChoices class, exactly as a CharField does. - - CharField's overloads named type[TextChoices]; TextField's did not, so the - same enum was accepted on one string field and rejected on the other. - """ + """A TextField takes a TextChoices class, exactly as a CharField does.""" results = run_pyright( """\ from django.db import models @@ -42,11 +33,7 @@ class Foo(models.Model): def test_choices_accepts_a_callable() -> None: - """A choices callable keeps a new enum member from needing a migration. - - Django serializes the reference rather than the values. Field.choices - allowed the callable; the overloads did not. - """ + """A choices callable keeps a new enum member from needing a migration.""" results = run_pyright( """\ from django.db import models @@ -62,11 +49,7 @@ class Foo(models.Model): def test_integer_field_still_rejects_a_text_choices_class() -> None: - """Widening the overloads must not erase the precision they already had. - - An IntegerField's choices are IntegerChoices; handing it a TextChoices - class was an error before this change and stays one. - """ + """Widening the overloads must not erase the precision they already had.""" results = run_pyright( """\ from django.db import models @@ -81,3 +64,28 @@ class Foo(models.Model): errors = [r for r in results if r.type == "error"] assert len(errors) == 1 assert '"choices"' in errors[0].message + + +def test_choices_widened_outside_fields_init() -> None: + """The same three spellings work on JSONField, FileField, ForeignKey and ArrayField.""" + results = run_pyright( + """\ +from django.contrib.postgres.fields import ArrayField +from django.db import models + +class Kind(models.TextChoices): + A = "a", "A" + +def opts() -> list[tuple[str, str]]: + return [("a", "A")] + +class Thing(models.Model): ... + +class Foo(models.Model): + data = models.JSONField(choices={"a": "A"}) + doc = models.FileField(choices=Kind) + ref = models.ForeignKey(Thing, on_delete=models.CASCADE, choices=opts) + tags = ArrayField(models.CharField(max_length=8), choices=[(["a", "b"], "AB")]) +""" + ) + assert [r for r in results if r.type == "error"] == [] From f0fb54099314b9d336afbadd215bd86c23ddb217 Mon Sep 17 00:00:00 2001 From: Adam Hitchcock Date: Tue, 15 Sep 2026 13:57:23 -0700 Subject: [PATCH 3/4] Exercise the GeneratedField and ArrayField overloads in the test Both were widened but the test only used spellings upstream already accepted for them. --- tests/pyright/test_choices.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/pyright/test_choices.py b/tests/pyright/test_choices.py index 1818ed486..2c1471ae9 100644 --- a/tests/pyright/test_choices.py +++ b/tests/pyright/test_choices.py @@ -85,7 +85,8 @@ class Foo(models.Model): data = models.JSONField(choices={"a": "A"}) doc = models.FileField(choices=Kind) ref = models.ForeignKey(Thing, on_delete=models.CASCADE, choices=opts) - tags = ArrayField(models.CharField(max_length=8), choices=[(["a", "b"], "AB")]) + tags = ArrayField(models.CharField(max_length=8), choices=lambda: [(["a", "b"], "AB")]) + total = models.GeneratedField(expression=models.F("a") + models.F("b"), output_field=models.IntegerField(), db_persist=True, choices={1: "one"}) """ ) assert [r for r in results if r.type == "error"] == [] From 5ea5ee0974c1d50a04822fb8bda7d90d0647089f Mon Sep 17 00:00:00 2001 From: Adam Hitchcock Date: Tue, 15 Sep 2026 14:06:30 -0700 Subject: [PATCH 4/4] Name GeneratedField in the outside-fields test docstring --- tests/pyright/test_choices.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/pyright/test_choices.py b/tests/pyright/test_choices.py index 2c1471ae9..15993a4cc 100644 --- a/tests/pyright/test_choices.py +++ b/tests/pyright/test_choices.py @@ -67,7 +67,7 @@ class Foo(models.Model): def test_choices_widened_outside_fields_init() -> None: - """The same three spellings work on JSONField, FileField, ForeignKey and ArrayField.""" + """The widened spellings work on JSONField, FileField, ForeignKey, ArrayField and GeneratedField.""" results = run_pyright( """\ from django.contrib.postgres.fields import ArrayField