From 889996a4e543272ec7426e666db58ff5ca0931de Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 29 Jan 2024 18:48:39 +0200 Subject: [PATCH] Update Black to 2024 style and Python 3.10 target --- .github/workflows/python-package.yml | 2 +- .pre-commit-config.yaml | 4 ++-- dev-requirements.txt | 2 +- mautrix/bridge/config.py | 20 ++++++++++++-------- mautrix/client/api/filtering.py | 8 +++++--- mautrix/client/api/rooms.py | 9 ++++++--- mautrix/crypto/encrypt_megolm.py | 6 +++--- mautrix/types/event/type.pyi | 1 + mautrix/util/bridge_state.py | 7 ++++--- mautrix/util/message_send_checkpoint.py | 15 +++++++++------ mautrix/util/utf16_surrogate.py | 8 +++++--- pyproject.toml | 2 +- 12 files changed, 50 insertions(+), 34 deletions(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 41ebfd8f..5a71c970 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -56,7 +56,7 @@ jobs: - uses: psf/black@stable with: src: "./mautrix" - version: "23.11.0" + version: "24.1.1" - name: pre-commit run: | pip install pre-commit diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 7bed3e1e..a056ffdb 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,13 +8,13 @@ repos: - id: check-yaml - id: check-added-large-files - repo: https://github.com/psf/black - rev: 23.11.0 + rev: 24.1.1 hooks: - id: black language_version: python3 files: ^mautrix/.*\.pyi?$ - repo: https://github.com/PyCQA/isort - rev: 5.12.0 + rev: 5.13.2 hooks: - id: isort files: ^mautrix/.*\.pyi?$ diff --git a/dev-requirements.txt b/dev-requirements.txt index 5cd14c23..bb8c2a0a 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -1,3 +1,3 @@ pre-commit>=2.10.1,<3 isort>=5.10.1,<6 -black>=23,<24 +black>=24,<25 diff --git a/mautrix/bridge/config.py b/mautrix/bridge/config.py index 1705e914..b98ebc52 100644 --- a/mautrix/bridge/config.py +++ b/mautrix/bridge/config.py @@ -200,14 +200,18 @@ def namespaces(self) -> dict[str, list[dict[str, Any]]]: "regex": re.escape(f"@{username_format}:{homeserver}").replace(regex_ph, ".*"), } ], - "aliases": [ - { - "exclusive": True, - "regex": re.escape(f"#{alias_format}:{homeserver}").replace(regex_ph, ".*"), - } - ] - if alias_format - else [], + "aliases": ( + [ + { + "exclusive": True, + "regex": re.escape(f"#{alias_format}:{homeserver}").replace( + regex_ph, ".*" + ), + } + ] + if alias_format + else [] + ), } def generate_registration(self) -> None: diff --git a/mautrix/client/api/filtering.py b/mautrix/client/api/filtering.py index 09889ec5..b8df1f36 100644 --- a/mautrix/client/api/filtering.py +++ b/mautrix/client/api/filtering.py @@ -50,9 +50,11 @@ async def create_filter(self, filter_params: Filter) -> FilterID: resp = await self.api.request( Method.POST, Path.v3.user[self.mxid].filter, - filter_params.serialize() - if isinstance(filter_params, Serializable) - else filter_params, + ( + filter_params.serialize() + if isinstance(filter_params, Serializable) + else filter_params + ), ) try: return resp["filter_id"] diff --git a/mautrix/client/api/rooms.py b/mautrix/client/api/rooms.py index 9f935430..a488fba6 100644 --- a/mautrix/client/api/rooms.py +++ b/mautrix/client/api/rooms.py @@ -354,9 +354,12 @@ async def join_room( except KeyError: raise MatrixResponseError("`room_id` not in response.") - fill_member_event_callback: Callable[ - [RoomID, UserID, MemberStateEventContent], Awaitable[MemberStateEventContent | None] - ] | None + fill_member_event_callback: ( + Callable[ + [RoomID, UserID, MemberStateEventContent], Awaitable[MemberStateEventContent | None] + ] + | None + ) async def fill_member_event( self, room_id: RoomID, user_id: UserID, content: MemberStateEventContent diff --git a/mautrix/crypto/encrypt_megolm.py b/mautrix/crypto/encrypt_megolm.py index 227fd3fe..459bbf1f 100644 --- a/mautrix/crypto/encrypt_megolm.py +++ b/mautrix/crypto/encrypt_megolm.py @@ -95,9 +95,9 @@ async def _encrypt_megolm_event( { "room_id": room_id, "type": event_type.serialize(), - "content": content.serialize() - if isinstance(content, Serializable) - else content, + "content": ( + content.serialize() if isinstance(content, Serializable) else content + ), } ) ) diff --git a/mautrix/types/event/type.pyi b/mautrix/types/event/type.pyi index bee0fde0..22922288 100644 --- a/mautrix/types/event/type.pyi +++ b/mautrix/types/event/type.pyi @@ -18,6 +18,7 @@ class EventType(Serializable): ACCOUNT_DATA = "account_data" EPHEMERAL = "ephemeral" TO_DEVICE = "to_device" + _by_event_type: ClassVar[dict[str, EventType]] ROOM_CANONICAL_ALIAS: "EventType" diff --git a/mautrix/util/bridge_state.py b/mautrix/util/bridge_state.py index dcb42977..d28448bf 100644 --- a/mautrix/util/bridge_state.py +++ b/mautrix/util/bridge_state.py @@ -115,9 +115,10 @@ async def send(self, url: str, token: str, log: logging.Logger, log_sent: bool = self.send_attempts_ += 1 headers = {"Authorization": f"Bearer {token}", "User-Agent": HTTPAPI.default_ua} try: - async with aiohttp.ClientSession() as sess, sess.post( - url, json=self.serialize(), headers=headers - ) as resp: + async with ( + aiohttp.ClientSession() as sess, + sess.post(url, json=self.serialize(), headers=headers) as resp, + ): if not 200 <= resp.status < 300: text = await resp.text() text = text.replace("\n", "\\n") diff --git a/mautrix/util/message_send_checkpoint.py b/mautrix/util/message_send_checkpoint.py index 5dd023b5..ee0c17f3 100644 --- a/mautrix/util/message_send_checkpoint.py +++ b/mautrix/util/message_send_checkpoint.py @@ -57,12 +57,15 @@ async def send(self, endpoint: str, as_token: str, log: logging.Logger) -> None: return try: headers = {"Authorization": f"Bearer {as_token}", "User-Agent": HTTPAPI.default_ua} - async with aiohttp.ClientSession() as sess, sess.post( - endpoint, - json={"checkpoints": [self.serialize()]}, - headers=headers, - timeout=ClientTimeout(30), - ) as resp: + async with ( + aiohttp.ClientSession() as sess, + sess.post( + endpoint, + json={"checkpoints": [self.serialize()]}, + headers=headers, + timeout=ClientTimeout(30), + ) as resp, + ): if not 200 <= resp.status < 300: text = await resp.text() text = text.replace("\n", "\\n") diff --git a/mautrix/util/utf16_surrogate.py b/mautrix/util/utf16_surrogate.py index d202e15e..92dc49c2 100644 --- a/mautrix/util/utf16_surrogate.py +++ b/mautrix/util/utf16_surrogate.py @@ -16,9 +16,11 @@ def add(text: str) -> str: The text with surrogate pairs. """ return "".join( - "".join(chr(y) for y in struct.unpack("