Use type hinting generics in standard collections (#19046)

aka PEP 585, added in Python 3.9

 - https://peps.python.org/pep-0585/
 - https://docs.astral.sh/ruff/rules/non-pep585-annotation/
This commit is contained in:
Andrew Ferrazzutti
2025-10-22 16:48:19 -05:00
committed by GitHub
parent cba3a814c6
commit fc244bb592
539 changed files with 4599 additions and 5066 deletions
+2 -2
View File
@@ -94,7 +94,7 @@ The Pusher instance also calls out to various utilities for generating payloads
"""
import abc
from typing import TYPE_CHECKING, Any, Dict, Optional
from typing import TYPE_CHECKING, Any, Optional
import attr
@@ -131,7 +131,7 @@ class PusherConfig:
# while the "set_device_id_for_pushers" background update is running.
access_token: Optional[int]
def as_dict(self) -> Dict[str, Any]:
def as_dict(self) -> dict[str, Any]:
"""Information that can be retrieved about a pusher after creation."""
return {
"app_display_name": self.app_display_name,
+13 -17
View File
@@ -24,13 +24,9 @@ from typing import (
TYPE_CHECKING,
Any,
Collection,
Dict,
FrozenSet,
List,
Mapping,
Optional,
Sequence,
Tuple,
Union,
cast,
)
@@ -237,7 +233,7 @@ class BulkPushRuleEvaluator:
event: EventBase,
context: EventContext,
event_id_to_event: Mapping[str, EventBase],
) -> Tuple[dict, Optional[int]]:
) -> tuple[dict, Optional[int]]:
"""
Given an event and an event context, get the power level event relevant to the event
and the power level of the sender of the event.
@@ -309,13 +305,13 @@ class BulkPushRuleEvaluator:
async def _related_events(
self, event: EventBase
) -> Dict[str, Dict[str, JsonValue]]:
) -> dict[str, dict[str, JsonValue]]:
"""Fetches the related events for 'event'. Sets the im.vector.is_falling_back key if the event is from a fallback relation
Returns:
Mapping of relation type to flattened events.
"""
related_events: Dict[str, Dict[str, JsonValue]] = {}
related_events: dict[str, dict[str, JsonValue]] = {}
if self._related_event_match_enabled:
related_event_id = event.content.get("m.relates_to", {}).get("event_id")
relation_type = event.content.get("m.relates_to", {}).get("rel_type")
@@ -352,7 +348,7 @@ class BulkPushRuleEvaluator:
return related_events
async def action_for_events_by_user(
self, events_and_context: List[EventPersistencePair]
self, events_and_context: list[EventPersistencePair]
) -> None:
"""Given a list of events and their associated contexts, evaluate the push rules
for each event, check if the message should increment the unread count, and
@@ -394,7 +390,7 @@ class BulkPushRuleEvaluator:
count_as_unread = _should_count_as_unread(event, context)
rules_by_user = await self._get_rules_for_event(event)
actions_by_user: Dict[str, Collection[Union[Mapping, str]]] = {}
actions_by_user: dict[str, Collection[Union[Mapping, str]]] = {}
# Gather a bunch of info in parallel.
#
@@ -409,7 +405,7 @@ class BulkPushRuleEvaluator:
profiles,
) = await make_deferred_yieldable(
cast(
"Deferred[Tuple[int, Tuple[dict, Optional[int]], Dict[str, Dict[str, JsonValue]], Mapping[str, ProfileInfo]]]",
"Deferred[tuple[int, tuple[dict, Optional[int]], dict[str, dict[str, JsonValue]], Mapping[str, ProfileInfo]]]",
gather_results(
(
run_in_background( # type: ignore[call-overload]
@@ -481,7 +477,7 @@ class BulkPushRuleEvaluator:
self.hs.config.experimental.msc4306_enabled,
)
msc4306_thread_subscribers: Optional[FrozenSet[str]] = None
msc4306_thread_subscribers: Optional[frozenset[str]] = None
if self.hs.config.experimental.msc4306_enabled and thread_id != MAIN_TIMELINE:
# pull out, in batch, all local subscribers to this thread
# (in the common case, they will all be getting processed for push
@@ -556,9 +552,9 @@ class BulkPushRuleEvaluator:
)
MemberMap = Dict[str, Optional[EventIdMembership]]
Rule = Dict[str, dict]
RulesByUser = Dict[str, List[Rule]]
MemberMap = dict[str, Optional[EventIdMembership]]
Rule = dict[str, dict]
RulesByUser = dict[str, list[Rule]]
StateGroup = Union[object, int]
@@ -572,9 +568,9 @@ def _is_simple_value(value: Any) -> bool:
def _flatten_dict(
d: Union[EventBase, Mapping[str, Any]],
prefix: Optional[List[str]] = None,
result: Optional[Dict[str, JsonValue]] = None,
) -> Dict[str, JsonValue]:
prefix: Optional[list[str]] = None,
result: Optional[dict[str, JsonValue]] = None,
) -> dict[str, JsonValue]:
"""
Given a JSON dictionary (or event) which might contain sub dictionaries,
flatten it into a single layer dictionary by combining the keys & sub-keys.
+7 -7
View File
@@ -20,7 +20,7 @@
#
import copy
from typing import Any, Dict, List, Optional
from typing import Any, Optional
from synapse.push.rulekinds import PRIORITY_CLASS_INVERSE_MAP, PRIORITY_CLASS_MAP
from synapse.synapse_rust.push import FilteredPushRules, PushRule
@@ -29,11 +29,11 @@ from synapse.types import UserID
def format_push_rules_for_user(
user: UserID, ruleslist: FilteredPushRules
) -> Dict[str, Dict[str, List[Dict[str, Any]]]]:
) -> dict[str, dict[str, list[dict[str, Any]]]]:
"""Converts a list of rawrules and a enabled map into nested dictionaries
to match the Matrix client-server format for push rules"""
rules: Dict[str, Dict[str, List[Dict[str, Any]]]] = {"global": {}}
rules: dict[str, dict[str, list[dict[str, Any]]]] = {"global": {}}
rules["global"] = _add_empty_priority_class_arrays(rules["global"])
@@ -70,7 +70,7 @@ def format_push_rules_for_user(
return rules
def _convert_type_to_value(rule_or_cond: Dict[str, Any], user: UserID) -> None:
def _convert_type_to_value(rule_or_cond: dict[str, Any], user: UserID) -> None:
for type_key in ("pattern", "value"):
type_value = rule_or_cond.pop(f"{type_key}_type", None)
if type_value == "user_id":
@@ -79,14 +79,14 @@ def _convert_type_to_value(rule_or_cond: Dict[str, Any], user: UserID) -> None:
rule_or_cond[type_key] = user.localpart
def _add_empty_priority_class_arrays(d: Dict[str, list]) -> Dict[str, list]:
def _add_empty_priority_class_arrays(d: dict[str, list]) -> dict[str, list]:
for pc in PRIORITY_CLASS_MAP.keys():
d[pc] = []
return d
def _rule_to_template(rule: PushRule) -> Optional[Dict[str, Any]]:
templaterule: Dict[str, Any]
def _rule_to_template(rule: PushRule) -> Optional[dict[str, Any]]:
templaterule: dict[str, Any]
unscoped_rule_id = _rule_id_from_namespaced(rule.rule_id)
+3 -3
View File
@@ -20,7 +20,7 @@
#
import logging
from typing import TYPE_CHECKING, Dict, List, Optional
from typing import TYPE_CHECKING, Optional
from twisted.internet.error import AlreadyCalled, AlreadyCancelled
from twisted.internet.interfaces import IDelayedCall
@@ -71,7 +71,7 @@ class EmailPusher(Pusher):
self.store = self.hs.get_datastores().main
self.email = pusher_config.pushkey
self.timed_call: Optional[IDelayedCall] = None
self.throttle_params: Dict[str, ThrottleParams] = {}
self.throttle_params: dict[str, ThrottleParams] = {}
self._inited = False
self._is_processing = False
@@ -324,7 +324,7 @@ class EmailPusher(Pusher):
)
async def send_notification(
self, push_actions: List[EmailPushAction], reason: EmailReason
self, push_actions: list[EmailPushAction], reason: EmailReason
) -> None:
logger.info("Sending notif email for user %r", self.user_id)
+4 -4
View File
@@ -21,7 +21,7 @@
import logging
import random
import urllib.parse
from typing import TYPE_CHECKING, Dict, List, Optional, Union
from typing import TYPE_CHECKING, Optional, Union
from prometheus_client import Counter
@@ -68,7 +68,7 @@ http_badges_failed_counter = Counter(
)
def tweaks_for_actions(actions: List[Union[str, Dict]]) -> JsonMapping:
def tweaks_for_actions(actions: list[Union[str, dict]]) -> JsonMapping:
"""
Converts a list of actions into a `tweaks` dict (which can then be passed to
the push gateway).
@@ -396,7 +396,7 @@ class HttpPusher(Pusher):
content: JsonDict,
tweaks: Optional[JsonMapping] = None,
default_payload: Optional[JsonMapping] = None,
) -> Union[bool, List[str]]:
) -> Union[bool, list[str]]:
"""Send a notification to the registered push gateway, with `content` being
the content of the `notification` top property specified in the spec.
Note that the `devices` property will be added with device-specific
@@ -453,7 +453,7 @@ class HttpPusher(Pusher):
event: EventBase,
tweaks: JsonMapping,
badge: int,
) -> Union[bool, List[str]]:
) -> Union[bool, list[str]]:
"""Send a notification to the registered push gateway by building it
from an event.
+12 -12
View File
@@ -21,7 +21,7 @@
import logging
import urllib.parse
from typing import TYPE_CHECKING, Dict, Iterable, List, Optional, TypeVar
from typing import TYPE_CHECKING, Iterable, Optional, TypeVar
import bleach
import jinja2
@@ -287,7 +287,7 @@ class Mailer:
notif_events = await self.store.get_events([pa.event_id for pa in push_actions])
notifs_by_room: Dict[str, List[EmailPushAction]] = {}
notifs_by_room: dict[str, list[EmailPushAction]] = {}
for pa in push_actions:
notifs_by_room.setdefault(pa.room_id, []).append(pa)
@@ -317,7 +317,7 @@ class Mailer:
# actually sort our so-called rooms_in_order list, most recent room first
rooms_in_order.sort(key=lambda r: -(notifs_by_room[r][-1].received_ts or 0))
rooms: List[RoomVars] = []
rooms: list[RoomVars] = []
for r in rooms_in_order:
roomvars = await self._get_room_vars(
@@ -417,7 +417,7 @@ class Mailer:
room_id: str,
user_id: str,
notifs: Iterable[EmailPushAction],
notif_events: Dict[str, EventBase],
notif_events: dict[str, EventBase],
room_state_ids: StateMap[str],
) -> RoomVars:
"""
@@ -665,9 +665,9 @@ class Mailer:
async def _make_summary_text_single_room(
self,
room_id: str,
notifs: List[EmailPushAction],
notifs: list[EmailPushAction],
room_state_ids: StateMap[str],
notif_events: Dict[str, EventBase],
notif_events: dict[str, EventBase],
user_id: str,
) -> str:
"""
@@ -781,9 +781,9 @@ class Mailer:
async def _make_summary_text(
self,
notifs_by_room: Dict[str, List[EmailPushAction]],
room_state_ids: Dict[str, StateMap[str]],
notif_events: Dict[str, EventBase],
notifs_by_room: dict[str, list[EmailPushAction]],
room_state_ids: dict[str, StateMap[str]],
notif_events: dict[str, EventBase],
reason: EmailReason,
) -> str:
"""
@@ -814,9 +814,9 @@ class Mailer:
async def _make_summary_text_from_member_events(
self,
room_id: str,
notifs: List[EmailPushAction],
notifs: list[EmailPushAction],
room_state_ids: StateMap[str],
notif_events: Dict[str, EventBase],
notif_events: dict[str, EventBase],
) -> str:
"""
Make a summary text for the email when only a single room has notifications.
@@ -995,7 +995,7 @@ def safe_text(raw_text: str) -> Markup:
)
def deduped_ordered_list(it: Iterable[T]) -> List[T]:
def deduped_ordered_list(it: Iterable[T]) -> list[T]:
seen = set()
ret = []
for item in it:
+3 -3
View File
@@ -21,7 +21,7 @@
import logging
import re
from typing import TYPE_CHECKING, Dict, Iterable, Optional
from typing import TYPE_CHECKING, Iterable, Optional
from synapse.api.constants import EventTypes, Membership
from synapse.events import EventBase
@@ -205,8 +205,8 @@ def name_from_member_event(member_event: EventBase) -> str:
return member_event.state_key
def _state_as_two_level_dict(state: StateMap[str]) -> Dict[str, Dict[str, str]]:
ret: Dict[str, Dict[str, str]] = {}
def _state_as_two_level_dict(state: StateMap[str]) -> dict[str, dict[str, str]]:
ret: dict[str, dict[str, str]] = {}
for k, v in state.items():
ret.setdefault(k[0], {})[k[1]] = v
return ret
+2 -3
View File
@@ -18,7 +18,6 @@
# [This file includes modifications made by New Vector Limited]
#
#
from typing import Dict
from synapse.api.constants import EventTypes, Membership
from synapse.events import EventBase
@@ -56,8 +55,8 @@ async def get_badge_count(store: DataStore, user_id: str, group_by_room: bool) -
async def get_context_for_event(
storage: StorageControllers, ev: EventBase, user_id: str
) -> Dict[str, str]:
ctx: Dict[str, str] = {}
) -> dict[str, str]:
ctx: dict[str, str] = {}
if ev.internal_metadata.outlier:
# We don't have state for outliers, so we can't compute the context
+4 -4
View File
@@ -18,7 +18,7 @@
# [This file includes modifications made by New Vector Limited]
#
#
from typing import List, Optional, TypedDict
from typing import Optional, TypedDict
class EmailReason(TypedDict, total=False):
@@ -91,7 +91,7 @@ class NotifVars(TypedDict):
link: str
ts: Optional[int]
messages: List[MessageVars]
messages: list[MessageVars]
class RoomVars(TypedDict):
@@ -110,7 +110,7 @@ class RoomVars(TypedDict):
title: Optional[str]
hash: int
invite: bool
notifs: List[NotifVars]
notifs: list[NotifVars]
link: str
avatar_url: Optional[str]
@@ -137,5 +137,5 @@ class TemplateVars(TypedDict, total=False):
user_display_name: str
unsubscribe_link: str
summary_text: str
rooms: List[RoomVars]
rooms: list[RoomVars]
reason: EmailReason
+3 -3
View File
@@ -20,7 +20,7 @@
#
import logging
from typing import TYPE_CHECKING, Callable, Dict, Optional
from typing import TYPE_CHECKING, Callable, Optional
from synapse.push import Pusher, PusherConfig
from synapse.push.emailpusher import EmailPusher
@@ -38,13 +38,13 @@ class PusherFactory:
self.hs = hs
self.config = hs.config
self.pusher_types: Dict[str, Callable[[HomeServer, PusherConfig], Pusher]] = {
self.pusher_types: dict[str, Callable[[HomeServer, PusherConfig], Pusher]] = {
"http": HttpPusher
}
logger.info("email enable notifs: %r", hs.config.email.email_enable_notifs)
if hs.config.email.email_enable_notifs:
self.mailers: Dict[str, Mailer] = {}
self.mailers: dict[str, Mailer] = {}
self._notif_template_html = hs.config.email.email_notif_template_html
self._notif_template_text = hs.config.email.email_notif_template_text
+2 -2
View File
@@ -20,7 +20,7 @@
#
import logging
from typing import TYPE_CHECKING, Dict, Iterable, Optional
from typing import TYPE_CHECKING, Iterable, Optional
from prometheus_client import Gauge
@@ -100,7 +100,7 @@ class PusherPool:
self._last_room_stream_id_seen = self.store.get_room_max_stream_ordering()
# map from user id to app_id:pushkey to pusher
self.pushers: Dict[str, Dict[str, Pusher]] = {}
self.pushers: dict[str, dict[str, Pusher]] = {}
self._account_validity_handler = hs.get_account_validity_handler()