Skip to content

Commit

Permalink
cherry-pick django 5.1 fixes (#25)
Browse files Browse the repository at this point in the history
* add cache_name for django 5.1 (typeddjango#2365)

* 5.1: Deprecate CheckConstraint.check (typeddjango#2331)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

---------

Co-authored-by: Anthony Sottile <asottile@umich.edu>
Co-authored-by: q0w <43147888+q0w@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored Sep 11, 2024
1 parent b5bf9aa commit a41c425
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
1 change: 0 additions & 1 deletion django-stubs/contrib/contenttypes/fields.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class GenericForeignKey(FieldCacheMixin):
def get_filter_kwargs_for_object(self, obj: Model) -> dict[str, ContentType | None]: ...
def get_forward_related_filter(self, obj: Model) -> dict[str, int]: ...
def check(self, **kwargs: Any) -> list[CheckMessage]: ...
def get_cache_name(self) -> str: ...
def get_content_type(
self, obj: Model | None = ..., id: int | None = ..., using: str | None = ..., model: type[Model] | None = ...
) -> ContentType: ...
Expand Down
17 changes: 15 additions & 2 deletions django-stubs/db/models/constraints.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,26 @@ class BaseConstraint:

class CheckConstraint(BaseConstraint):
check: Q | BaseExpression
condition: Q | BaseExpression

@overload
@deprecated("The check keyword argument is deprecated in favor of condition and will be removed in Django 6.0")
def __init__(
self,
*,
name: str,
check: Q | BaseExpression,
violation_error_code: str | None = None,
violation_error_message: _StrOrPromise | None = None,
) -> None: ...
@overload
def __init__(
self,
*,
name: str,
violation_error_code: str | None = ...,
violation_error_message: _StrOrPromise | None = ...,
condition: Q | BaseExpression,
violation_error_code: str | None = None,
violation_error_message: _StrOrPromise | None = None,
) -> None: ...

class UniqueConstraint(BaseConstraint):
Expand Down
3 changes: 3 additions & 0 deletions django-stubs/db/models/fields/mixins.pyi
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from typing import Any

from django.db.models.base import Model
from django.utils.functional import cached_property

NOT_PROVIDED: Any

class FieldCacheMixin:
def get_cache_name(self) -> str: ...
@cached_property
def cache_name(self) -> str: ...
def get_cached_value(self, instance: Model, default: Any = ...) -> Model | None: ...
def is_cached(self, instance: Model) -> bool: ...
def set_cached_value(self, instance: Model, value: Model | None) -> None: ...
Expand Down
2 changes: 1 addition & 1 deletion tests/assert_type/db/models/test_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
name="unique_mess",
)

CheckConstraint(name="less_than_constraint", check=LessThan[Any](F("months"), 1))
CheckConstraint(name="less_than_constraint", check=LessThan[Any](F("months"), 1)) # pyright: ignore[reportDeprecated]

0 comments on commit a41c425

Please sign in to comment.