Skip to content

Commit

Permalink
typing: Import ValuesQuerySet alias from django_stubs_ext.
Browse files Browse the repository at this point in the history
This saves us from using a conditional import.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
  • Loading branch information
PIG208 authored and andersk committed Sep 26, 2022
1 parent b24b8f8 commit 31208fc
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 51 deletions.
5 changes: 1 addition & 4 deletions zerver/actions/message_send.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from collections import defaultdict
from email.headerregistry import Address
from typing import (
TYPE_CHECKING,
AbstractSet,
Any,
Callable,
Expand All @@ -27,6 +26,7 @@
from django.utils.timezone import now as timezone_now
from django.utils.translation import gettext as _
from django.utils.translation import override as override_language
from django_stubs_ext import ValuesQuerySet

from zerver.actions.uploads import do_claim_attachments
from zerver.lib.addressee import Addressee
Expand Down Expand Up @@ -88,9 +88,6 @@
)
from zerver.tornado.django_api import send_event

if TYPE_CHECKING:
from django.db.models.query import _QuerySet as ValuesQuerySet


def compute_irc_user_fullname(email: str) -> str:
return Address(addr_spec=email).username + " (IRC)"
Expand Down
19 changes: 3 additions & 16 deletions zerver/actions/streams.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
import hashlib
from collections import defaultdict
from dataclasses import dataclass
from typing import (
TYPE_CHECKING,
Any,
Collection,
Dict,
Iterable,
List,
Mapping,
Optional,
Set,
Tuple,
)
from typing import Any, Collection, Dict, Iterable, List, Mapping, Optional, Set, Tuple

import orjson
from django.conf import settings
from django.db import transaction
from django.utils.timezone import now as timezone_now
from django.utils.translation import gettext as _
from django.utils.translation import override as override_language
from django_stubs_ext import ValuesQuerySet

from zerver.actions.default_streams import (
do_remove_default_stream,
Expand Down Expand Up @@ -75,9 +65,6 @@
)
from zerver.tornado.django_api import send_event

if TYPE_CHECKING:
from django.db.models.query import _QuerySet as ValuesQuerySet


@transaction.atomic(savepoint=False)
def do_deactivate_stream(
Expand Down Expand Up @@ -220,7 +207,7 @@ def merge_streams(

def get_subscriber_ids(
stream: Stream, requesting_user: Optional[UserProfile] = None
) -> "ValuesQuerySet[Subscription, int]":
) -> ValuesQuerySet[Subscription, int]:
subscriptions_query = get_subscribers_query(stream, requesting_user)
return subscriptions_query.values_list("user_profile_id", flat=True)

Expand Down
8 changes: 3 additions & 5 deletions zerver/lib/cache_helpers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# See https://zulip.readthedocs.io/en/latest/subsystems/caching.html for docs
import datetime
import logging
from typing import TYPE_CHECKING, Any, Callable, Dict, Iterable, Tuple
from typing import Any, Callable, Dict, Iterable, Tuple

from django.conf import settings
from django.contrib.sessions.models import Session
from django.db.models.query import QuerySet
from django.utils.timezone import now as timezone_now
from django_stubs_ext import ValuesQuerySet

# This file needs to be different from cache.py because cache.py
# cannot import anything from zerver.models or we'd have an import
Expand All @@ -32,9 +33,6 @@
huddle_hash_cache_key,
)

if TYPE_CHECKING:
from django.db.models.query import _QuerySet as ValuesQuerySet


def user_cache_items(
items_for_remote_cache: Dict[str, Tuple[UserProfile]], user_profile: UserProfile
Expand Down Expand Up @@ -73,7 +71,7 @@ def session_cache_items(
items_for_remote_cache[store.cache_key] = store.decode(session.session_data)


def get_active_realm_ids() -> "ValuesQuerySet[RealmCount, int]":
def get_active_realm_ids() -> ValuesQuerySet[RealmCount, int]:
"""For installations like Zulip Cloud hosting a lot of realms, it only makes
sense to do cache-filling work for realms that have any currently
active users/clients. Otherwise, we end up with every single-user
Expand Down
9 changes: 4 additions & 5 deletions zerver/lib/display_recipient.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import TYPE_CHECKING, Dict, List, Optional, Set, Tuple, TypedDict
from typing import Dict, List, Optional, Set, Tuple, TypedDict

from django_stubs_ext import ValuesQuerySet

from zerver.lib.cache import (
bulk_cached_fetch,
Expand All @@ -10,9 +12,6 @@
from zerver.lib.types import DisplayRecipientT, UserDisplayRecipient
from zerver.models import Recipient, Stream, UserProfile, bulk_get_huddle_user_ids

if TYPE_CHECKING:
from django.db.models.query import _QuerySet as ValuesQuerySet

display_recipient_fields = [
"id",
"email",
Expand Down Expand Up @@ -101,7 +100,7 @@ def bulk_fetch_display_recipients(

def stream_query_function(
recipient_ids: List[int],
) -> "ValuesQuerySet[Stream, TinyStreamResult]":
) -> ValuesQuerySet[Stream, TinyStreamResult]:
stream_ids = [
recipient_id_to_type_pair_dict[recipient_id][1] for recipient_id in recipient_ids
]
Expand Down
7 changes: 2 additions & 5 deletions zerver/lib/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import zlib
from dataclasses import dataclass, field
from typing import (
TYPE_CHECKING,
Any,
Collection,
Dict,
Expand All @@ -24,6 +23,7 @@
from django.db.models import Max, Sum
from django.utils.timezone import now as timezone_now
from django.utils.translation import gettext as _
from django_stubs_ext import ValuesQuerySet
from psycopg2.sql import SQL

from analytics.lib.counts import COUNT_STATS
Expand Down Expand Up @@ -69,9 +69,6 @@
query_for_ids,
)

if TYPE_CHECKING:
from django.db.models.query import _QuerySet as ValuesQuerySet


class MessageDetailsDict(TypedDict, total=False):
type: str
Expand Down Expand Up @@ -891,7 +888,7 @@ def bulk_access_messages(

def bulk_access_messages_expect_usermessage(
user_profile_id: int, message_ids: Sequence[int]
) -> "ValuesQuerySet[UserMessage, int]":
) -> ValuesQuerySet[UserMessage, int]:
"""
Like bulk_access_messages, but faster and potentially stricter.
Expand Down
10 changes: 4 additions & 6 deletions zerver/lib/stream_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
from collections import defaultdict
from dataclasses import dataclass
from operator import itemgetter
from typing import TYPE_CHECKING, AbstractSet, Any, Collection, Dict, List, Optional, Set
from typing import AbstractSet, Any, Collection, Dict, List, Optional, Set

from django.db.models import Q, QuerySet

if TYPE_CHECKING:
from django.db.models.query import _QuerySet as ValuesQuerySet
from django_stubs_ext import ValuesQuerySet

from zerver.models import AlertWord, Realm, Recipient, Stream, Subscription, UserProfile

Expand Down Expand Up @@ -53,7 +51,7 @@ def get_active_subscriptions_for_stream_ids(stream_ids: Set[int]) -> QuerySet[Su

def get_subscribed_stream_ids_for_user(
user_profile: UserProfile,
) -> "ValuesQuerySet[Subscription, int]":
) -> ValuesQuerySet[Subscription, int]:
return Subscription.objects.filter(
user_profile_id=user_profile,
recipient__type=Recipient.STREAM,
Expand All @@ -63,7 +61,7 @@ def get_subscribed_stream_ids_for_user(

def get_subscribed_stream_recipient_ids_for_user(
user_profile: UserProfile,
) -> "ValuesQuerySet[Subscription, int]":
) -> ValuesQuerySet[Subscription, int]:
return Subscription.objects.filter(
user_profile_id=user_profile,
recipient__type=Recipient.STREAM,
Expand Down
8 changes: 3 additions & 5 deletions zerver/lib/user_groups.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
from typing import TYPE_CHECKING, Dict, Iterable, List, Sequence, TypedDict
from typing import Dict, Iterable, List, Sequence, TypedDict

from django.db import transaction
from django.db.models import QuerySet
from django.utils.translation import gettext as _
from django_cte import With
from django_stubs_ext import ValuesQuerySet

from zerver.lib.exceptions import JsonableError
from zerver.models import GroupGroupMembership, Realm, UserGroup, UserGroupMembership, UserProfile

if TYPE_CHECKING:
from django.db.models.query import _QuerySet as ValuesQuerySet


class UserGroupDict(TypedDict):
id: int
Expand Down Expand Up @@ -128,7 +126,7 @@ def create_user_group(

def get_user_group_direct_member_ids(
user_group: UserGroup,
) -> "ValuesQuerySet[UserGroupMembership, int]":
) -> ValuesQuerySet[UserGroupMembership, int]:
return UserGroupMembership.objects.filter(user_group=user_group).values_list(
"user_profile_id", flat=True
)
Expand Down
9 changes: 4 additions & 5 deletions zerver/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
from django.utils.translation import gettext as _
from django.utils.translation import gettext_lazy
from django_cte import CTEManager
from django_stubs_ext import StrPromise
from django_stubs_ext import StrPromise, ValuesQuerySet

from confirmation import settings as confirmation_settings
from zerver.lib import cache
Expand Down Expand Up @@ -119,7 +119,6 @@
if TYPE_CHECKING:
# We use ModelBackend only for typing. Importing it otherwise causes circular dependency.
from django.contrib.auth.backends import ModelBackend
from django.db.models.query import _QuerySet as ValuesQuerySet


class EmojiInfo(TypedDict):
Expand Down Expand Up @@ -160,10 +159,10 @@ def as_sql(


def query_for_ids(
query: "ValuesQuerySet[ModelT, RowT]",
query: ValuesQuerySet[ModelT, RowT],
user_ids: List[int],
field: str,
) -> "ValuesQuerySet[ModelT, RowT]":
) -> ValuesQuerySet[ModelT, RowT]:
"""
This function optimizes searches of the form
`user_profile_id in (1, 2, 3, 4)` by quickly
Expand Down Expand Up @@ -2786,7 +2785,7 @@ def get_huddle_recipient(user_profile_ids: Set[int]) -> Recipient:
return huddle.recipient


def get_huddle_user_ids(recipient: Recipient) -> "ValuesQuerySet[Subscription, int]":
def get_huddle_user_ids(recipient: Recipient) -> ValuesQuerySet["Subscription", int]:
assert recipient.type == Recipient.HUDDLE

return (
Expand Down

0 comments on commit 31208fc

Please sign in to comment.