Skip to content

Commit

Permalink
Support more use cases of gettext_lazy. (#1233)
Browse files Browse the repository at this point in the history
* Replace str with _StrOrPromise where the latter is expected.

Fixes #1231

Signed-off-by: Zixuan James Li <[email protected]>

* Support lazy strings for _FieldsetSpec.

Signed-off-by: Zixuan James Li <[email protected]>

Signed-off-by: Zixuan James Li <[email protected]>
  • Loading branch information
PIG208 authored Nov 6, 2022
1 parent f4cf7f2 commit a59eacd
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion django-stubs/contrib/admin/options.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class _FieldOpts(_OptionalFieldOpts, total=True):
# Workaround for mypy issue, a Sequence type should be preferred here.
# https://github.com/python/mypy/issues/8921
# _FieldsetSpec = Sequence[Tuple[Optional[str], _FieldOpts]]
_FieldsetSpec = _ListOrTuple[Tuple[Optional[str], _FieldOpts]]
_FieldsetSpec = _ListOrTuple[Tuple[Optional[_StrOrPromise], _FieldOpts]]
_ListFilterT = Union[
Type[ListFilter],
Field,
Expand Down
5 changes: 3 additions & 2 deletions django-stubs/contrib/auth/mixins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ from typing import Any, Callable, Optional, Sequence

from django import http
from django.http.response import HttpResponseBase, HttpResponseRedirect
from django.utils.functional import _StrOrPromise

class AccessMixin:
login_url: Any = ...
permission_denied_message: str = ...
permission_denied_message: _StrOrPromise = ...
raise_exception: bool = ...
redirect_field_name: Any = ...
def get_login_url(self) -> str: ...
def get_permission_denied_message(self) -> str: ...
def get_permission_denied_message(self) -> _StrOrPromise: ...
def get_redirect_field_name(self) -> str: ...
def handle_no_permission(self) -> HttpResponseRedirect: ...

Expand Down
5 changes: 3 additions & 2 deletions django-stubs/contrib/messages/views.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ from typing import Dict

from django.forms.forms import BaseForm
from django.http.response import HttpResponse
from django.utils.functional import _StrOrPromise

class SuccessMessageMixin:
success_message: str = ...
success_message: _StrOrPromise = ...
def form_valid(self, form: BaseForm) -> HttpResponse: ...
def get_success_message(self, cleaned_data: Dict[str, str]) -> str: ...
def get_success_message(self, cleaned_data: Dict[str, str]) -> _StrOrPromise: ...
2 changes: 2 additions & 0 deletions tests/typecheck/contrib/admin/test_options.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from django.db.models.options import Options
from django.http.request import HttpRequest
from django.db.models.query import QuerySet
from django.utils.translation import gettext_lazy as _
def an_action(modeladmin: admin.ModelAdmin, request: HttpRequest, queryset: QuerySet) -> None:
pass
Expand All @@ -29,6 +30,7 @@
fieldsets = [
(None, {"fields": ["a", "b"]}),
("group", {"fields": ("c",), "classes": ("a",), "description": "foo"}),
(_("lazy"), {"fields": ["bar"]})
]
form = ModelForm
filter_vertical = ("fields",)
Expand Down

0 comments on commit a59eacd

Please sign in to comment.