Skip to content

Various fixes for Django 4.2 #1536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from 10 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
2 changes: 2 additions & 0 deletions django-stubs/contrib/postgres/lookups.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ class HasAnyKeys(HasKeys): ...
class Unaccent(Transform): ...
class SearchLookup(SearchVectorExact): ...
class TrigramSimilar(PostgresOperatorLookup): ...
class TrigramWordSimilar(PostgresOperatorLookup): ...
class TrigramStrictWordSimilar(PostgresOperatorLookup): ...
2 changes: 1 addition & 1 deletion django-stubs/contrib/postgres/search.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class SearchQuery(SearchQueryCombinable, Func): # type: ignore
invert: bool = ...,
search_type: str = ...,
) -> None: ...
def __invert__(self: Self) -> Self: ...
def __invert__(self: Self) -> Self: ... # type: ignore[override]

class CombinedSearchQuery(SearchQueryCombinable, CombinedExpression): # type: ignore
def __init__(
Expand Down
17 changes: 13 additions & 4 deletions django-stubs/db/models/expressions.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime
from collections.abc import Callable, Iterable, Iterator, Sequence
from decimal import Decimal
from typing import Any, Literal
from typing import Any, Generic, Literal, TypeVar

from _typeshed import Self
from django.db.backends.base.base import BaseDatabaseWrapper
Expand Down Expand Up @@ -41,6 +41,7 @@ class Combinable:
def bitand(self, other: int) -> CombinedExpression: ...
def bitleftshift(self, other: int) -> CombinedExpression: ...
def bitrightshift(self, other: int) -> CombinedExpression: ...
def __xor__(self, other: Combinable | Q) -> Q: ...
def bitxor(self, other: int) -> CombinedExpression: ...
def __or__(self, other: Combinable | Q) -> Q: ...
def bitor(self, other: int) -> CombinedExpression: ...
Expand All @@ -52,6 +53,8 @@ class Combinable:
def __rpow__(self, other: _Numeric | Combinable) -> CombinedExpression: ...
def __rand__(self, other: Any) -> Combinable: ...
def __ror__(self, other: Any) -> Combinable: ...
def __rxor__(self, other: Any) -> Combinable: ...
def __invert__(self) -> NegatedExpression[Combinable]: ...

class BaseExpression:
is_summary: bool
Expand Down Expand Up @@ -199,8 +202,15 @@ class ExpressionList(Func):

class OrderByList(Func): ...

class ExpressionWrapper(Expression):
def __init__(self, expression: Q | Combinable, output_field: Field) -> None: ...
_E = TypeVar("_E", bound=Q | Combinable)

class ExpressionWrapper(Expression, Generic[_E]):
def __init__(self, expression: _E, output_field: Field) -> None: ...
expression: _E

class NegatedExpression(ExpressionWrapper[_E]):
def __init__(self, expression: _E) -> None: ...
def __invert__(self) -> _E: ... # type: ignore[override]

class When(Expression):
template: str
Expand All @@ -226,7 +236,6 @@ class Subquery(BaseExpression, Combinable):

class Exists(Subquery):
def __init__(self, queryset: Query | QuerySet, **kwargs: Any) -> None: ...
def __invert__(self) -> Exists: ...

class OrderBy(Expression):
template: str
Expand Down
9 changes: 9 additions & 0 deletions django-stubs/db/models/fields/json.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json
from collections.abc import Callable
from typing import Any

from _typeshed import Self
from django.db.backends.base.base import BaseDatabaseWrapper
from django.db.models import lookups
from django.db.models.lookups import PostgresOperatorLookup, Transform
Expand All @@ -27,6 +29,7 @@ class ContainedBy(PostgresOperatorLookup): ...

class HasKeyLookup(PostgresOperatorLookup):
logical_operator: str | None
def compile_json_path_final_key(self, key_transform: Any) -> str: ...

class HasKey(HasKeyLookup):
postgres_operator: str
Expand All @@ -39,7 +42,9 @@ class HasAnyKeys(HasKeys):
postgres_operator: str
logical_operator: str

class HasKeyOrArrayIndex(HasKey): ...
class JSONExact(lookups.Exact): ...
class JSONIContains(CaseInsensitiveMixin, lookups.IContains): ...

class KeyTransform(Transform):
key_name: str
Expand All @@ -51,6 +56,10 @@ class KeyTransform(Transform):
class KeyTextTransform(KeyTransform):
postgres_operator: str
postgres_nested_operator: str
@classmethod
def from_lookup(cls: type[Self], lookup: str) -> Self: ...

KT: Callable[[str], KeyTextTransform]

class KeyTransformTextLookupMixin:
def __init__(self, key_transform: Any, *args: Any, **kwargs: Any) -> None: ...
Expand Down
6 changes: 6 additions & 0 deletions django-stubs/forms/models.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class ModelFormOptions(Generic[_M]):
help_texts: _HelpTexts | None
error_messages: _ErrorMessages | None
field_classes: dict[str, type[Field]] | None
formfield_callback: _FormFieldCallback | None
def __init__(self, options: type | None = ...) -> None: ...

class ModelFormMetaclass(DeclarativeFieldsMetaclass): ...
Expand Down Expand Up @@ -116,6 +117,7 @@ _ModelFormT = TypeVar("_ModelFormT", bound=ModelForm)

class BaseModelFormSet(Generic[_M, _ModelFormT], BaseFormSet[_ModelFormT]):
model: type[_M]
edit_only: bool
unique_fields: Collection[str]
queryset: QuerySet[_M] | None
initial_extra: Sequence[dict[str, Any]] | None
Expand Down Expand Up @@ -172,6 +174,8 @@ def modelformset_factory(
field_classes: Mapping[str, type[Field]] | None = ...,
absolute_max: int | None = ...,
can_delete_extra: bool = ...,
renderer: BaseRenderer | None = ...,
edit_only: bool = ...,
) -> type[BaseModelFormSet[_M, _ModelFormT]]: ...

class BaseInlineFormSet(Generic[_M, _ParentM, _ModelFormT], BaseModelFormSet[_M, _ModelFormT]):
Expand Down Expand Up @@ -220,6 +224,8 @@ def inlineformset_factory(
field_classes: Mapping[str, type[Field]] | None = ...,
absolute_max: int | None = ...,
can_delete_extra: bool = ...,
renderer: BaseRenderer | None = ...,
edit_only: bool = ...,
) -> type[BaseInlineFormSet[_M, _ParentM, _ModelFormT]]: ...

class InlineForeignKeyField(Field):
Expand Down
3 changes: 2 additions & 1 deletion django-stubs/utils/html.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections.abc import Iterable
from html.parser import HTMLParser
from json import JSONEncoder
from re import Pattern
from typing import Any

Expand All @@ -14,7 +15,7 @@ simple_url_2_re: Pattern[str]

def escape(text: Any) -> SafeString: ...
def escapejs(value: Any) -> SafeString: ...
def json_script(value: Any, element_id: str) -> SafeString: ...
def json_script(value: Any, element_id: str | None = ..., encoder: type[JSONEncoder] | None = ...) -> SafeString: ...
def conditional_escape(text: Any) -> str: ...
def format_html(format_string: str, *args: Any, **kwargs: Any) -> SafeString: ...
def format_html_join(sep: str, format_string: str, args_generator: Iterable[Iterable[Any]]) -> SafeString: ...
Expand Down
3 changes: 3 additions & 0 deletions django_stubs_ext/django_stubs_ext/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.contrib.syndication.views import Feed
from django.core.files.utils import FileProxyMixin
from django.core.paginator import Paginator
from django.db.models.expressions import ExpressionWrapper, NegatedExpression
Copy link
Collaborator

@intgr intgr Jun 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oof, this import will fail with Django 4.1 and earlier, since NegatedExpression class only appeared in 4.2.

Also we clearly have a blind spot in our CI since it didn't detect this.

@sobolevn Any ideas how to handle backwards compatibility in django-stubs-ext monkeypatching?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR #1552 will add CI coverage for such surprises.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway I'd remove NegatedExpression monkeypatching for now, to unblock this PR again.

I've created issue #1555 to solve it in the future.

from django.db.models.fields import Field
from django.db.models.fields.related import ForeignKey
from django.db.models.lookups import Lookup
Expand Down Expand Up @@ -66,6 +67,8 @@ def __repr__(self) -> str:
MPGeneric(FileProxyMixin),
MPGeneric(Lookup),
MPGeneric(BaseConnectionHandler),
MPGeneric(ExpressionWrapper),
MPGeneric(NegatedExpression),
# These types do have native `__class_getitem__` method since django 3.1:
MPGeneric(QuerySet, (3, 1)),
MPGeneric(BaseManager, (3, 1)),
Expand Down