Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions django-stubs/db/models/fields/related.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ from django.db.models.fields.reverse_related import OneToOneRel as OneToOneRel
from django.db.models.query_utils import FilteredRelation, PathInfo, Q
from django.db.models.sql.where import WhereNode
from django.forms.widgets import Widget
from django.utils.choices import _Choices
from django.utils.choices import _ChoicesInput
from django.utils.functional import _StrOrPromise, cached_property
from typing_extensions import TypeVar, Unpack, override

Expand Down Expand Up @@ -298,7 +298,7 @@ class ManyToManyField(RelatedField, Generic[_To, _Through]):
unique_for_date: str | None = ...,
unique_for_month: str | None = ...,
unique_for_year: str | None = ...,
choices: _Choices | None = ...,
choices: _ChoicesInput | None = ...,
help_text: _StrOrPromise = ...,
db_column: str | None = ...,
db_tablespace: str | None = ...,
Expand Down
65 changes: 65 additions & 0 deletions tests/assert_type/db/models/fields/test_choices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from django.contrib.gis.db.models import PointField
from django.contrib.postgres.fields import ArrayField
from django.db import models

if TYPE_CHECKING:
from collections.abc import Mapping, Sequence


class Kind(models.TextChoices):
A = "a", "A"


class Size(models.IntegerChoices):
SMALL = 1, "Small"


def pairs() -> Sequence[tuple[str, str]]:
return [("a", "A")]


def mapping() -> Mapping[str, str]:
return {"a": "A"}


def grouped_pairs() -> Sequence[tuple[str, Sequence[tuple[str, str]]]]:
return [("Group", pairs())]


def grouped_mapping() -> Mapping[str, Mapping[str, str]]:
return {"Group": mapping()}


def choices_class() -> type[models.Choices]:
return Kind


# ty drops `type[Choices]` from the union: https://github.com/astral-sh/ty/issues/4551
def field_choices() -> None:
models.CharField(choices=Kind) # ty: ignore[invalid-argument-type]
models.IntegerField(choices=Size) # ty: ignore[invalid-argument-type]
models.CharField(choices=pairs())
models.CharField(choices=mapping())
models.CharField(choices=grouped_pairs())
models.CharField(choices=grouped_mapping())
models.CharField(choices=pairs)
models.CharField(choices=mapping)
models.CharField(choices=grouped_pairs)
models.CharField(choices=grouped_mapping)
models.CharField(choices=choices_class)
models.CharField(choices="ab") # type: ignore[arg-type] # pyright: ignore[reportArgumentType] # pyrefly: ignore[bad-argument-type] # ty: ignore[invalid-argument-type]


# Fields redeclaring `choices` accept the same input.
def redeclared_field_choices() -> None:
models.GeneratedField(expression=models.Value("a"), output_field=models.CharField(), db_persist=False, choices=Kind) # ty: ignore[invalid-argument-type]
models.FileField(choices=Kind) # ty: ignore[invalid-argument-type]
models.ForeignKey("auth.User", models.CASCADE, choices=mapping())
models.OneToOneField("auth.User", models.CASCADE, choices=mapping)
models.ManyToManyField("auth.User", choices=Kind) # ty: ignore[invalid-argument-type]
ArrayField(models.CharField(), choices=Kind) # ty: ignore[invalid-argument-type]
PointField(choices=grouped_pairs)
157 changes: 0 additions & 157 deletions tests/typecheck/db/models/test_fields_choices.yml

This file was deleted.

Loading