ruff: Fix UP006 Use list instead of List for type annotation.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2024-07-11 17:30:17 -07:00
committed by Tim Abbott
parent c2214b3904
commit e08a24e47f
457 changed files with 3588 additions and 3857 deletions

View File

@@ -4,7 +4,7 @@ import unicodedata
from collections import defaultdict
from email.headerregistry import Address
from operator import itemgetter
from typing import Any, Dict, Iterable, List, Mapping, Optional, Sequence, Set, Tuple, TypedDict
from typing import Any, Iterable, Mapping, Optional, Sequence, TypedDict
import dateutil.parser as date_parser
from django.conf import settings
@@ -211,7 +211,7 @@ def is_administrator_role(role: int) -> bool:
return role in {UserProfile.ROLE_REALM_ADMINISTRATOR, UserProfile.ROLE_REALM_OWNER}
def bulk_get_cross_realm_bots() -> Dict[str, UserProfile]:
def bulk_get_cross_realm_bots() -> dict[str, UserProfile]:
emails = list(settings.CROSS_REALM_BOT_EMAILS)
# This should be just
@@ -231,7 +231,7 @@ def bulk_get_cross_realm_bots() -> Dict[str, UserProfile]:
return {user.email.lower(): user for user in users}
def user_ids_to_users(user_ids: Sequence[int], realm: Realm) -> List[UserProfile]:
def user_ids_to_users(user_ids: Sequence[int], realm: Realm) -> list[UserProfile]:
# TODO: Consider adding a flag to control whether deactivated
# users should be included.
@@ -354,7 +354,7 @@ class Account(TypedDict):
avatar: Optional[str]
def get_accounts_for_email(email: str) -> List[Account]:
def get_accounts_for_email(email: str) -> list[Account]:
profiles = (
UserProfile.objects.select_related("realm")
.filter(
@@ -380,7 +380,7 @@ def get_api_key(user_profile: UserProfile) -> str:
return user_profile.api_key
def get_all_api_keys(user_profile: UserProfile) -> List[str]:
def get_all_api_keys(user_profile: UserProfile) -> list[str]:
# Users can only have one API key for now
return [user_profile.api_key]
@@ -408,7 +408,7 @@ def validate_user_custom_profile_field(
def validate_user_custom_profile_data(
realm_id: int, profile_data: List[ProfileDataElementUpdateDict]
realm_id: int, profile_data: list[ProfileDataElementUpdateDict]
) -> None:
# This function validate all custom field values according to their field type.
for item in profile_data:
@@ -466,7 +466,7 @@ class APIUserDict(TypedDict):
delivery_email: Optional[str]
bot_type: NotRequired[Optional[int]]
bot_owner_id: NotRequired[Optional[int]]
profile_data: NotRequired[Optional[Dict[str, Any]]]
profile_data: NotRequired[Optional[dict[str, Any]]]
is_system_bot: NotRequired[bool]
max_message_id: NotRequired[int]
@@ -477,7 +477,7 @@ def format_user_row(
row: RawUserDict,
client_gravatar: bool,
user_avatar_url_field_optional: bool,
custom_profile_field_data: Optional[Dict[str, Any]] = None,
custom_profile_field_data: Optional[dict[str, Any]] = None,
) -> APIUserDict:
"""Formats a user row returned by a database fetch using
.values(*realm_user_dict_fields) into a dictionary representation
@@ -637,8 +637,8 @@ def check_can_access_user(
def get_inaccessible_user_ids(
target_user_ids: List[int], acting_user: Optional[UserProfile]
) -> Set[int]:
target_user_ids: list[int], acting_user: Optional[UserProfile]
) -> set[int]:
if check_user_can_access_all_users(acting_user):
return set()
@@ -696,7 +696,7 @@ def get_inaccessible_user_ids(
return inaccessible_user_ids
def get_user_ids_who_can_access_user(target_user: UserProfile) -> List[int]:
def get_user_ids_who_can_access_user(target_user: UserProfile) -> list[int]:
# We assume that caller only needs active users here, since
# this function is used to get users to send events and to
# send presence update.
@@ -719,8 +719,8 @@ def get_user_ids_who_can_access_user(target_user: UserProfile) -> List[int]:
def get_subscribers_of_target_user_subscriptions(
target_users: List[UserProfile], include_deactivated_users_for_huddles: bool = False
) -> Dict[int, Set[int]]:
target_users: list[UserProfile], include_deactivated_users_for_huddles: bool = False
) -> dict[int, set[int]]:
target_user_ids = [user.id for user in target_users]
target_user_subscriptions = (
Subscription.objects.filter(
@@ -733,7 +733,7 @@ def get_subscribers_of_target_user_subscriptions(
)
target_users_subbed_recipient_ids = set()
target_user_subscriptions_dict: Dict[int, Set[int]] = defaultdict(set)
target_user_subscriptions_dict: dict[int, set[int]] = defaultdict(set)
for user_profile_id, sub_rows in itertools.groupby(
target_user_subscriptions, itemgetter("user_profile_id")
@@ -762,14 +762,14 @@ def get_subscribers_of_target_user_subscriptions(
"recipient_id"
).values("user_profile_id", "recipient_id")
subscribers_dict_by_recipient_ids: Dict[int, Set[int]] = defaultdict(set)
subscribers_dict_by_recipient_ids: dict[int, set[int]] = defaultdict(set)
for recipient_id, sub_rows in itertools.groupby(
subs_in_target_user_subscriptions, itemgetter("recipient_id")
):
user_ids = {row["user_profile_id"] for row in sub_rows}
subscribers_dict_by_recipient_ids[recipient_id] = user_ids
users_subbed_to_target_user_subscriptions_dict: Dict[int, Set[int]] = defaultdict(set)
users_subbed_to_target_user_subscriptions_dict: dict[int, set[int]] = defaultdict(set)
for user_id in target_user_ids:
target_user_subbed_recipients = target_user_subscriptions_dict[user_id]
for recipient_id in target_user_subbed_recipients:
@@ -781,8 +781,8 @@ def get_subscribers_of_target_user_subscriptions(
def get_users_involved_in_dms_with_target_users(
target_users: List[UserProfile], realm: Realm, include_deactivated_users: bool = False
) -> Dict[int, Set[int]]:
target_users: list[UserProfile], realm: Realm, include_deactivated_users: bool = False
) -> dict[int, set[int]]:
target_user_ids = [user.id for user in target_users]
direct_messages_recipient_users = (
@@ -801,7 +801,7 @@ def get_users_involved_in_dms_with_target_users(
id__in=list(direct_messages_recipient_users_set), is_active=True
).values_list("id", flat=True)
direct_message_participants_dict: Dict[int, Set[int]] = defaultdict(set)
direct_message_participants_dict: dict[int, set[int]] = defaultdict(set)
for sender_id, message_rows in itertools.groupby(
direct_messages_recipient_users, itemgetter("sender_id")
):
@@ -858,7 +858,7 @@ def user_profile_to_user_row(user_profile: UserProfile) -> RawUserDict:
@cache_with_key(get_cross_realm_dicts_key)
def get_cross_realm_dicts() -> List[APIUserDict]:
def get_cross_realm_dicts() -> list[APIUserDict]:
user_dict = bulk_get_cross_realm_bots()
users = sorted(user_dict.values(), key=lambda user: user.full_name)
result = []
@@ -914,7 +914,7 @@ def get_data_for_inaccessible_user(realm: Realm, user_id: int) -> APIUserDict:
def get_accessible_user_ids(
realm: Realm, user_profile: UserProfile, include_deactivated_users: bool = False
) -> List[int]:
) -> list[int]:
subscribers_dict_of_target_user_subscriptions = get_subscribers_of_target_user_subscriptions(
[user_profile], include_deactivated_users_for_huddles=include_deactivated_users
)
@@ -935,7 +935,7 @@ def get_accessible_user_ids(
def get_user_dicts_in_realm(
realm: Realm, user_profile: Optional[UserProfile]
) -> Tuple[List[RawUserDict], List[APIUserDict]]:
) -> tuple[list[RawUserDict], list[APIUserDict]]:
group_allowed_to_access_all_users = realm.can_access_all_users_group
assert group_allowed_to_access_all_users is not None
@@ -948,8 +948,8 @@ def get_user_dicts_in_realm(
realm, user_profile, include_deactivated_users=True
)
accessible_user_dicts: List[RawUserDict] = []
inaccessible_user_dicts: List[APIUserDict] = []
accessible_user_dicts: list[RawUserDict] = []
inaccessible_user_dicts: list[APIUserDict] = []
for user_dict in all_user_dicts:
if user_dict["id"] in accessible_user_ids or user_dict["is_bot"]:
accessible_user_dicts.append(user_dict)
@@ -961,8 +961,8 @@ def get_user_dicts_in_realm(
def get_custom_profile_field_values(
custom_profile_field_values: Iterable[CustomProfileFieldValue],
) -> Dict[int, Dict[str, Any]]:
profiles_by_user_id: Dict[int, Dict[str, Any]] = defaultdict(dict)
) -> dict[int, dict[str, Any]]:
profiles_by_user_id: dict[int, dict[str, Any]] = defaultdict(dict)
for profile_field in custom_profile_field_values:
user_id = profile_field.user_profile_id
if profile_field.field.is_renderable():
@@ -986,7 +986,7 @@ def get_users_for_api(
user_avatar_url_field_optional: bool,
include_custom_profile_fields: bool = True,
user_list_incomplete: bool = False,
) -> Dict[int, APIUserDict]:
) -> dict[int, APIUserDict]:
"""Fetches data about the target user(s) appropriate for sending to
acting_user via the standard format for the Zulip API. If
target_user is None, we fetch all users in the realm.
@@ -995,8 +995,8 @@ def get_users_for_api(
custom_profile_field_data = None
# target_user is an optional parameter which is passed when user data of a specific user
# is required. It is 'None' otherwise.
accessible_user_dicts: List[RawUserDict] = []
inaccessible_user_dicts: List[APIUserDict] = []
accessible_user_dicts: list[RawUserDict] = []
inaccessible_user_dicts: list[APIUserDict] = []
if target_user is not None:
accessible_user_dicts = [user_profile_to_user_row(target_user)]
else:
@@ -1056,7 +1056,7 @@ def is_2fa_verified(user: UserProfile) -> bool:
return is_verified(user)
def get_users_with_access_to_real_email(user_profile: UserProfile) -> List[int]:
def get_users_with_access_to_real_email(user_profile: UserProfile) -> list[int]:
if not user_access_restricted_in_realm(user_profile):
active_users = user_profile.realm.get_active_users()
else: