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 types for QuerySet.contains and bulk_create #1134

Merged
merged 2 commits into from
Aug 28, 2022
Merged
Changes from all 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
20 changes: 16 additions & 4 deletions django-stubs/db/models/query.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,22 @@ class _QuerySet(Generic[_T, _Row], Collection[_Row], Reversible[_Row], Sized):
def create(self, **kwargs: Any) -> _T: ...
async def acreate(self, **kwargs: Any) -> _T: ...
def bulk_create(
self, objs: Iterable[_T], batch_size: Optional[int] = ..., ignore_conflicts: bool = ...
self,
objs: Iterable[_T],
batch_size: Optional[int] = ...,
ignore_conflicts: bool = ...,
update_conflicts: bool = ...,
update_fields: Optional[Collection[str]] = ...,
unique_fields: Optional[Collection[str]] = ...,
) -> List[_T]: ...
async def abulk_create(
self, objs: Iterable[_T], batch_size: Optional[int] = ..., ignore_conflicts: bool = ...
self,
objs: Iterable[_T],
batch_size: Optional[int] = ...,
ignore_conflicts: bool = ...,
update_conflicts: bool = ...,
update_fields: Optional[Collection[str]] = ...,
unique_fields: Optional[Collection[str]] = ...,
) -> List[_T]: ...
def bulk_update(self, objs: Iterable[_T], fields: Iterable[str], batch_size: Optional[int] = ...) -> int: ...
async def abulk_update(self, objs: Iterable[_T], fields: Iterable[str], batch_size: Optional[int] = ...) -> int: ...
Expand Down Expand Up @@ -98,8 +110,8 @@ class _QuerySet(Generic[_T, _Row], Collection[_Row], Reversible[_Row], Sized):
async def aexists(self) -> bool: ...
def explain(self, *, format: Optional[Any] = ..., **options: Any) -> str: ...
async def aexplain(self, *, format: Optional[Any] = ..., **options: Any) -> str: ...
def contains(self, objs: Iterable[_T]) -> bool: ...
async def acontains(self, objs: Iterable[_T]) -> bool: ...
def contains(self, obj: models.Model) -> bool: ...
async def acontains(self, obj: models.Model) -> bool: ...
Comment on lines +113 to +114
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using _T here gave me:

django-stubs/db/models/query.pyi:113: error: Cannot use a covariant type variable as a parameter
django-stubs/db/models/query.pyi:114: error: Cannot use a covariant type variable as a parameter

But looking at the source, it seems any instance of a model class is acceptable:

https://github.com/django/django/blob/b92ffebb0cdc469baaf1b8f0e72dddb069eb2fb4/django/db/models/query.py#L1241-L1255

hence change to models.Model

def raw(
self,
raw_query: str,
Expand Down