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

Formset get_queryset() returns QuerySet #2174

Merged
merged 3 commits into from
May 27, 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
4 changes: 2 additions & 2 deletions django-stubs/forms/models.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ from django.forms.renderers import BaseRenderer
from django.forms.utils import ErrorList, _DataT, _FilesT
from django.forms.widgets import Widget
from django.utils.choices import BaseChoiceIterator, CallableChoiceIterator, _Choices, _ChoicesCallable
from django.utils.datastructures import _IndexableCollection, _PropertyDescriptor
from django.utils.datastructures import _PropertyDescriptor, _QuerySetLike
from django.utils.functional import _StrOrPromise
from typing_extensions import TypeAlias

Expand Down Expand Up @@ -126,7 +126,7 @@ class BaseModelFormSet(Generic[_M, _ModelFormT], BaseFormSet[_ModelFormT]):
**kwargs: Any,
) -> None: ...
def initial_form_count(self) -> int: ...
def get_queryset(self) -> _IndexableCollection[_M]: ...
def get_queryset(self) -> _QuerySetLike[_M]: ...
def save_new(self, form: _ModelFormT, commit: bool = ...) -> _M: ...
def save_existing(self, form: _ModelFormT, obj: _M, commit: bool = ...) -> _M: ...
def delete_existing(self, obj: _M, commit: bool = ...) -> None: ...
Expand Down
13 changes: 12 additions & 1 deletion django-stubs/utils/datastructures.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from collections.abc import Collection, Iterable, Iterator, Mapping, MutableMapping, MutableSet
from collections.abc import Collection, Iterable, Iterator, Mapping, MutableMapping, MutableSet, Sized
from typing import Any, Generic, NoReturn, Protocol, TypeVar, overload, type_check_only

from _typeshed import Incomplete
Expand Down Expand Up @@ -43,6 +43,17 @@ class _IndexableCollection(Protocol[_I], Collection[_I]): # noqa: PYI046
@overload
def __getitem__(self, index: slice) -> Self: ...

@type_check_only
class _QuerySetLike(Protocol[_I], Iterable[_I], Sized): # noqa: PYI046
"""
Abstract collection type reflecting methods provided by QuerySet.
"""

@overload
def __getitem__(self, index: int) -> _I: ...
@overload
def __getitem__(self, index: slice) -> Self: ...

class OrderedSet(MutableSet[_K]):
dict: dict[_K, None]
def __init__(self, iterable: Iterable[_K] | None = ...) -> None: ...
Expand Down
1 change: 1 addition & 0 deletions tests/typecheck/test_formsets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
ArticleFS(instance=Article()) # E: Argument "instance" to "BaseInlineFormSet" has incompatible type "Article"; expected "Optional[Category]" [arg-type]
fs = ArticleFS(instance=Category())
reveal_type(fs.instance) # N: Revealed type is "myapp.models.Category"
reveal_type(fs.get_queryset()) # N: Revealed type is "django.utils.datastructures._QuerySetLike[myapp.models.Article]"
installed_apps:
- myapp
files:
Expand Down
Loading