From 1a704c1b69d3976ed0fb94346f758c18b855a8a5 Mon Sep 17 00:00:00 2001 From: Jonathan de Jong Date: Mon, 5 Apr 2021 17:10:55 +0200 Subject: [PATCH 1/5] synapse/crypto --- synapse/crypto/__init__.py | 1 - synapse/crypto/event_signing.py | 7 +++---- synapse/crypto/keyring.py | 11 +++++------ 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/synapse/crypto/__init__.py b/synapse/crypto/__init__.py index bfebb0f644f4..5e83dba2ed6f 100644 --- a/synapse/crypto/__init__.py +++ b/synapse/crypto/__init__.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2014-2016 OpenMarket Ltd # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/synapse/crypto/event_signing.py b/synapse/crypto/event_signing.py index 8fb116ae182c..2822fc338a60 100644 --- a/synapse/crypto/event_signing.py +++ b/synapse/crypto/event_signing.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright 2014-2016 OpenMarket Ltd # Copyright 2020 The Matrix.org Foundation C.I.C. @@ -53,13 +52,13 @@ def check_event_content_hash( # nb it might be a frozendict or a dict if not isinstance(hashes, collections.abc.Mapping): raise SynapseError( - 400, "Malformed 'hashes': %s" % (type(hashes),), Codes.UNAUTHORIZED + 400, "Malformed 'hashes': {}".format(type(hashes)), Codes.UNAUTHORIZED ) if name not in hashes: raise SynapseError( 400, - "Algorithm %s not in hashes %s" % (name, list(hashes)), + "Algorithm {} not in hashes {}".format(name, list(hashes)), Codes.UNAUTHORIZED, ) message_hash_base64 = hashes[name] @@ -67,7 +66,7 @@ def check_event_content_hash( message_hash_bytes = decode_base64(message_hash_base64) except Exception: raise SynapseError( - 400, "Invalid base64: %s" % (message_hash_base64,), Codes.UNAUTHORIZED + 400, f"Invalid base64: {message_hash_base64}", Codes.UNAUTHORIZED ) return message_hash_bytes == expected_hash diff --git a/synapse/crypto/keyring.py b/synapse/crypto/keyring.py index d5fb51513b59..25148682635e 100644 --- a/synapse/crypto/keyring.py +++ b/synapse/crypto/keyring.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2014-2016 OpenMarket Ltd # Copyright 2017, 2018 New Vector Ltd # @@ -209,7 +208,7 @@ def process(verify_request: VerifyJsonRequest) -> defer.Deferred: return defer.fail( SynapseError( 400, - "Not signed by %s" % (verify_request.server_name,), + f"Not signed by {verify_request.server_name}", Codes.UNAUTHORIZED, ) ) @@ -319,7 +318,7 @@ async def wait_for_previous_lookups(self, server_names: Iterable[str]) -> None: loop_count, ) with PreserveLoggingContext(): - await defer.DeferredList((w[1] for w in wait_on)) + await defer.DeferredList(w[1] for w in wait_on) loop_count += 1 @@ -369,7 +368,7 @@ async def do_iterations(): verify_request.key_ready.errback, SynapseError( 401, - "Failed to find any key to satisfy %s" % (rq_str,), + f"Failed to find any key to satisfy {rq_str}", Codes.UNAUTHORIZED, ), ) @@ -688,7 +687,7 @@ async def get_server_verify_key_v2_indirect( # these both have str() representations which we can't really improve upon raise KeyLookupError(str(e)) except HttpResponseException as e: - raise KeyLookupError("Remote server returned an error: %s" % (e,)) + raise KeyLookupError(f"Remote server returned an error: {e}") keys = {} # type: Dict[str, Dict[str, FetchKeyResult]] added_keys = [] # type: List[Tuple[str, str, FetchKeyResult]] @@ -856,7 +855,7 @@ async def get_server_verify_key_v2_direct( # upon raise KeyLookupError(str(e)) except HttpResponseException as e: - raise KeyLookupError("Remote server returned an error: %s" % (e,)) + raise KeyLookupError(f"Remote server returned an error: {e}") assert isinstance(response, dict) if response["server_name"] != server_name: From 23a2693264902f22777e1c04f5b3738a7f77f4c4 Mon Sep 17 00:00:00 2001 From: Jonathan de Jong Date: Mon, 5 Apr 2021 17:12:12 +0200 Subject: [PATCH 2/5] synapse/events --- synapse/events/__init__.py | 13 ++++++------- synapse/events/builder.py | 3 +-- synapse/events/snapshot.py | 1 - synapse/events/spamcheck.py | 1 - synapse/events/third_party_rules.py | 1 - synapse/events/utils.py | 9 +++------ synapse/events/validator.py | 13 ++++++------- 7 files changed, 16 insertions(+), 25 deletions(-) diff --git a/synapse/events/__init__.py b/synapse/events/__init__.py index 8f6b955d17b7..e251205afc31 100644 --- a/synapse/events/__init__.py +++ b/synapse/events/__init__.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2014-2016 OpenMarket Ltd # Copyright 2019 New Vector Ltd # Copyright 2020 The Matrix.org Foundation C.I.C. @@ -63,7 +62,7 @@ def __get__(self, instance, owner=None): # because that would omit any *earlier* exceptions). # raise AttributeError( - "'%s' has no '%s' property" % (type(instance), self.key) + "'{}' has no '{}' property".format(type(instance), self.key) ) from e1.__context__ def __set__(self, instance, v): @@ -74,7 +73,7 @@ def __delete__(self, instance): del instance._dict[self.key] except KeyError as e1: raise AttributeError( - "'%s' has no '%s' property" % (type(instance), self.key) + "'{}' has no '{}' property".format(type(instance), self.key) ) from e1.__context__ @@ -284,7 +283,7 @@ def get_pdu_json(self, time_now=None) -> JsonDict: return pdu_json def __set__(self, instance, value): - raise AttributeError("Unrecognized attribute %s" % (instance,)) + raise AttributeError(f"Unrecognized attribute {instance}") def __getitem__(self, field): return self._dict[field] @@ -372,7 +371,7 @@ def __str__(self): return self.__repr__() def __repr__(self): - return "" % ( + return "".format( self.get("event_id", None), self.get("type", None), self.get("state_key", None), @@ -455,7 +454,7 @@ def __str__(self): return self.__repr__() def __repr__(self): - return "<%s event_id=%r, type=%r, state_key=%r>" % ( + return "<{} event_id={!r}, type={!r}, state_key={!r}>".format( self.__class__.__name__, self.event_id, self.get("type", None), @@ -501,7 +500,7 @@ def _event_type_from_format_version(format_version: int) -> Type[EventBase]: elif format_version == EventFormatVersions.V3: return FrozenEventV3 else: - raise Exception("No event format %r" % (format_version,)) + raise Exception(f"No event format {format_version!r}") def make_event_from_dict( diff --git a/synapse/events/builder.py b/synapse/events/builder.py index c1c0426f6ea0..4e6e2f656062 100644 --- a/synapse/events/builder.py +++ b/synapse/events/builder.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2014-2016 OpenMarket Ltd # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -245,7 +244,7 @@ def create_local_event_from_event_dict( format_version = room_version.event_format if format_version not in KNOWN_EVENT_FORMAT_VERSIONS: - raise Exception("No event format defined for version %r" % (format_version,)) + raise Exception(f"No event format defined for version {format_version!r}") if internal_metadata_dict is None: internal_metadata_dict = {} diff --git a/synapse/events/snapshot.py b/synapse/events/snapshot.py index 7295df74fed6..f8d898c3b1d1 100644 --- a/synapse/events/snapshot.py +++ b/synapse/events/snapshot.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2014-2016 OpenMarket Ltd # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/synapse/events/spamcheck.py b/synapse/events/spamcheck.py index a9185987a237..c727b48c1ef5 100644 --- a/synapse/events/spamcheck.py +++ b/synapse/events/spamcheck.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2017 New Vector Ltd # Copyright 2019 The Matrix.org Foundation C.I.C. # diff --git a/synapse/events/third_party_rules.py b/synapse/events/third_party_rules.py index 9767d2394050..f7944fd8344f 100644 --- a/synapse/events/third_party_rules.py +++ b/synapse/events/third_party_rules.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2019 The Matrix.org Foundation C.I.C. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/synapse/events/utils.py b/synapse/events/utils.py index 0f8a3b5ad839..89768b71c6e1 100644 --- a/synapse/events/utils.py +++ b/synapse/events/utils.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2014-2016 OpenMarket Ltd # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -453,7 +452,7 @@ def copy_power_levels_contents( TypeError if the input does not look like a valid power levels event content """ if not isinstance(old_power_levels, collections.abc.Mapping): - raise TypeError("Not a valid power-levels content: %r" % (old_power_levels,)) + raise TypeError(f"Not a valid power-levels content: {old_power_levels!r}") power_levels = {} for k, v in old_power_levels.items(): @@ -467,13 +466,11 @@ def copy_power_levels_contents( for k1, v1 in v.items(): # we should only have one level of nesting if not isinstance(v1, int): - raise TypeError( - "Invalid power_levels value for %s.%s: %r" % (k, k1, v1) - ) + raise TypeError(f"Invalid power_levels value for {k}.{k1}: {v1!r}") h[k1] = v1 continue - raise TypeError("Invalid power_levels value for %s: %r" % (k, v)) + raise TypeError(f"Invalid power_levels value for {k}: {v!r}") return power_levels diff --git a/synapse/events/validator.py b/synapse/events/validator.py index f8f3b1a31e0b..aeea35c6a034 100644 --- a/synapse/events/validator.py +++ b/synapse/events/validator.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2014-2016 OpenMarket Ltd # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -51,14 +50,14 @@ def validate_new(self, event: EventBase, config: HomeServerConfig): for k in required: if not hasattr(event, k): - raise SynapseError(400, "Event does not have key %s" % (k,)) + raise SynapseError(400, f"Event does not have key {k}") # Check that the following keys have string values event_strings = ["origin"] for s in event_strings: if not isinstance(getattr(event, s), str): - raise SynapseError(400, "'%s' not a string type" % (s,)) + raise SynapseError(400, f"'{s}' not a string type") # Depending on the room version, ensure the data is spec compliant JSON. if event.room_version.strict_canonicaljson: @@ -141,7 +140,7 @@ def validate_builder(self, event: Union[EventBase, EventBuilder]): for s in strings: if not isinstance(getattr(event, s), str): - raise SynapseError(400, "Not '%s' a string type" % (s,)) + raise SynapseError(400, f"Not '{s}' a string type") RoomID.from_string(event.room_id) UserID.from_string(event.sender) @@ -179,10 +178,10 @@ def validate_builder(self, event: Union[EventBase, EventBuilder]): def _ensure_strings(self, d, keys): for s in keys: if s not in d: - raise SynapseError(400, "'%s' not in content" % (s,)) + raise SynapseError(400, f"'{s}' not in content") if not isinstance(d[s], str): - raise SynapseError(400, "'%s' not a string type" % (s,)) + raise SynapseError(400, f"'{s}' not a string type") def _ensure_state_event(self, event): if not event.is_state(): - raise SynapseError(400, "'%s' must be state events" % (event.type,)) + raise SynapseError(400, f"'{event.type}' must be state events") From 024c074da128215ce59333fd76d246da396971bb Mon Sep 17 00:00:00 2001 From: Jonathan de Jong Date: Mon, 5 Apr 2021 17:13:40 +0200 Subject: [PATCH 3/5] synapse/federation --- synapse/federation/__init__.py | 1 - synapse/federation/federation_base.py | 5 ++--- synapse/federation/federation_client.py | 3 +-- synapse/federation/federation_server.py | 9 ++++----- synapse/federation/persistence.py | 1 - synapse/federation/send_queue.py | 3 +-- synapse/federation/sender/__init__.py | 1 - synapse/federation/sender/per_destination_queue.py | 1 - synapse/federation/sender/transaction_manager.py | 1 - synapse/federation/transport/__init__.py | 1 - synapse/federation/transport/client.py | 1 - synapse/federation/transport/server.py | 1 - synapse/federation/units.py | 1 - 13 files changed, 8 insertions(+), 21 deletions(-) diff --git a/synapse/federation/__init__.py b/synapse/federation/__init__.py index f5f0bdfca3ec..46300cba2564 100644 --- a/synapse/federation/__init__.py +++ b/synapse/federation/__init__.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2014-2016 OpenMarket Ltd # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/synapse/federation/federation_base.py b/synapse/federation/federation_base.py index 383737520afa..04997f224361 100644 --- a/synapse/federation/federation_base.py +++ b/synapse/federation/federation_base.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2015, 2016 OpenMarket Ltd # Copyright 2020 The Matrix.org Foundation C.I.C. # @@ -209,7 +208,7 @@ def _check_sigs_on_pdus( ) def sender_err(e, pdu_to_check): - errmsg = "event id %s: unable to verify signature for sender %s: %s" % ( + errmsg = "event id {}: unable to verify signature for sender {}: {}".format( pdu_to_check.pdu.event_id, pdu_to_check.sender_domain, e.getErrorMessage(), @@ -298,7 +297,7 @@ def event_from_pdu_json( depth = pdu_json["depth"] if not isinstance(depth, int): - raise SynapseError(400, "Depth %r not an intger" % (depth,), Codes.BAD_JSON) + raise SynapseError(400, f"Depth {depth!r} not an intger", Codes.BAD_JSON) if depth < 0: raise SynapseError(400, "Depth too small", Codes.BAD_JSON) diff --git a/synapse/federation/federation_client.py b/synapse/federation/federation_client.py index afdb5bf2fafb..100ef60469b6 100644 --- a/synapse/federation/federation_client.py +++ b/synapse/federation/federation_client.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2015, 2016 OpenMarket Ltd # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -530,7 +529,7 @@ async def _try_destination_list( "Failed to %s via %s", description, destination, exc_info=True ) - raise SynapseError(502, "Failed to %s via any server" % (description,)) + raise SynapseError(502, f"Failed to {description} via any server") async def make_membership_event( self, diff --git a/synapse/federation/federation_server.py b/synapse/federation/federation_server.py index 71cb120ef76b..7661d232b4da 100644 --- a/synapse/federation/federation_server.py +++ b/synapse/federation/federation_server.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2015, 2016 OpenMarket Ltd # Copyright 2018 New Vector Ltd # Copyright 2019 Matrix.org Federation C.I.C @@ -636,7 +635,7 @@ async def on_claim_client_keys( "Claimed one-time-keys: %s", ",".join( ( - "%s for %s:%s" % (key_id, user_id, device_id) + f"{key_id} for {user_id}:{device_id}" for user_id, user_keys in json_result.items() for device_id, device_keys in user_keys.items() for key_id, _ in device_keys.items() @@ -889,7 +888,7 @@ def register_edu_handler( the EDU contents. """ if edu_type in self.edu_handlers: - raise KeyError("Already have an EDU handler for %s" % (edu_type,)) + raise KeyError(f"Already have an EDU handler for {edu_type}") logger.info("Registering federation EDU handler for %r", edu_type) @@ -909,7 +908,7 @@ def register_query_handler( on and the result used as the response to the query request. """ if query_type in self.query_handlers: - raise KeyError("Already have a Query handler for %s" % (query_type,)) + raise KeyError(f"Already have a Query handler for {query_type}") logger.info("Registering federation query handler for %r", query_type) @@ -983,4 +982,4 @@ async def on_query(self, query_type: str, args: dict) -> JsonDict: # Uh oh, no handler! Let's raise an exception so the request returns an # error. logger.warning("No handler registered for query type %s", query_type) - raise NotFoundError("No handler for Query type '%s'" % (query_type,)) + raise NotFoundError(f"No handler for Query type '{query_type}'") diff --git a/synapse/federation/persistence.py b/synapse/federation/persistence.py index ce5fc758f0e6..2f9c9bc2cdc8 100644 --- a/synapse/federation/persistence.py +++ b/synapse/federation/persistence.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2014-2016 OpenMarket Ltd # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/synapse/federation/send_queue.py b/synapse/federation/send_queue.py index 0c18c49abb70..83d99665232a 100644 --- a/synapse/federation/send_queue.py +++ b/synapse/federation/send_queue.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2014-2016 OpenMarket Ltd # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -110,7 +109,7 @@ def __init__(self, hs: "HomeServer"): # changes. ARGH. def register(name: str, queue: Sized) -> None: LaterGauge( - "synapse_federation_send_queue_%s_size" % (queue_name,), + f"synapse_federation_send_queue_{queue_name}_size", "", [], lambda: len(queue), diff --git a/synapse/federation/sender/__init__.py b/synapse/federation/sender/__init__.py index 8babb1ebbe4e..d4a71ac43279 100644 --- a/synapse/federation/sender/__init__.py +++ b/synapse/federation/sender/__init__.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2019 New Vector Ltd # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/synapse/federation/sender/per_destination_queue.py b/synapse/federation/sender/per_destination_queue.py index e9c8a9f20a66..3b053ebcfb08 100644 --- a/synapse/federation/sender/per_destination_queue.py +++ b/synapse/federation/sender/per_destination_queue.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2014-2016 OpenMarket Ltd # Copyright 2019 New Vector Ltd # diff --git a/synapse/federation/sender/transaction_manager.py b/synapse/federation/sender/transaction_manager.py index 07b740c2f2fc..12fe3a719bc3 100644 --- a/synapse/federation/sender/transaction_manager.py +++ b/synapse/federation/sender/transaction_manager.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2019 New Vector Ltd # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/synapse/federation/transport/__init__.py b/synapse/federation/transport/__init__.py index 5db733af98ca..3c9a0f694482 100644 --- a/synapse/federation/transport/__init__.py +++ b/synapse/federation/transport/__init__.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2014-2016 OpenMarket Ltd # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/synapse/federation/transport/client.py b/synapse/federation/transport/client.py index 6aee47c43116..ada322a81e14 100644 --- a/synapse/federation/transport/client.py +++ b/synapse/federation/transport/client.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2014-2016 OpenMarket Ltd # Copyright 2018 New Vector Ltd # diff --git a/synapse/federation/transport/server.py b/synapse/federation/transport/server.py index 84e39c5a468d..b65e5e66d0f5 100644 --- a/synapse/federation/transport/server.py +++ b/synapse/federation/transport/server.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2014-2016 OpenMarket Ltd # Copyright 2018 New Vector Ltd # Copyright 2019 The Matrix.org Foundation C.I.C. diff --git a/synapse/federation/units.py b/synapse/federation/units.py index b662c4262120..3eb236d11908 100644 --- a/synapse/federation/units.py +++ b/synapse/federation/units.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2014-2016 OpenMarket Ltd # # Licensed under the Apache License, Version 2.0 (the "License"); From 6d1edb487203fc2ba57963a8326f65ff3a4ed7bc Mon Sep 17 00:00:00 2001 From: Jonathan de Jong Date: Mon, 5 Apr 2021 17:14:24 +0200 Subject: [PATCH 4/5] synapse/groups --- synapse/groups/attestations.py | 1 - synapse/groups/groups_server.py | 5 ++--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/synapse/groups/attestations.py b/synapse/groups/attestations.py index 368c44708dac..d2fc8be5f571 100644 --- a/synapse/groups/attestations.py +++ b/synapse/groups/attestations.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2017 Vector Creations Ltd # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/synapse/groups/groups_server.py b/synapse/groups/groups_server.py index 4b16a4ac29ea..897cf71679e5 100644 --- a/synapse/groups/groups_server.py +++ b/synapse/groups/groups_server.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2017 Vector Creations Ltd # Copyright 2018 New Vector Ltd # Copyright 2019 Michael Telatynski <7t3chguy@gmail.com> @@ -554,13 +553,13 @@ async def update_group_profile( if not isinstance(value, str): raise SynapseError( 400, - "%r value is not a string" % (keyname,), + f"{keyname!r} value is not a string", errcode=Codes.INVALID_PARAM, ) if len(value) > max_length: raise SynapseError( 400, - "Invalid %s parameter" % (keyname,), + f"Invalid {keyname} parameter", errcode=Codes.INVALID_PARAM, ) profile[keyname] = value From 89fc4acb2af91fed2302e4f2089ca1fa0e305753 Mon Sep 17 00:00:00 2001 From: Jonathan de Jong Date: Mon, 5 Apr 2021 18:15:01 +0200 Subject: [PATCH 5/5] news --- changelog.d/9749.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/9749.misc diff --git a/changelog.d/9749.misc b/changelog.d/9749.misc new file mode 100644 index 000000000000..7140799fdab7 --- /dev/null +++ b/changelog.d/9749.misc @@ -0,0 +1 @@ +Run `pyupgrade --py36-plus` over the codebase. \ No newline at end of file