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 2 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
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):
flaeppe marked this conversation as resolved.
Show resolved Hide resolved
@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
1 change: 0 additions & 1 deletion scripts/stubtest/allowlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ django.db.backends.base.base.BaseDatabaseWrapper.ops
django.db.backends.base.base.BaseDatabaseWrapper.ops_class

# Attributes defaulting to None messing with mypy
django.views.generic.detail.SingleObjectMixin.model
django.views.generic.edit.BaseDeleteView.form_class

# Dynamically added via django.core.management.color.make_style
Expand Down
30 changes: 30 additions & 0 deletions tests/assert_type/views/generic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from typing import Optional

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, Optional[type[MyModel]])
assert_type(detail_view.queryset, Optional[models.QuerySet[MyModel, MyModel]])
assert_type(detail_view.get_context_object_name(MyModel()), str)
assert_type(detail_view.get_context_object_name(1), Optional[str])


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


list_view = MyListView()
assert_type(list_view.model, Optional[type[MyModel]])
assert_type(list_view.queryset, Optional[models.QuerySet[MyModel, MyModel]])
assert_type(list_view.get_context_object_name(models.QuerySet[MyModel]()), str)
assert_type(list_view.get_context_object_name(MyModel()), Optional[str])
assert_type(list_view.get_context_object_name(1), Optional[str])
6 changes: 3 additions & 3 deletions tests/typecheck/views/generic/test_detail.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
def get_queryset(self) -> QuerySet[MyModel]:
self.get_object(super().get_queryset())
return super().get_queryset()
custom_settings: |
INSTALLED_APPS = ('myapp',)
installed_apps:
- myapp
files:
- path: myapp/__init__.py
- path: myapp/models.py
Expand All @@ -49,7 +49,7 @@
class Other(models.Model):
...
out: |
main:7: error: Incompatible types in assignment (expression has type "Type[MyModel]", base class "SingleObjectMixin" defined the type as "Type[Other]") [assignment]
main:7: error: Incompatible types in assignment (expression has type "Type[MyModel]", base class "SingleObjectMixin" defined the type as "Optional[Type[Other]]") [assignment]
main:8: error: Incompatible types in assignment (expression has type "QuerySet[MyModel, MyModel]", base class "SingleObjectMixin" defined the type as "Optional[QuerySet[Other, Other]]") [assignment]
main:10: error: Return type "QuerySet[MyModel, MyModel]" of "get_queryset" incompatible with return type "QuerySet[Other, Other]" in supertype "SingleObjectMixin" [override]
main:12: error: Incompatible return value type (got "QuerySet[Other, Other]", expected "QuerySet[MyModel, MyModel]") [return-value]
Loading