Skip to content

Commit

Permalink
Improve BaseManager, Manager types (#205)
Browse files Browse the repository at this point in the history
This PR makes a series of improvements to methods and properties of `BaseManager` and `Manager`:

- `BaseManager.deconstruct()`: Most of the values in the return tuple can be null ([see implementation](https://github.com/django/django/blob/a576ef98aea2709741f32a863cff3c7a54172ded/django/db/models/manager.py#L42))
- `BaseManager.from_queryset()`: `queryset_class` is now generic, and the return type is made more specific ([see implementation](https://github.com/django/django/blob/a576ef98aea2709741f32a863cff3c7a54172ded/django/db/models/manager.py#L108)). This allows code like this to receive type information:

    ```python
    class CustomQuerySet(models.QuerySet["User"]):
        ...

    class CustomManager(models.Manager.from_queryset(CustomQuerySet)):
        def get_queryset(self):
            qs = super().get_queryset() # ← Type checker knows get_queryset exists in the parent
            user = qs.get() # ← Inferred type is "User"
            return qs
    ```

- `BaseManager._get_queryset_methods()`: Return dictionary values are functions ([see implementation](https://github.com/django/django/blob/a576ef98aea2709741f32a863cff3c7a54172ded/django/db/models/manager.py#L83)).
- `Manager._queryset_class`: This property is assigned dynamically by `BaseManager.from_queryset()` ([see implementation](https://github.com/django/django/blob/a576ef98aea2709741f32a863cff3c7a54172ded/django/db/models/manager.py#L115))
  • Loading branch information
noelleleigh authored Oct 21, 2023
1 parent aa3497f commit 6f1f9a3
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions django-stubs/db/models/manager.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections.abc import Iterable, MutableMapping
from typing import Any, Generic, TypeVar
from typing import Any, Callable, Generic, TypeVar
from typing_extensions import Self

from django.db.models.base import Model
Expand All @@ -19,21 +19,24 @@ class BaseManager(QuerySet[_T]):
def __init__(self) -> None: ...
def deconstruct(
self,
) -> tuple[bool, str, None, tuple[Any, ...], dict[str, int]]: ...
) -> tuple[bool, str | None, str | None, tuple[Any, ...] | None, dict[str, Any] | None]: ...
def check(self, **kwargs: Any) -> list[Any]: ...
@classmethod
def from_queryset(
cls, queryset_class: type[QuerySet[Any]], class_name: str | None = ...
) -> Any: ...
cls, queryset_class: type[QuerySet[_T]], class_name: str | None = ...
) -> type[Manager[_T]]: ...
@classmethod
def _get_queryset_methods(cls, queryset_class: type) -> dict[str, Any]: ...
def _get_queryset_methods(
cls, queryset_class: type[QuerySet[_T]]
) -> dict[str, Callable[..., Any]]: ...
def contribute_to_class(self, model: type[Model], name: str) -> None: ...
def db_manager(
self, using: str | None = ..., hints: dict[str, Model] | None = ...
) -> Self: ...
def get_queryset(self) -> QuerySet[_T]: ...

class Manager(BaseManager[_T]): ...
class Manager(BaseManager[_T]):
_queryset_class: type[QuerySet[_T]]

class RelatedManager(Manager[_T]):
related_val: tuple[int, ...]
Expand Down

0 comments on commit 6f1f9a3

Please sign in to comment.