Skip to content

Commit

Permalink
Broaden type annotation for verbose_name(_plural) to accept lazystr. (#…
Browse files Browse the repository at this point in the history
…1139)

* Broaden type annotation for verbose_name(_plural) to accept lazystr.

Fixes #1137.

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

* Broaden type annotation for help_text to accept lazystr.

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

* Broaden type annotation for ValidationError to accept lazystr.

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

* Broaden type annotation for label to accept lazystr.

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

* Add StrPromise and StrOrPromise aliases to django_stubs_ext.

We make StrPromise and StrOrPromise available via django_stubs_ext so
that conditional imports with TYPE_CHECKING is not required.
These aliases fall back to Promise or Union[str, Promise]
when not TYPE_CHECKING.

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

Signed-off-by: Zixuan James Li <[email protected]>
  • Loading branch information
PIG208 authored Sep 19, 2022
1 parent a2a3543 commit 9a41aa6
Show file tree
Hide file tree
Showing 19 changed files with 138 additions and 104 deletions.
3 changes: 2 additions & 1 deletion django-stubs/apps/config.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ from typing import Dict, Iterator, Optional, Type

from django.apps.registry import Apps
from django.db.models.base import Model
from django.utils.functional import _StrOrPromise

MODELS_MODULE_NAME: str

Expand All @@ -11,7 +12,7 @@ class AppConfig:
module: Optional[types.ModuleType] = ...
apps: Optional[Apps] = ...
label: str = ...
verbose_name: str = ...
verbose_name: _StrOrPromise = ...
path: str = ...
models_module: Optional[str] = ...
models: Dict[str, Type[Model]] = ...
Expand Down
5 changes: 3 additions & 2 deletions django-stubs/contrib/admin/options.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ from django.http.response import HttpResponse, HttpResponseRedirect, JsonRespons
from django.template.response import _TemplateForResponseT
from django.urls.resolvers import URLPattern
from django.utils.datastructures import _ListOrTuple
from django.utils.functional import _StrOrPromise
from django.utils.safestring import SafeString
from typing_extensions import Literal, TypedDict

Expand Down Expand Up @@ -296,8 +297,8 @@ class InlineModelAdmin(Generic[_ChildModelT, _ParentModelT], BaseModelAdmin[_Chi
min_num: Optional[int] = ...
max_num: Optional[int] = ...
template: str = ...
verbose_name: Optional[str] = ...
verbose_name_plural: Optional[str] = ...
verbose_name: Optional[_StrOrPromise] = ...
verbose_name_plural: Optional[_StrOrPromise] = ...
can_delete: bool = ...
show_change_link: bool = ...
classes: Optional[Sequence[str]] = ...
Expand Down
4 changes: 2 additions & 2 deletions django-stubs/contrib/admin/sites.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ from django.http.request import HttpRequest
from django.http.response import HttpResponse
from django.template.response import TemplateResponse
from django.urls import URLPattern, URLResolver
from django.utils.functional import LazyObject
from django.utils.functional import LazyObject, _StrOrPromise

if sys.version_info >= (3, 9):
from weakref import WeakSet
Expand Down Expand Up @@ -77,7 +77,7 @@ class AdminSite:
def i18n_javascript(self, request: HttpRequest, extra_context: Optional[Dict[str, Any]] = ...) -> HttpResponse: ...
def logout(self, request: HttpRequest, extra_context: Optional[Dict[str, Any]] = ...) -> TemplateResponse: ...
def login(self, request: HttpRequest, extra_context: Optional[Dict[str, Any]] = ...) -> HttpResponse: ...
def _build_app_dict(self, request: HttpRequest, label: Optional[str] = ...) -> Dict[str, Any]: ...
def _build_app_dict(self, request: HttpRequest, label: Optional[_StrOrPromise] = ...) -> Dict[str, Any]: ...
def get_app_list(self, request: HttpRequest) -> List[Any]: ...
def index(self, request: HttpRequest, extra_context: Optional[Dict[str, Any]] = ...) -> TemplateResponse: ...
def app_index(
Expand Down
9 changes: 7 additions & 2 deletions django-stubs/contrib/admin/widgets.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ from django.db.models.fields import _FieldChoices
from django.db.models.fields.reverse_related import ForeignObjectRel, ManyToManyRel, ManyToOneRel
from django.forms.models import ModelChoiceIterator
from django.forms.widgets import Media, _OptAttrs
from django.utils.functional import _StrOrPromise

class FilteredSelectMultiple(forms.SelectMultiple):
verbose_name: str = ...
verbose_name: _StrOrPromise = ...
is_stacked: bool = ...
def __init__(
self, verbose_name: str, is_stacked: bool, attrs: Optional[_OptAttrs] = ..., choices: _FieldChoices = ...
self,
verbose_name: _StrOrPromise,
is_stacked: bool,
attrs: Optional[_OptAttrs] = ...,
choices: _FieldChoices = ...,
) -> None: ...

class AdminDateWidget(forms.DateInput):
Expand Down
9 changes: 5 additions & 4 deletions django-stubs/contrib/gis/db/models/fields.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ from typing import Any, Iterable, NamedTuple, Optional, Tuple, TypeVar, Union

from django.core.validators import _ValidatorCallable
from django.db.models.fields import Field, _ErrorMessagesT, _FieldChoices
from django.utils.functional import _StrOrPromise

# __set__ value type
_ST = TypeVar("_ST")
Expand All @@ -19,7 +20,7 @@ def get_srid_info(srid: int, connection: Any) -> SRIDCacheEntry: ...
class BaseSpatialField(Field[_ST, _GT]):
def __init__(
self,
verbose_name: Optional[Union[str, bytes]] = ...,
verbose_name: Optional[Union[_StrOrPromise, bytes]] = ...,
srid: int = ...,
spatial_index: bool = ...,
*,
Expand All @@ -38,7 +39,7 @@ class BaseSpatialField(Field[_ST, _GT]):
unique_for_month: Optional[str] = ...,
unique_for_year: Optional[str] = ...,
choices: Optional[_FieldChoices] = ...,
help_text: str = ...,
help_text: _StrOrPromise = ...,
db_column: Optional[str] = ...,
db_tablespace: Optional[str] = ...,
validators: Iterable[_ValidatorCallable] = ...,
Expand All @@ -65,7 +66,7 @@ class GeometryField(BaseSpatialField):
geography: Any = ...
def __init__(
self,
verbose_name: Optional[Union[str, bytes]] = ...,
verbose_name: Optional[Union[_StrOrPromise, bytes]] = ...,
dim: int = ...,
geography: bool = ...,
*,
Expand All @@ -88,7 +89,7 @@ class GeometryField(BaseSpatialField):
unique_for_month: Optional[str] = ...,
unique_for_year: Optional[str] = ...,
choices: Optional[_FieldChoices] = ...,
help_text: str = ...,
help_text: _StrOrPromise = ...,
db_column: Optional[str] = ...,
db_tablespace: Optional[str] = ...,
validators: Iterable[_ValidatorCallable] = ...,
Expand Down
3 changes: 2 additions & 1 deletion django-stubs/contrib/postgres/fields/array.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ from django.db.models import Field, Transform
from django.db.models.expressions import Combinable
from django.db.models.fields import _ErrorMessagesT, _FieldChoices
from django.db.models.fields.mixins import CheckFieldDefaultMixin
from django.utils.functional import _StrOrPromise

# __set__ value type
_ST = TypeVar("_ST")
Expand Down Expand Up @@ -42,7 +43,7 @@ class ArrayField(CheckFieldDefaultMixin, Field[_ST, _GT]):
unique_for_month: Optional[str] = ...,
unique_for_year: Optional[str] = ...,
choices: Optional[_FieldChoices] = ...,
help_text: str = ...,
help_text: _StrOrPromise = ...,
db_column: Optional[str] = ...,
db_tablespace: Optional[str] = ...,
validators: Iterable[_ValidatorCallable] = ...,
Expand Down
5 changes: 3 additions & 2 deletions django-stubs/core/cache/backends/db.pyi
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from typing import Any, Dict

from django.core.cache.backends.base import BaseCache
from django.utils.functional import _StrOrPromise

class Options:
db_table: str = ...
app_label: str = ...
model_name: str = ...
verbose_name: str = ...
verbose_name_plural: str = ...
verbose_name: _StrOrPromise = ...
verbose_name_plural: _StrOrPromise = ...
object_name: str = ...
abstract: bool = ...
managed: bool = ...
Expand Down
3 changes: 2 additions & 1 deletion django-stubs/core/exceptions.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Any, Dict, Iterator, List, Optional, Tuple, Union

from django.utils.functional import _StrPromise
from typing_extensions import Literal

class FieldDoesNotExist(Exception): ...
Expand Down Expand Up @@ -35,7 +36,7 @@ class ValidationError(Exception):
def __init__(
self,
# Accepts arbitrarily nested data structure, mypy doesn't allow describing it accurately.
message: Union[str, ValidationError, Dict[str, Any], List[Any]],
message: Union[str, _StrPromise, ValidationError, Dict[str, Any], List[Any]],
code: Optional[str] = ...,
params: Optional[Dict[str, Any]] = ...,
) -> None: ...
Expand Down
48 changes: 24 additions & 24 deletions django-stubs/db/models/fields/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ from django.db.models.query_utils import Q, RegisterLookupMixin
from django.forms import Field as FormField
from django.forms import Widget
from django.utils.datastructures import DictWrapper
from django.utils.functional import _Getter
from django.utils.functional import _Getter, _StrOrPromise
from typing_extensions import Protocol

class Empty: ...
Expand Down Expand Up @@ -120,7 +120,7 @@ class Field(RegisterLookupMixin, Generic[_ST, _GT]):
_pyi_lookup_exact_type: Any

widget: Widget
help_text: str
help_text: _StrOrPromise
attname: str
auto_created: bool
primary_key: bool
Expand All @@ -134,7 +134,7 @@ class Field(RegisterLookupMixin, Generic[_ST, _GT]):
max_length: Optional[int]
model: Type[Model]
name: str
verbose_name: str
verbose_name: _StrOrPromise
description: Union[str, _Getter[str]]
blank: bool
null: bool
Expand All @@ -158,7 +158,7 @@ class Field(RegisterLookupMixin, Generic[_ST, _GT]):
non_db_attrs: Tuple[str, ...]
def __init__(
self,
verbose_name: Optional[str] = ...,
verbose_name: Optional[_StrOrPromise] = ...,
name: Optional[str] = ...,
primary_key: bool = ...,
max_length: Optional[int] = ...,
Expand All @@ -174,7 +174,7 @@ class Field(RegisterLookupMixin, Generic[_ST, _GT]):
unique_for_month: Optional[str] = ...,
unique_for_year: Optional[str] = ...,
choices: Optional[_FieldChoices] = ...,
help_text: str = ...,
help_text: _StrOrPromise = ...,
db_column: Optional[str] = ...,
db_tablespace: Optional[str] = ...,
auto_created: bool = ...,
Expand Down Expand Up @@ -260,7 +260,7 @@ class DecimalField(Field[_ST, _GT]):
decimal_places: int = ...
def __init__(
self,
verbose_name: Optional[str] = ...,
verbose_name: Optional[_StrOrPromise] = ...,
name: Optional[str] = ...,
max_digits: Optional[int] = ...,
decimal_places: Optional[int] = ...,
Expand All @@ -275,7 +275,7 @@ class DecimalField(Field[_ST, _GT]):
auto_created: bool = ...,
serialize: bool = ...,
choices: Optional[_FieldChoices] = ...,
help_text: str = ...,
help_text: _StrOrPromise = ...,
db_column: Optional[str] = ...,
db_tablespace: Optional[str] = ...,
validators: Iterable[validators._ValidatorCallable] = ...,
Expand All @@ -289,7 +289,7 @@ class CharField(Field[_ST, _GT]):
_pyi_lookup_exact_type: Any
def __init__(
self,
verbose_name: Optional[str] = ...,
verbose_name: Optional[_StrOrPromise] = ...,
name: Optional[str] = ...,
primary_key: bool = ...,
max_length: Optional[int] = ...,
Expand All @@ -305,7 +305,7 @@ class CharField(Field[_ST, _GT]):
unique_for_month: Optional[str] = ...,
unique_for_year: Optional[str] = ...,
choices: Optional[_FieldChoices] = ...,
help_text: str = ...,
help_text: _StrOrPromise = ...,
db_column: Optional[str] = ...,
db_tablespace: Optional[str] = ...,
validators: Iterable[validators._ValidatorCallable] = ...,
Expand All @@ -319,7 +319,7 @@ class CommaSeparatedIntegerField(CharField[_ST, _GT]): ...
class SlugField(CharField[_ST, _GT]):
def __init__(
self,
verbose_name: Optional[str] = ...,
verbose_name: Optional[_StrOrPromise] = ...,
name: Optional[str] = ...,
primary_key: bool = ...,
unique: bool = ...,
Expand All @@ -333,7 +333,7 @@ class SlugField(CharField[_ST, _GT]):
unique_for_month: Optional[str] = ...,
unique_for_year: Optional[str] = ...,
choices: Optional[_FieldChoices] = ...,
help_text: str = ...,
help_text: _StrOrPromise = ...,
db_column: Optional[str] = ...,
db_tablespace: Optional[str] = ...,
validators: Iterable[validators._ValidatorCallable] = ...,
Expand All @@ -349,7 +349,7 @@ class EmailField(CharField[_ST, _GT]): ...
class URLField(CharField[_ST, _GT]):
def __init__(
self,
verbose_name: Optional[str] = ...,
verbose_name: Optional[_StrOrPromise] = ...,
name: Optional[str] = ...,
*,
primary_key: bool = ...,
Expand All @@ -366,7 +366,7 @@ class URLField(CharField[_ST, _GT]):
unique_for_month: Optional[str] = ...,
unique_for_year: Optional[str] = ...,
choices: Optional[_FieldChoices] = ...,
help_text: str = ...,
help_text: _StrOrPromise = ...,
db_column: Optional[str] = ...,
db_tablespace: Optional[str] = ...,
auto_created: bool = ...,
Expand All @@ -381,7 +381,7 @@ class TextField(Field[_ST, _GT]):
_pyi_lookup_exact_type: Any
def __init__(
self,
verbose_name: Optional[str] = ...,
verbose_name: Optional[_StrOrPromise] = ...,
name: Optional[str] = ...,
primary_key: bool = ...,
max_length: Optional[int] = ...,
Expand All @@ -397,7 +397,7 @@ class TextField(Field[_ST, _GT]):
unique_for_month: Optional[str] = ...,
unique_for_year: Optional[str] = ...,
choices: Optional[_FieldChoices] = ...,
help_text: str = ...,
help_text: _StrOrPromise = ...,
db_column: Optional[str] = ...,
db_tablespace: Optional[str] = ...,
validators: Iterable[validators._ValidatorCallable] = ...,
Expand Down Expand Up @@ -443,7 +443,7 @@ class GenericIPAddressField(Field[_ST, _GT]):
auto_created: bool = ...,
serialize: bool = ...,
choices: Optional[_FieldChoices] = ...,
help_text: str = ...,
help_text: _StrOrPromise = ...,
db_column: Optional[str] = ...,
db_tablespace: Optional[str] = ...,
validators: Iterable[validators._ValidatorCallable] = ...,
Expand All @@ -458,7 +458,7 @@ class DateField(DateTimeCheckMixin, Field[_ST, _GT]):
_pyi_lookup_exact_type: Union[str, date]
def __init__(
self,
verbose_name: Optional[str] = ...,
verbose_name: Optional[_StrOrPromise] = ...,
name: Optional[str] = ...,
auto_now: bool = ...,
auto_now_add: bool = ...,
Expand All @@ -474,7 +474,7 @@ class DateField(DateTimeCheckMixin, Field[_ST, _GT]):
auto_created: bool = ...,
serialize: bool = ...,
choices: Optional[_FieldChoices] = ...,
help_text: str = ...,
help_text: _StrOrPromise = ...,
db_column: Optional[str] = ...,
db_tablespace: Optional[str] = ...,
validators: Iterable[validators._ValidatorCallable] = ...,
Expand All @@ -486,7 +486,7 @@ class TimeField(DateTimeCheckMixin, Field[_ST, _GT]):
_pyi_private_get_type: time
def __init__(
self,
verbose_name: Optional[str] = ...,
verbose_name: Optional[_StrOrPromise] = ...,
name: Optional[str] = ...,
auto_now: bool = ...,
auto_now_add: bool = ...,
Expand All @@ -501,7 +501,7 @@ class TimeField(DateTimeCheckMixin, Field[_ST, _GT]):
auto_created: bool = ...,
serialize: bool = ...,
choices: Optional[_FieldChoices] = ...,
help_text: str = ...,
help_text: _StrOrPromise = ...,
db_column: Optional[str] = ...,
db_tablespace: Optional[str] = ...,
validators: Iterable[validators._ValidatorCallable] = ...,
Expand All @@ -518,7 +518,7 @@ class UUIDField(Field[_ST, _GT]):
_pyi_private_get_type: uuid.UUID
def __init__(
self,
verbose_name: Optional[str] = ...,
verbose_name: Optional[_StrOrPromise] = ...,
*,
name: Optional[str] = ...,
primary_key: bool = ...,
Expand All @@ -535,7 +535,7 @@ class UUIDField(Field[_ST, _GT]):
unique_for_month: Optional[str] = ...,
unique_for_year: Optional[str] = ...,
choices: Optional[_FieldChoices] = ...,
help_text: str = ...,
help_text: _StrOrPromise = ...,
db_column: Optional[str] = ...,
db_tablespace: Optional[str] = ...,
auto_created: bool = ...,
Expand All @@ -551,7 +551,7 @@ class FilePathField(Field[_ST, _GT]):
allow_folders: bool = ...
def __init__(
self,
verbose_name: Optional[str] = ...,
verbose_name: Optional[_StrOrPromise] = ...,
name: Optional[str] = ...,
path: Union[str, Callable[..., str]] = ...,
match: Optional[str] = ...,
Expand All @@ -570,7 +570,7 @@ class FilePathField(Field[_ST, _GT]):
auto_created: bool = ...,
serialize: bool = ...,
choices: Optional[_FieldChoices] = ...,
help_text: str = ...,
help_text: _StrOrPromise = ...,
db_column: Optional[str] = ...,
db_tablespace: Optional[str] = ...,
validators: Iterable[validators._ValidatorCallable] = ...,
Expand Down
Loading

0 comments on commit 9a41aa6

Please sign in to comment.