typing: Broaden type annotations for QuerySet compatibility.

To explain the rationale of this change, for example, there is
`get_user_activity_summary` which accepts either a `Collection[UserActivity]`,
where `QuerySet[T]` is not strictly `Sequence[T]` because its slicing behavior
is different from the `Protocol`, making `Collection` necessary.

Similarily, we should have `Iterable[T]` instead of `List[T]` so that
`QuerySet[T]` will also be an acceptable subtype, or `Sequence[T]` when we
also expect it to be indexed.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
This commit is contained in:
Zixuan James Li
2022-06-23 14:07:19 -04:00
committed by Tim Abbott
parent 40fcf5a633
commit ab1bbdda65
18 changed files with 65 additions and 41 deletions

View File

@@ -2,7 +2,7 @@ import datetime
import heapq
import logging
from collections import defaultdict
from typing import Any, Dict, List, Set, Tuple
from typing import Any, Collection, Dict, List, Set, Tuple
from django.conf import settings
from django.db import transaction
@@ -276,10 +276,12 @@ def get_slim_stream_map(stream_ids: Set[int]) -> Dict[int, Stream]:
return {stream.id: stream for stream in streams}
def bulk_get_digest_context(users: List[UserProfile], cutoff: float) -> Dict[int, Dict[str, Any]]:
def bulk_get_digest_context(
users: Collection[UserProfile], cutoff: float
) -> Dict[int, Dict[str, Any]]:
# We expect a non-empty list of users all from the same realm.
assert users
realm = users[0].realm
realm = next(iter(users)).realm
for user in users:
assert user.realm_id == realm.id