From 1ede371430c5fff809a9237035a2d0daadb6b704 Mon Sep 17 00:00:00 2001 From: John Parton Date: Thu, 15 Aug 2024 16:21:05 -0500 Subject: [PATCH 1/2] KT is a function, not an instance. (#260) KT shortcut is actually an alias for KeyTextTransform.from_lookup class method. Annotated classmethod and then assigned alias. --- django-stubs/db/models/fields/json.pyi | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/django-stubs/db/models/fields/json.pyi b/django-stubs/db/models/fields/json.pyi index 048d51d28..b2a078962 100644 --- a/django-stubs/db/models/fields/json.pyi +++ b/django-stubs/db/models/fields/json.pyi @@ -1,7 +1,7 @@ import json from collections.abc import Callable, Iterable from typing import Any, TypeVar, overload -from typing_extensions import Literal +from typing_extensions import Literal, Self from django.db.models import lookups from django.db.models.expressions import Combinable @@ -135,6 +135,8 @@ class KeyTransform(Transform): class KeyTextTransform(KeyTransform): postgres_operator: str = ... postgres_nested_operator: str = ... + @classmethod + def from_lookup(cls, lookup: str) -> Self: ... class KeyTransformTextLookupMixin: def __init__(self, key_transform: Any, *args: Any, **kwargs: Any) -> None: ... @@ -187,4 +189,4 @@ class KeyTransformFactory: def __init__(self, key_name: Any) -> None: ... def __call__(self, *args: Any, **kwargs: Any) -> Any: ... -KT: KeyTextTransform = ... +KT = KeyTextTransform.from_lookup From 79fd3f9cc91c54f24de260c30c362defc1ae0092 Mon Sep 17 00:00:00 2001 From: John Parton Date: Thu, 15 Aug 2024 16:22:14 -0500 Subject: [PATCH 2/2] Allow text choices being directly assigned as choices. (#261) Allow CharField(choices=...) to be a TextChoices class. Changed in Django 5.0, see note here: https://docs.djangoproject.com/en/5.0/ref/models/fields/#s-enumeration-types --- django-stubs/db/models/fields/__init__.pyi | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/django-stubs/db/models/fields/__init__.pyi b/django-stubs/db/models/fields/__init__.pyi index 89cc66d3e..4cb1af1d8 100644 --- a/django-stubs/db/models/fields/__init__.pyi +++ b/django-stubs/db/models/fields/__init__.pyi @@ -8,7 +8,7 @@ from typing_extensions import Literal, Self from django.core.checks import CheckMessage from django.core.exceptions import FieldDoesNotExist as FieldDoesNotExist -from django.db.models import Model +from django.db.models import Model, TextChoices from django.db.models.expressions import Col, Combinable from django.db.models.query_utils import RegisterLookupMixin from django.forms import Widget @@ -679,7 +679,8 @@ class CharField(Generic[_C], Field[_C | Combinable, _C]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_C, str] | tuple[str, Iterable[tuple[_C, str]]]] = ..., + choices: Iterable[tuple[_C, str] | tuple[str, Iterable[tuple[_C, str]]]] + | type[TextChoices] = ..., help_text: str = ..., db_column: str | None = ..., db_comment: str | None = ..., @@ -706,7 +707,8 @@ class CharField(Generic[_C], Field[_C | Combinable, _C]): unique_for_date: str | None = ..., unique_for_month: str | None = ..., unique_for_year: str | None = ..., - choices: Iterable[tuple[_C, str] | tuple[str, Iterable[tuple[_C, str]]]] = ..., + choices: Iterable[tuple[_C, str] | tuple[str, Iterable[tuple[_C, str]]]] + | type[TextChoices] = ..., help_text: str = ..., db_column: str | None = ..., db_comment: str | None = ...,