1
0

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 17:48:19 -04:00
committed by GitHub
parent cba3a814c6
commit fc244bb592
539 changed files with 4599 additions and 5066 deletions

View File

@@ -20,7 +20,7 @@
#
import logging
from typing import Awaitable, Callable, List, Optional, Tuple
from typing import Awaitable, Callable, Optional
from twisted.web.http import Request
@@ -33,15 +33,15 @@ ON_USER_LOGIN_CALLBACK = Callable[[str, Optional[str], Optional[str]], Awaitable
# Temporary hooks to allow for a transition from `/_matrix/client` endpoints
# to `/_synapse/client/account_validity`. See `register_callbacks` below.
ON_LEGACY_SEND_MAIL_CALLBACK = Callable[[str], Awaitable]
ON_LEGACY_RENEW_CALLBACK = Callable[[str], Awaitable[Tuple[bool, bool, int]]]
ON_LEGACY_RENEW_CALLBACK = Callable[[str], Awaitable[tuple[bool, bool, int]]]
ON_LEGACY_ADMIN_REQUEST = Callable[[Request], Awaitable]
class AccountValidityModuleApiCallbacks:
def __init__(self) -> None:
self.is_user_expired_callbacks: List[IS_USER_EXPIRED_CALLBACK] = []
self.on_user_registration_callbacks: List[ON_USER_REGISTRATION_CALLBACK] = []
self.on_user_login_callbacks: List[ON_USER_LOGIN_CALLBACK] = []
self.is_user_expired_callbacks: list[IS_USER_EXPIRED_CALLBACK] = []
self.on_user_registration_callbacks: list[ON_USER_REGISTRATION_CALLBACK] = []
self.on_user_login_callbacks: list[ON_USER_LOGIN_CALLBACK] = []
self.on_legacy_send_mail_callback: Optional[ON_LEGACY_SEND_MAIL_CALLBACK] = None
self.on_legacy_renew_callback: Optional[ON_LEGACY_RENEW_CALLBACK] = None

View File

@@ -13,7 +13,7 @@
#
import logging
from typing import TYPE_CHECKING, Awaitable, Callable, List, Optional
from typing import TYPE_CHECKING, Awaitable, Callable, Optional
from synapse.config.repository import MediaUploadLimit
from synapse.types import JsonDict
@@ -30,7 +30,7 @@ GET_MEDIA_CONFIG_FOR_USER_CALLBACK = Callable[[str], Awaitable[Optional[JsonDict
IS_USER_ALLOWED_TO_UPLOAD_MEDIA_OF_SIZE_CALLBACK = Callable[[str, int], Awaitable[bool]]
GET_MEDIA_UPLOAD_LIMITS_FOR_USER_CALLBACK = Callable[
[str], Awaitable[Optional[List[MediaUploadLimit]]]
[str], Awaitable[Optional[list[MediaUploadLimit]]]
]
ON_MEDIA_UPLOAD_LIMIT_EXCEEDED_CALLBACK = Callable[
@@ -42,16 +42,16 @@ class MediaRepositoryModuleApiCallbacks:
def __init__(self, hs: "HomeServer") -> None:
self.server_name = hs.hostname
self.clock = hs.get_clock()
self._get_media_config_for_user_callbacks: List[
self._get_media_config_for_user_callbacks: list[
GET_MEDIA_CONFIG_FOR_USER_CALLBACK
] = []
self._is_user_allowed_to_upload_media_of_size_callbacks: List[
self._is_user_allowed_to_upload_media_of_size_callbacks: list[
IS_USER_ALLOWED_TO_UPLOAD_MEDIA_OF_SIZE_CALLBACK
] = []
self._get_media_upload_limits_for_user_callbacks: List[
self._get_media_upload_limits_for_user_callbacks: list[
GET_MEDIA_UPLOAD_LIMITS_FOR_USER_CALLBACK
] = []
self._on_media_upload_limit_exceeded_callbacks: List[
self._on_media_upload_limit_exceeded_callbacks: list[
ON_MEDIA_UPLOAD_LIMIT_EXCEEDED_CALLBACK
] = []
@@ -117,7 +117,7 @@ class MediaRepositoryModuleApiCallbacks:
async def get_media_upload_limits_for_user(
self, user_id: str
) -> Optional[List[MediaUploadLimit]]:
) -> Optional[list[MediaUploadLimit]]:
"""
Get the first non-None list of MediaUploadLimits for the user from the registered callbacks.
If a list is returned it will be sorted in descending order of duration.
@@ -128,7 +128,7 @@ class MediaRepositoryModuleApiCallbacks:
name=f"{callback.__module__}.{callback.__qualname__}",
server_name=self.server_name,
):
res: Optional[List[MediaUploadLimit]] = await delay_cancellation(
res: Optional[list[MediaUploadLimit]] = await delay_cancellation(
callback(user_id)
)
if res is not None: # to allow [] to be returned meaning no limit

View File

@@ -13,7 +13,7 @@
#
import logging
from typing import TYPE_CHECKING, Awaitable, Callable, List, Optional
from typing import TYPE_CHECKING, Awaitable, Callable, Optional
import attr
@@ -45,7 +45,7 @@ class RatelimitModuleApiCallbacks:
def __init__(self, hs: "HomeServer") -> None:
self.server_name = hs.hostname
self.clock = hs.get_clock()
self._get_ratelimit_override_for_user_callbacks: List[
self._get_ratelimit_override_for_user_callbacks: list[
GET_RATELIMIT_OVERRIDE_FOR_USER_CALLBACK
] = []

View File

@@ -29,10 +29,8 @@ from typing import (
Awaitable,
Callable,
Collection,
List,
Literal,
Optional,
Tuple,
Union,
cast,
)
@@ -63,7 +61,7 @@ CHECK_EVENT_FOR_SPAM_CALLBACK = Callable[
# disappear without warning depending on the results of ongoing
# experiments.
# Use this to return additional information as part of an error.
Tuple[Codes, JsonDict],
tuple[Codes, JsonDict],
# Deprecated
bool,
]
@@ -83,7 +81,7 @@ USER_MAY_JOIN_ROOM_CALLBACK = Callable[
# disappear without warning depending on the results of ongoing
# experiments.
# Use this to return additional information as part of an error.
Tuple[Codes, JsonDict],
tuple[Codes, JsonDict],
# Deprecated
bool,
]
@@ -99,7 +97,7 @@ USER_MAY_INVITE_CALLBACK = Callable[
# disappear without warning depending on the results of ongoing
# experiments.
# Use this to return additional information as part of an error.
Tuple[Codes, JsonDict],
tuple[Codes, JsonDict],
# Deprecated
bool,
]
@@ -115,7 +113,7 @@ FEDERATED_USER_MAY_INVITE_CALLBACK = Callable[
# disappear without warning depending on the results of ongoing
# experiments.
# Use this to return additional information as part of an error.
Tuple[Codes, JsonDict],
tuple[Codes, JsonDict],
# Deprecated
bool,
]
@@ -131,7 +129,7 @@ USER_MAY_SEND_3PID_INVITE_CALLBACK = Callable[
# disappear without warning depending on the results of ongoing
# experiments.
# Use this to return additional information as part of an error.
Tuple[Codes, JsonDict],
tuple[Codes, JsonDict],
# Deprecated
bool,
]
@@ -144,7 +142,7 @@ USER_MAY_CREATE_ROOM_CALLBACK_RETURN_VALUE = Union[
# disappear without warning depending on the results of ongoing
# experiments.
# Use this to return additional information as part of an error.
Tuple[Codes, JsonDict],
tuple[Codes, JsonDict],
# Deprecated
bool,
]
@@ -167,7 +165,7 @@ USER_MAY_CREATE_ROOM_ALIAS_CALLBACK = Callable[
# disappear without warning depending on the results of ongoing
# experiments.
# Use this to return additional information as part of an error.
Tuple[Codes, JsonDict],
tuple[Codes, JsonDict],
# Deprecated
bool,
]
@@ -183,7 +181,7 @@ USER_MAY_PUBLISH_ROOM_CALLBACK = Callable[
# disappear without warning depending on the results of ongoing
# experiments.
# Use this to return additional information as part of an error.
Tuple[Codes, JsonDict],
tuple[Codes, JsonDict],
# Deprecated
bool,
]
@@ -199,7 +197,7 @@ USER_MAY_SEND_STATE_EVENT_CALLBACK = Callable[
# disappear without warning depending on the results of ongoing
# experiments.
# Use this to return additional information as part of an error.
Tuple[Codes, JsonDict],
tuple[Codes, JsonDict],
]
],
]
@@ -211,7 +209,7 @@ LEGACY_CHECK_REGISTRATION_FOR_SPAM_CALLBACK = Callable[
[
Optional[dict],
Optional[str],
Collection[Tuple[str, str]],
Collection[tuple[str, str]],
],
Awaitable[RegistrationBehaviour],
]
@@ -219,7 +217,7 @@ CHECK_REGISTRATION_FOR_SPAM_CALLBACK = Callable[
[
Optional[dict],
Optional[str],
Collection[Tuple[str, str]],
Collection[tuple[str, str]],
Optional[str],
],
Awaitable[RegistrationBehaviour],
@@ -234,7 +232,7 @@ CHECK_MEDIA_FILE_FOR_SPAM_CALLBACK = Callable[
# disappear without warning depending on the results of ongoing
# experiments.
# Use this to return additional information as part of an error.
Tuple[Codes, JsonDict],
tuple[Codes, JsonDict],
# Deprecated
bool,
]
@@ -245,7 +243,7 @@ CHECK_LOGIN_FOR_SPAM_CALLBACK = Callable[
str,
Optional[str],
Optional[str],
Collection[Tuple[Optional[str], str]],
Collection[tuple[Optional[str], str]],
Optional[str],
],
Awaitable[
@@ -256,7 +254,7 @@ CHECK_LOGIN_FOR_SPAM_CALLBACK = Callable[
# disappear without warning depending on the results of ongoing
# experiments.
# Use this to return additional information as part of an error.
Tuple[Codes, JsonDict],
tuple[Codes, JsonDict],
]
],
]
@@ -266,7 +264,7 @@ def load_legacy_spam_checkers(hs: "synapse.server.HomeServer") -> None:
"""Wrapper that loads spam checkers configured using the old configuration, and
registers the spam checker hooks they implement.
"""
spam_checkers: List[Any] = []
spam_checkers: list[Any] = []
api = hs.get_module_api()
for module, config in hs.config.spamchecker.spam_checkers:
# Older spam checkers don't accept the `api` argument, so we
@@ -312,7 +310,7 @@ def load_legacy_spam_checkers(hs: "synapse.server.HomeServer") -> None:
def wrapper(
email_threepid: Optional[dict],
username: Optional[str],
request_info: Collection[Tuple[str, str]],
request_info: Collection[tuple[str, str]],
auth_provider_id: Optional[str],
) -> Union[Awaitable[RegistrationBehaviour], RegistrationBehaviour]:
# Assertion required because mypy can't prove we won't
@@ -359,36 +357,36 @@ class SpamCheckerModuleApiCallbacks:
self.server_name = hs.hostname
self.clock = hs.get_clock()
self._check_event_for_spam_callbacks: List[CHECK_EVENT_FOR_SPAM_CALLBACK] = []
self._should_drop_federated_event_callbacks: List[
self._check_event_for_spam_callbacks: list[CHECK_EVENT_FOR_SPAM_CALLBACK] = []
self._should_drop_federated_event_callbacks: list[
SHOULD_DROP_FEDERATED_EVENT_CALLBACK
] = []
self._user_may_join_room_callbacks: List[USER_MAY_JOIN_ROOM_CALLBACK] = []
self._user_may_invite_callbacks: List[USER_MAY_INVITE_CALLBACK] = []
self._federated_user_may_invite_callbacks: List[
self._user_may_join_room_callbacks: list[USER_MAY_JOIN_ROOM_CALLBACK] = []
self._user_may_invite_callbacks: list[USER_MAY_INVITE_CALLBACK] = []
self._federated_user_may_invite_callbacks: list[
FEDERATED_USER_MAY_INVITE_CALLBACK
] = []
self._user_may_send_3pid_invite_callbacks: List[
self._user_may_send_3pid_invite_callbacks: list[
USER_MAY_SEND_3PID_INVITE_CALLBACK
] = []
self._user_may_create_room_callbacks: List[USER_MAY_CREATE_ROOM_CALLBACK] = []
self._user_may_send_state_event_callbacks: List[
self._user_may_create_room_callbacks: list[USER_MAY_CREATE_ROOM_CALLBACK] = []
self._user_may_send_state_event_callbacks: list[
USER_MAY_SEND_STATE_EVENT_CALLBACK
] = []
self._user_may_create_room_alias_callbacks: List[
self._user_may_create_room_alias_callbacks: list[
USER_MAY_CREATE_ROOM_ALIAS_CALLBACK
] = []
self._user_may_publish_room_callbacks: List[USER_MAY_PUBLISH_ROOM_CALLBACK] = []
self._check_username_for_spam_callbacks: List[
self._user_may_publish_room_callbacks: list[USER_MAY_PUBLISH_ROOM_CALLBACK] = []
self._check_username_for_spam_callbacks: list[
CHECK_USERNAME_FOR_SPAM_CALLBACK
] = []
self._check_registration_for_spam_callbacks: List[
self._check_registration_for_spam_callbacks: list[
CHECK_REGISTRATION_FOR_SPAM_CALLBACK
] = []
self._check_media_file_for_spam_callbacks: List[
self._check_media_file_for_spam_callbacks: list[
CHECK_MEDIA_FILE_FOR_SPAM_CALLBACK
] = []
self._check_login_for_spam_callbacks: List[CHECK_LOGIN_FOR_SPAM_CALLBACK] = []
self._check_login_for_spam_callbacks: list[CHECK_LOGIN_FOR_SPAM_CALLBACK] = []
def register_callbacks(
self,
@@ -471,7 +469,7 @@ class SpamCheckerModuleApiCallbacks:
@trace
async def check_event_for_spam(
self, event: "synapse.events.EventBase"
) -> Union[Tuple[Codes, JsonDict], str]:
) -> Union[tuple[Codes, JsonDict], str]:
"""Checks if a given event is considered "spammy" by this server.
If the server considers an event spammy, then it will be rejected if
@@ -561,7 +559,7 @@ class SpamCheckerModuleApiCallbacks:
async def user_may_join_room(
self, user_id: str, room_id: str, is_invited: bool
) -> Union[Tuple[Codes, JsonDict], Literal["NOT_SPAM"]]:
) -> Union[tuple[Codes, JsonDict], Literal["NOT_SPAM"]]:
"""Checks if a given users is allowed to join a room.
Not called when a user creates a room.
@@ -605,7 +603,7 @@ class SpamCheckerModuleApiCallbacks:
async def user_may_invite(
self, inviter_userid: str, invitee_userid: str, room_id: str
) -> Union[Tuple[Codes, dict], Literal["NOT_SPAM"]]:
) -> Union[tuple[Codes, dict], Literal["NOT_SPAM"]]:
"""Checks if a given user may send an invite
Args:
@@ -650,7 +648,7 @@ class SpamCheckerModuleApiCallbacks:
async def federated_user_may_invite(
self, event: "synapse.events.EventBase"
) -> Union[Tuple[Codes, dict], Literal["NOT_SPAM"]]:
) -> Union[tuple[Codes, dict], Literal["NOT_SPAM"]]:
"""Checks if a given user may send an invite
Args:
@@ -691,7 +689,7 @@ class SpamCheckerModuleApiCallbacks:
async def user_may_send_3pid_invite(
self, inviter_userid: str, medium: str, address: str, room_id: str
) -> Union[Tuple[Codes, dict], Literal["NOT_SPAM"]]:
) -> Union[tuple[Codes, dict], Literal["NOT_SPAM"]]:
"""Checks if a given user may invite a given threepid into the room
Note that if the threepid is already associated with a Matrix user ID, Synapse
@@ -739,7 +737,7 @@ class SpamCheckerModuleApiCallbacks:
async def user_may_create_room(
self, userid: str, room_config: JsonDict
) -> Union[Tuple[Codes, dict], Literal["NOT_SPAM"]]:
) -> Union[tuple[Codes, dict], Literal["NOT_SPAM"]]:
"""Checks if a given user may create a room
Args:
@@ -805,7 +803,7 @@ class SpamCheckerModuleApiCallbacks:
event_type: str,
state_key: str,
content: JsonDict,
) -> Union[Tuple[Codes, dict], Literal["NOT_SPAM"]]:
) -> Union[tuple[Codes, dict], Literal["NOT_SPAM"]]:
"""Checks if a given user may create a room with a given visibility
Args:
user_id: The ID of the user attempting to create a room
@@ -838,7 +836,7 @@ class SpamCheckerModuleApiCallbacks:
async def user_may_create_room_alias(
self, userid: str, room_alias: RoomAlias
) -> Union[Tuple[Codes, dict], Literal["NOT_SPAM"]]:
) -> Union[tuple[Codes, dict], Literal["NOT_SPAM"]]:
"""Checks if a given user may create a room alias
Args:
@@ -876,7 +874,7 @@ class SpamCheckerModuleApiCallbacks:
async def user_may_publish_room(
self, userid: str, room_id: str
) -> Union[Tuple[Codes, dict], Literal["NOT_SPAM"]]:
) -> Union[tuple[Codes, dict], Literal["NOT_SPAM"]]:
"""Checks if a given user may publish a room to the directory
Args:
@@ -964,7 +962,7 @@ class SpamCheckerModuleApiCallbacks:
self,
email_threepid: Optional[dict],
username: Optional[str],
request_info: Collection[Tuple[str, str]],
request_info: Collection[tuple[str, str]],
auth_provider_id: Optional[str] = None,
) -> RegistrationBehaviour:
"""Checks if we should allow the given registration request.
@@ -1000,7 +998,7 @@ class SpamCheckerModuleApiCallbacks:
@trace
async def check_media_file_for_spam(
self, file_wrapper: ReadableFileWrapper, file_info: FileInfo
) -> Union[Tuple[Codes, dict], Literal["NOT_SPAM"]]:
) -> Union[tuple[Codes, dict], Literal["NOT_SPAM"]]:
"""Checks if a piece of newly uploaded media should be blocked.
This will be called for local uploads, downloads of remote media, each
@@ -1062,9 +1060,9 @@ class SpamCheckerModuleApiCallbacks:
user_id: str,
device_id: Optional[str],
initial_display_name: Optional[str],
request_info: Collection[Tuple[Optional[str], str]],
request_info: Collection[tuple[Optional[str], str]],
auth_provider_id: Optional[str] = None,
) -> Union[Tuple[Codes, dict], Literal["NOT_SPAM"]]:
) -> Union[tuple[Codes, dict], Literal["NOT_SPAM"]]:
"""Checks if we should allow the given registration request.
Args:

View File

@@ -19,7 +19,7 @@
#
#
import logging
from typing import TYPE_CHECKING, Any, Awaitable, Callable, List, Optional, Tuple
from typing import TYPE_CHECKING, Any, Awaitable, Callable, Optional
from twisted.internet.defer import CancelledError
@@ -37,7 +37,7 @@ logger = logging.getLogger(__name__)
CHECK_EVENT_ALLOWED_CALLBACK = Callable[
[EventBase, StateMap[EventBase]], Awaitable[Tuple[bool, Optional[dict]]]
[EventBase, StateMap[EventBase]], Awaitable[tuple[bool, Optional[dict]]]
]
ON_CREATE_ROOM_CALLBACK = Callable[[Requester, dict, bool], Awaitable]
CHECK_THREEPID_CAN_BE_INVITED_CALLBACK = Callable[
@@ -93,7 +93,7 @@ def load_legacy_third_party_event_rules(hs: "HomeServer") -> None:
async def wrap_check_event_allowed(
event: EventBase,
state_events: StateMap[EventBase],
) -> Tuple[bool, Optional[dict]]:
) -> tuple[bool, Optional[dict]]:
# Assertion required because mypy can't prove we won't change
# `f` back to `None`. See
# https://mypy.readthedocs.io/en/latest/common_issues.html#narrowing-and-inner-functions
@@ -159,30 +159,30 @@ class ThirdPartyEventRulesModuleApiCallbacks:
self.store = hs.get_datastores().main
self._storage_controllers = hs.get_storage_controllers()
self._check_event_allowed_callbacks: List[CHECK_EVENT_ALLOWED_CALLBACK] = []
self._on_create_room_callbacks: List[ON_CREATE_ROOM_CALLBACK] = []
self._check_threepid_can_be_invited_callbacks: List[
self._check_event_allowed_callbacks: list[CHECK_EVENT_ALLOWED_CALLBACK] = []
self._on_create_room_callbacks: list[ON_CREATE_ROOM_CALLBACK] = []
self._check_threepid_can_be_invited_callbacks: list[
CHECK_THREEPID_CAN_BE_INVITED_CALLBACK
] = []
self._check_visibility_can_be_modified_callbacks: List[
self._check_visibility_can_be_modified_callbacks: list[
CHECK_VISIBILITY_CAN_BE_MODIFIED_CALLBACK
] = []
self._on_new_event_callbacks: List[ON_NEW_EVENT_CALLBACK] = []
self._check_can_shutdown_room_callbacks: List[
self._on_new_event_callbacks: list[ON_NEW_EVENT_CALLBACK] = []
self._check_can_shutdown_room_callbacks: list[
CHECK_CAN_SHUTDOWN_ROOM_CALLBACK
] = []
self._check_can_deactivate_user_callbacks: List[
self._check_can_deactivate_user_callbacks: list[
CHECK_CAN_DEACTIVATE_USER_CALLBACK
] = []
self._on_profile_update_callbacks: List[ON_PROFILE_UPDATE_CALLBACK] = []
self._on_user_deactivation_status_changed_callbacks: List[
self._on_profile_update_callbacks: list[ON_PROFILE_UPDATE_CALLBACK] = []
self._on_user_deactivation_status_changed_callbacks: list[
ON_USER_DEACTIVATION_STATUS_CHANGED_CALLBACK
] = []
self._on_threepid_bind_callbacks: List[ON_THREEPID_BIND_CALLBACK] = []
self._on_add_user_third_party_identifier_callbacks: List[
self._on_threepid_bind_callbacks: list[ON_THREEPID_BIND_CALLBACK] = []
self._on_add_user_third_party_identifier_callbacks: list[
ON_ADD_USER_THIRD_PARTY_IDENTIFIER_CALLBACK
] = []
self._on_remove_user_third_party_identifier_callbacks: List[
self._on_remove_user_third_party_identifier_callbacks: list[
ON_REMOVE_USER_THIRD_PARTY_IDENTIFIER_CALLBACK
] = []
@@ -261,7 +261,7 @@ class ThirdPartyEventRulesModuleApiCallbacks:
self,
event: EventBase,
context: UnpersistedEventContextBase,
) -> Tuple[bool, Optional[dict]]:
) -> tuple[bool, Optional[dict]]:
"""Check if a provided event should be allowed in the given context.
The module can return: