Skip to content
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

Improve get_context_object_name on SingleObjectMixin and MultipleObjectMixin #2298

Merged
merged 5 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 6 additions & 3 deletions django-stubs/views/generic/detail.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Generic, TypeVar
from typing import Any, Generic, TypeVar, overload

from django.db import models
from django.http import HttpRequest, HttpResponse
Expand All @@ -7,7 +7,7 @@ from django.views.generic.base import ContextMixin, TemplateResponseMixin, View
_M = TypeVar("_M", bound=models.Model)

class SingleObjectMixin(Generic[_M], ContextMixin):
model: type[_M]
model: type[_M] | None
queryset: models.query.QuerySet[_M] | None
flaeppe marked this conversation as resolved.
Show resolved Hide resolved
slug_field: str
context_object_name: str | None
Expand All @@ -17,7 +17,10 @@ class SingleObjectMixin(Generic[_M], ContextMixin):
def get_object(self, queryset: models.query.QuerySet[_M] | None = ...) -> _M: ...
def get_queryset(self) -> models.query.QuerySet[_M]: ...
def get_slug_field(self) -> str: ...
def get_context_object_name(self, obj: _M) -> str | None: ...
@overload
def get_context_object_name(self, obj: _M) -> str: ...
@overload
def get_context_object_name(self, obj: Any) -> str | None: ...

class BaseDetailView(SingleObjectMixin[_M], View):
object: _M
Expand Down
13 changes: 10 additions & 3 deletions django-stubs/views/generic/list.pyi
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from collections.abc import Sequence
from typing import Any, Generic, TypeVar
from typing import Any, Generic, Protocol, TypeVar, overload

from django.core.paginator import Page, Paginator, _SupportsPagination
from django.db.models import Model, QuerySet
from django.http import HttpRequest, HttpResponse
from django.views.generic.base import ContextMixin, TemplateResponseMixin, View

_M = TypeVar("_M", bound=Model, covariant=True)
_M = TypeVar("_M", bound=Model)

class HasModel(Protocol):
@property
def model(self) -> type[Model]: ...

class MultipleObjectMixin(Generic[_M], ContextMixin):
allow_empty: bool
Expand Down Expand Up @@ -34,7 +38,10 @@ class MultipleObjectMixin(Generic[_M], ContextMixin):
) -> Paginator: ...
def get_paginate_orphans(self) -> int: ...
def get_allow_empty(self) -> bool: ...
def get_context_object_name(self, object_list: _SupportsPagination[_M]) -> str | None: ...
@overload
def get_context_object_name(self, object_list: HasModel) -> str: ...
@overload
def get_context_object_name(self, object_list: Any) -> str | None: ...
def get_context_data(
self, *, object_list: _SupportsPagination[_M] | None = ..., **kwargs: Any
) -> dict[str, Any]: ...
Expand Down
28 changes: 28 additions & 0 deletions tests/assert_type/views/generic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from django.db import models
from django.views.generic.detail import SingleObjectMixin
from django.views.generic.list import ListView
from typing_extensions import assert_type


class MyModel(models.Model): ...


class MyDetailView(SingleObjectMixin[MyModel]): ...


detail_view = MyDetailView()
assert_type(detail_view.model, type[MyModel] | None)
assert_type(detail_view.queryset, models.QuerySet[MyModel, MyModel] | None)
assert_type(detail_view.get_context_object_name(MyModel()), str)
assert_type(detail_view.get_context_object_name(1), str | None)


class MyListView(ListView[MyModel]): ...


list_view = MyListView()
assert_type(list_view.model, type[MyModel] | None)
assert_type(list_view.queryset, models.QuerySet[MyModel, MyModel] | None)
assert_type(list_view.get_context_object_name(models.QuerySet[MyModel]()), str)
assert_type(list_view.get_context_object_name(MyModel()), str | None)
assert_type(list_view.get_context_object_name(1), str | None)
Loading