mirror of
https://github.com/zulip/zulip.git
synced 2025-10-23 04:52:12 +00:00
mypy: Remove use of ValuesQuerySet and QuerySetAny.
This was made unnecessary in django-stubs 5.0.1 and mypy 1.10. Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
@@ -128,24 +128,18 @@ needing to do an explicit annotation.
|
||||
|
||||
When declaring the types for functions that accept a `QuerySet`
|
||||
object, you should always supply the model type that it accepts as the
|
||||
type parameter.
|
||||
first type parameter.
|
||||
|
||||
```python
|
||||
def foo(user: QuerySet[UserProfile]) -> None:
|
||||
...
|
||||
```
|
||||
|
||||
In cases where you need to type the return value from `.values_list`
|
||||
or `.values` on a `QuerySet`, you can use the special
|
||||
`django_stubs_ext.ValuesQuerySet` type.
|
||||
|
||||
For `.values_list`, the second type parameter will be the type of the
|
||||
column.
|
||||
For `.values_list`, supply the type of the column as the second type
|
||||
parameter.
|
||||
|
||||
```python
|
||||
from django_stubs_ext import ValuesQuerySet
|
||||
|
||||
def get_book_page_counts() -> ValuesQuerySet[Book, int]:
|
||||
def get_book_page_counts() -> QuerySet[Book, int]:
|
||||
return Book.objects.filter().values_list("page_count", flat=True)
|
||||
```
|
||||
|
||||
@@ -153,15 +147,13 @@ For `.values`, we prefer to define a `TypedDict` containing the
|
||||
key-value pairs for the columns.
|
||||
|
||||
```python
|
||||
from django_stubs_ext import ValuesQuerySet
|
||||
|
||||
class BookMetadata(TypedDict):
|
||||
id: int
|
||||
name: str
|
||||
|
||||
def get_book_meta_data(
|
||||
book_ids: List[int],
|
||||
) -> ValuesQuerySet[Book, BookMetadata]:
|
||||
) -> QuerySet[Book, BookMetadata]:
|
||||
return Book.objects.filter(id__in=book_ids).values("name", "id")
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user