Skip to content

Commit 5514e37

Browse files
authored
Type django.views (#1716)
* fix: removed argument * fix: missing constant ERROR_PAGE_TEMPLATE * fix: missing Logger * fix: missing view_is_async We use _Getter | bool here, as users could use @Property, @cached_property or a direct assign (there is some usage on Github) * fix: attributes can be None E.g. for template_name, is we only set the type as str, users might wrongfully access it, and type checking would not warn them that it can be None. paginate_by can be None per get_paginate_by() documentation packages defaults to and supports None according to JavaScriptCatalog.get() implementation * fi: misc django.views * chore: allowlist
1 parent 3147e39 commit 5514e37

File tree

10 files changed

+38
-38
lines changed

10 files changed

+38
-38
lines changed

django-stubs/views/debug.pyi

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ DEBUG_ENGINE: Engine
1414
class CallableSettingWrapper:
1515
def __init__(self, callable_setting: Callable | type[Any]) -> None: ...
1616

17+
class ExceptionCycleWarning(UserWarning): ...
18+
1719
def technical_500_response(
1820
request: HttpRequest,
1921
exc_type: type[BaseException] | None,
@@ -29,6 +31,8 @@ class SafeExceptionReporterFilter:
2931
hidden_settings: re.Pattern[str]
3032
def cleanse_setting(self, key: int | str, value: Any) -> Any: ...
3133
def get_safe_settings(self) -> dict[str, Any]: ...
34+
def get_safe_request_meta(self, request: HttpRequest) -> dict[str, Any]: ...
35+
def get_safe_cookies(self, request: HttpRequest) -> dict[str, Any]: ...
3236
def is_active(self, request: HttpRequest | None) -> bool: ...
3337
def get_cleansed_multivaluedict(self, request: HttpRequest, multivaluedict: QueryDict) -> QueryDict: ...
3438
def get_post_parameters(self, request: HttpRequest | None) -> dict[str, Any]: ...
@@ -67,3 +71,6 @@ class ExceptionReporter:
6771

6872
def technical_404_response(request: HttpRequest, exception: Http404) -> HttpResponse: ...
6973
def default_urlconf(request: HttpResponse | None) -> HttpResponse: ...
74+
def builtin_template_path(name: str) -> Path: ...
75+
def get_caller(request: HttpRequest) -> str: ...
76+
def get_exception_reporter_class(request: HttpRequest) -> ExceptionReporter: ...

django-stubs/views/defaults.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ ERROR_404_TEMPLATE_NAME: str
1010
ERROR_403_TEMPLATE_NAME: str
1111
ERROR_400_TEMPLATE_NAME: str
1212
ERROR_500_TEMPLATE_NAME: str
13+
ERROR_PAGE_TEMPLATE: str
1314

1415
def page_not_found(
1516
request: HttpRequest, exception: Exception | None, template_name: str = ...

django-stubs/views/generic/base.pyi

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import logging
12
from collections.abc import Callable, Sequence
23
from typing import Any
34

45
from django.http.request import HttpRequest
56
from django.http.response import HttpResponse, HttpResponseBase
7+
from django.utils.functional import _Getter
8+
9+
logger: logging.Logger
610

711
class ContextMixin:
812
extra_context: dict[str, Any] | None
@@ -14,6 +18,7 @@ class View:
1418
args: Any
1519
kwargs: Any
1620
def __init__(self, **kwargs: Any) -> None: ...
21+
view_is_async: _Getter[bool] | bool
1722
@classmethod
1823
def as_view(cls: Any, **initkwargs: Any) -> Callable[..., HttpResponseBase]: ...
1924
def setup(self, request: HttpRequest, *args: Any, **kwargs: Any) -> None: ...
@@ -22,7 +27,7 @@ class View:
2227
def options(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponseBase: ...
2328

2429
class TemplateResponseMixin:
25-
template_name: str
30+
template_name: str | None
2631
template_engine: str | None
2732
response_class: type[HttpResponse]
2833
content_type: str | None

django-stubs/views/generic/dates.pyi

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ from django.db import models
66
from django.db.models.query import QuerySet
77
from django.http import HttpRequest, HttpResponse
88
from django.utils.datastructures import _IndexableCollection
9+
from django.utils.functional import _Getter
910
from django.views.generic.base import View
1011
from django.views.generic.detail import BaseDetailView, SingleObjectTemplateResponseMixin
1112
from django.views.generic.list import MultipleObjectMixin, MultipleObjectTemplateResponseMixin
1213
from typing_extensions import TypeAlias
1314

1415
_M = TypeVar("_M", bound=models.Model)
16+
_DatedItems: TypeAlias = tuple[_IndexableCollection[datetime.date] | None, _IndexableCollection[_M], dict[str, Any]]
1517

1618
class YearMixin:
1719
year_format: str
@@ -50,15 +52,12 @@ class DateMixin:
5052
allow_future: bool
5153
def get_date_field(self) -> str: ...
5254
def get_allow_future(self) -> bool: ...
53-
@property
54-
def uses_datetime_field(self) -> bool: ...
55-
56-
DatedItems: TypeAlias = tuple[_IndexableCollection[datetime.date] | None, _IndexableCollection[_M], dict[str, Any]]
55+
uses_datetime_field: _Getter[bool] | bool
5756

5857
class BaseDateListView(MultipleObjectMixin[_M], DateMixin, View):
5958
date_list_period: str
6059
def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: ...
61-
def get_dated_items(self) -> DatedItems: ...
60+
def get_dated_items(self) -> _DatedItems: ...
6261
def get_ordering(self) -> str | Sequence[str]: ...
6362
def get_dated_queryset(self, **lookup: Any) -> models.query.QuerySet[_M]: ...
6463
def get_date_list_period(self) -> str: ...
@@ -68,41 +67,41 @@ class BaseDateListView(MultipleObjectMixin[_M], DateMixin, View):
6867

6968
class BaseArchiveIndexView(BaseDateListView[_M]):
7069
context_object_name: str
71-
def get_dated_items(self) -> DatedItems[_M]: ...
70+
def get_dated_items(self) -> _DatedItems[_M]: ...
7271

7372
class ArchiveIndexView(MultipleObjectTemplateResponseMixin, BaseArchiveIndexView):
7473
template_name_suffix: str
7574

7675
class BaseYearArchiveView(YearMixin, BaseDateListView[_M]):
7776
date_list_period: str
7877
make_object_list: bool
79-
def get_dated_items(self) -> DatedItems[_M]: ...
78+
def get_dated_items(self) -> _DatedItems[_M]: ...
8079
def get_make_object_list(self) -> bool: ...
8180

8281
class YearArchiveView(MultipleObjectTemplateResponseMixin, BaseYearArchiveView):
8382
template_name_suffix: str
8483

8584
class BaseMonthArchiveView(YearMixin, MonthMixin, BaseDateListView[_M]):
8685
date_list_period: str
87-
def get_dated_items(self) -> DatedItems[_M]: ...
86+
def get_dated_items(self) -> _DatedItems[_M]: ...
8887

8988
class MonthArchiveView(MultipleObjectTemplateResponseMixin, BaseMonthArchiveView):
9089
template_name_suffix: str
9190

9291
class BaseWeekArchiveView(YearMixin, WeekMixin, BaseDateListView[_M]):
93-
def get_dated_items(self) -> DatedItems[_M]: ...
92+
def get_dated_items(self) -> _DatedItems[_M]: ...
9493

9594
class WeekArchiveView(MultipleObjectTemplateResponseMixin, BaseWeekArchiveView):
9695
template_name_suffix: str
9796

9897
class BaseDayArchiveView(YearMixin, MonthMixin, DayMixin, BaseDateListView[_M]):
99-
def get_dated_items(self) -> DatedItems[_M]: ...
98+
def get_dated_items(self) -> _DatedItems[_M]: ...
10099

101100
class DayArchiveView(MultipleObjectTemplateResponseMixin, BaseDayArchiveView):
102101
template_name_suffix: str
103102

104103
class BaseTodayArchiveView(BaseDayArchiveView[_M]):
105-
def get_dated_items(self) -> DatedItems[_M]: ...
104+
def get_dated_items(self) -> _DatedItems[_M]: ...
106105

107106
class TodayArchiveView(MultipleObjectTemplateResponseMixin, BaseTodayArchiveView):
108107
template_name_suffix: str

django-stubs/views/generic/edit.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ class DeletionMixin(Generic[_M]):
6767

6868
class BaseDeleteView(Generic[_M, _ModelFormT], DeletionMixin[_M], FormMixin[_ModelFormT], BaseDetailView[_M]):
6969
object: _M
70+
def __init__(self, *args: Any, **kwargs: Any) -> None: ...
7071

7172
class DeleteView(Generic[_M, _ModelFormT], SingleObjectTemplateResponseMixin, BaseDeleteView[_M, _ModelFormT]):
7273
object: _M
7374
template_name_suffix: str
75+
76+
class DeleteViewCustomDeleteWarning(Warning): ...

django-stubs/views/generic/list.pyi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class MultipleObjectMixin(Generic[_M], ContextMixin):
1212
allow_empty: bool
1313
queryset: _SupportsPagination[_M] | None
1414
model: type[_M] | None
15-
paginate_by: int
15+
paginate_by: int | None
1616
paginate_orphans: int
1717
context_object_name: str | None
1818
paginator_class: type[Paginator]
@@ -35,6 +35,9 @@ class MultipleObjectMixin(Generic[_M], ContextMixin):
3535
def get_paginate_orphans(self) -> int: ...
3636
def get_allow_empty(self) -> bool: ...
3737
def get_context_object_name(self, object_list: _SupportsPagination[_M]) -> str | None: ...
38+
def get_context_data(
39+
self, *, object_list: _SupportsPagination[_M] | None = ..., **kwargs: Any
40+
) -> dict[str, Any]: ...
3841

3942
class BaseListView(MultipleObjectMixin[_M], View):
4043
object_list: _SupportsPagination[_M]

django-stubs/views/i18n.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ js_catalog_template: str
1616
class JavaScriptCatalog(View):
1717
head: Callable
1818
domain: str
19-
packages: list[str]
19+
packages: list[str] | None
2020
translation: DjangoTranslation
2121
def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: ...
2222
def get_paths(self, packages: list[str]) -> list[str]: ...

django-stubs/views/static.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ DEFAULT_DIRECTORY_INDEX_TEMPLATE: str
1111
template_translatable: Any
1212

1313
def directory_index(path: Any, fullpath: Any) -> HttpResponse: ...
14-
def was_modified_since(header: str | None = ..., mtime: float = ..., size: int = ...) -> bool: ...
14+
def was_modified_since(header: str | None = ..., mtime: float = ...) -> bool: ...

scripts/stubtest/allowlist.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,12 @@ django.utils.translation.template.context_re
7373
django.utils.translation.template.constant_re
7474
django.utils.translation.template.block_re
7575
django.utils.version.version_component_re
76+
django.views.debug.SafeExceptionReporterFilter.hidden_settings
7677

7778
# AlterTogetherOptionsOperation.option_name is set as None,
7879
# but is required in the init, so we type it as str
7980
django.db.migrations.operations.models.AlterTogetherOptionOperation.option_name
81+
82+
# Attributes defaulting to None messing with mypy
83+
django.views.generic.detail.SingleObjectMixin.model
84+
django.views.generic.edit.BaseDeleteView.form_class

scripts/stubtest/allowlist_todo.txt

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2321,29 +2321,6 @@ django.urls.path
23212321
django.urls.re_path
23222322
django.urls.resolvers.LocaleRegexDescriptor.__get__
23232323
django.urls.resolvers.ResolverMatch.__iter__
2324-
django.views.View.view_is_async
2325-
django.views.debug.ExceptionCycleWarning
2326-
django.views.debug.SafeExceptionReporterFilter.get_safe_cookies
2327-
django.views.debug.SafeExceptionReporterFilter.get_safe_request_meta
2328-
django.views.debug.SafeExceptionReporterFilter.hidden_settings
2329-
django.views.debug.builtin_template_path
2330-
django.views.debug.get_caller
2331-
django.views.debug.get_exception_reporter_class
2332-
django.views.defaults.ERROR_PAGE_TEMPLATE
2333-
django.views.generic.View.view_is_async
2334-
django.views.generic.base.TemplateResponseMixin.template_name
2335-
django.views.generic.base.View.view_is_async
2336-
django.views.generic.base.logger
2337-
django.views.generic.dates.DateMixin.uses_datetime_field
2338-
django.views.generic.dates.DatedItems
2339-
django.views.generic.detail.SingleObjectMixin.model
2340-
django.views.generic.edit.BaseDeleteView.__init__
2341-
django.views.generic.edit.BaseDeleteView.form_class
2342-
django.views.generic.edit.DeleteViewCustomDeleteWarning
2343-
django.views.generic.list.MultipleObjectMixin.get_context_data
2344-
django.views.generic.list.MultipleObjectMixin.paginate_by
2345-
django.views.i18n.JavaScriptCatalog.packages
2346-
django.views.static.was_modified_since
23472324

23482325
# Jinja related:
23492326
django.template.backends.jinja2.Jinja2.template_context_processors

0 commit comments

Comments
 (0)