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
4 changes: 2 additions & 2 deletions django-stubs/views/generic/list.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ from django.views.generic.base import ContextMixin, TemplateResponseMixin, View

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

class HasModel(Protocol):
class _HasModel(Protocol):
flaeppe marked this conversation as resolved.
Show resolved Hide resolved
@property
def model(self) -> type[Model]: ...

Expand Down Expand Up @@ -39,7 +39,7 @@ class MultipleObjectMixin(Generic[_M], ContextMixin):
def get_paginate_orphans(self) -> int: ...
def get_allow_empty(self) -> bool: ...
@overload
def get_context_object_name(self, object_list: HasModel) -> str: ...
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(
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
16 changes: 9 additions & 7 deletions tests/assert_type/views/generic.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

from django.db import models
from django.views.generic.detail import SingleObjectMixin
from django.views.generic.list import ListView
Expand All @@ -11,18 +13,18 @@ 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.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), str | None)
assert_type(detail_view.get_context_object_name(1), Optional[str])


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.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()), str | None)
assert_type(list_view.get_context_object_name(1), str | None)
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