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
7 changes: 4 additions & 3 deletions django-stubs/contrib/postgres/fields/array.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = ...,
Expand Down Expand Up @@ -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 = ...,
Expand Down
128 changes: 61 additions & 67 deletions django-stubs/db/models/fields/__init__.pyi

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions django-stubs/db/models/fields/files.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ...
Expand Down Expand Up @@ -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 = ...,
Expand Down Expand Up @@ -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 = ...,
Expand Down
8 changes: 4 additions & 4 deletions django-stubs/db/models/fields/generated.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 = ...,
Expand Down Expand Up @@ -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 = ...,
Expand Down
8 changes: 4 additions & 4 deletions django-stubs/db/models/fields/json.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 = ...,
Expand Down Expand Up @@ -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 = ...,
Expand Down
15 changes: 9 additions & 6 deletions django-stubs/db/models/fields/related.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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 = ...,
Expand Down Expand Up @@ -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 = ...,
Expand Down Expand Up @@ -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 = ...,
Expand Down Expand Up @@ -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 = ...,
Expand Down Expand Up @@ -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 = ...,
Expand Down Expand Up @@ -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 = ...,
Expand Down
92 changes: 92 additions & 0 deletions tests/pyright/test_choices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
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."""
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."""
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."""
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."""
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


def test_choices_widened_outside_fields_init() -> None:
"""The widened spellings work on JSONField, FileField, ForeignKey, ArrayField and GeneratedField."""
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=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"] == []
Loading