Skip to content

Commit

Permalink
Format files with Ruff (#17643)
Browse files Browse the repository at this point in the history
I thought ruff check would also format, but it doesn't.

This runs ruff format in CI and dev scripts. The first commit is just a
run of `ruff format .` in the root directory.
  • Loading branch information
sandhose committed Sep 2, 2024
1 parent 709b736 commit 7d52ce7
Show file tree
Hide file tree
Showing 152 changed files with 526 additions and 492 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/fix_lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ jobs:
with:
install-project: "false"

- name: Run ruff
- name: Run ruff check
continue-on-error: true
run: poetry run ruff check --fix .

- name: Run ruff format
continue-on-error: true
run: poetry run ruff format --quiet .

- run: cargo clippy --all-features --fix -- -D warnings
continue-on-error: true

Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,12 @@ jobs:
with:
install-project: "false"

- name: Check style
- name: Run ruff check
run: poetry run ruff check --output-format=github .

- name: Run ruff format
run: poetry run ruff format --check .

lint-mypy:
runs-on: ubuntu-latest
name: Typechecking
Expand Down
1 change: 1 addition & 0 deletions changelog.d/17643.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Replace `isort` and `black with `ruff`.
3 changes: 2 additions & 1 deletion contrib/cmdclient/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
#
#

""" Starts a synapse client console. """
"""Starts a synapse client console."""

import argparse
import binascii
import cmd
Expand Down
1 change: 1 addition & 0 deletions scripts-dev/check_pydantic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
until then, this script is a best effort to stop us from introducing type coersion bugs
(like the infamous stringy power levels fixed in room version 10).
"""

import argparse
import contextlib
import functools
Expand Down
3 changes: 3 additions & 0 deletions scripts-dev/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ set -x
# --quiet suppresses the update check.
ruff check --quiet --fix "${files[@]}"

# Reformat Python code.
ruff format --quiet "${files[@]}"

# Catch any common programming mistakes in Rust code.
#
# --bins, --examples, --lib, --tests combined explicitly disable checking
Expand Down
3 changes: 1 addition & 2 deletions scripts-dev/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
#
#

"""An interactive script for doing a release. See `cli()` below.
"""
"""An interactive script for doing a release. See `cli()` below."""

import glob
import json
Expand Down
4 changes: 2 additions & 2 deletions stubs/txredisapi.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Contains *incomplete* type hints for txredisapi.
"""
"""Contains *incomplete* type hints for txredisapi."""

from typing import Any, List, Optional, Type, Union

from twisted.internet import protocol
Expand Down
3 changes: 1 addition & 2 deletions synapse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
#
#

""" This is an implementation of a Matrix homeserver.
"""
"""This is an implementation of a Matrix homeserver."""

import os
import sys
Expand Down
4 changes: 2 additions & 2 deletions synapse/_scripts/generate_workers_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def elide_http_methods_if_unconflicting(
"""

def paths_to_methods_dict(
methods_and_paths: Iterable[Tuple[str, str]]
methods_and_paths: Iterable[Tuple[str, str]],
) -> Dict[str, Set[str]]:
"""
Given (method, path) pairs, produces a dict from path to set of methods
Expand Down Expand Up @@ -201,7 +201,7 @@ def paths_to_methods_dict(


def simplify_path_regexes(
registrations: Dict[Tuple[str, str], EndpointDescription]
registrations: Dict[Tuple[str, str], EndpointDescription],
) -> Dict[Tuple[str, str], EndpointDescription]:
"""
Simplify all the path regexes for the dict of endpoint descriptions,
Expand Down
7 changes: 6 additions & 1 deletion synapse/_scripts/review_recent_signups.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

class ReviewConfig(RootConfig):
"A config class that just pulls out the database config"

config_classes = [DatabaseConfig]


Expand Down Expand Up @@ -160,7 +161,11 @@ def main() -> None:

with make_conn(database_config, engine, "review_recent_signups") as db_conn:
# This generates a type of Cursor, not LoggingTransaction.
user_infos = get_recent_users(db_conn.cursor(), since_ms, exclude_users_with_appservice) # type: ignore[arg-type]
user_infos = get_recent_users(
db_conn.cursor(),
since_ms, # type: ignore[arg-type]
exclude_users_with_appservice,
)

for user_info in user_infos:
if exclude_users_with_email and user_info.emails:
Expand Down
26 changes: 12 additions & 14 deletions synapse/_scripts/synapse_port_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,9 +717,7 @@ async def run(self) -> None:
return

# Check if all background updates are done, abort if not.
updates_complete = (
await self.sqlite_store.db_pool.updates.has_completed_background_updates()
)
updates_complete = await self.sqlite_store.db_pool.updates.has_completed_background_updates()
if not updates_complete:
end_error = (
"Pending background updates exist in the SQLite3 database."
Expand Down Expand Up @@ -1095,10 +1093,10 @@ async def _get_total_count_to_port(
return done, remaining + done

async def _setup_state_group_id_seq(self) -> None:
curr_id: Optional[int] = (
await self.sqlite_store.db_pool.simple_select_one_onecol(
table="state_groups", keyvalues={}, retcol="MAX(id)", allow_none=True
)
curr_id: Optional[
int
] = await self.sqlite_store.db_pool.simple_select_one_onecol(
table="state_groups", keyvalues={}, retcol="MAX(id)", allow_none=True
)

if not curr_id:
Expand Down Expand Up @@ -1186,13 +1184,13 @@ def r(txn: LoggingTransaction) -> None:
)

async def _setup_auth_chain_sequence(self) -> None:
curr_chain_id: Optional[int] = (
await self.sqlite_store.db_pool.simple_select_one_onecol(
table="event_auth_chains",
keyvalues={},
retcol="MAX(chain_id)",
allow_none=True,
)
curr_chain_id: Optional[
int
] = await self.sqlite_store.db_pool.simple_select_one_onecol(
table="event_auth_chains",
keyvalues={},
retcol="MAX(chain_id)",
allow_none=True,
)

def r(txn: LoggingTransaction) -> None:
Expand Down
3 changes: 2 additions & 1 deletion synapse/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
#
#

"""Contains the URL paths to prefix various aspects of the server with. """
"""Contains the URL paths to prefix various aspects of the server with."""

import hmac
from hashlib import sha256
from urllib.parse import urlencode
Expand Down
1 change: 1 addition & 0 deletions synapse/appservice/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
This is all tied together by the AppServiceScheduler which DIs the required
components.
"""

import logging
from typing import (
TYPE_CHECKING,
Expand Down
11 changes: 5 additions & 6 deletions synapse/config/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,13 @@ def generate_config_section(
)
form_secret = 'form_secret: "%s"' % random_string_with_symbols(50)

return (
"""\
return """\
%(macaroon_secret_key)s
%(form_secret)s
signing_key_path: "%(base_key_name)s.signing.key"
trusted_key_servers:
- server_name: "matrix.org"
"""
% locals()
)
""" % locals()

def read_signing_keys(self, signing_key_path: str, name: str) -> List[SigningKey]:
"""Read the signing keys in the given path.
Expand Down Expand Up @@ -249,7 +246,9 @@ def read_old_signing_keys(
if is_signing_algorithm_supported(key_id):
key_base64 = key_data["key"]
key_bytes = decode_base64(key_base64)
verify_key: "VerifyKeyWithExpiry" = decode_verify_key_bytes(key_id, key_bytes) # type: ignore[assignment]
verify_key: "VerifyKeyWithExpiry" = decode_verify_key_bytes(
key_id, key_bytes
) # type: ignore[assignment]
verify_key.expired = key_data["expired_ts"]
keys[key_id] = verify_key
else:
Expand Down
7 changes: 2 additions & 5 deletions synapse/config/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,9 @@ def generate_config_section(
self, config_dir_path: str, server_name: str, **kwargs: Any
) -> str:
log_config = os.path.join(config_dir_path, server_name + ".log.config")
return (
"""\
return """\
log_config: "%(log_config)s"
"""
% locals()
)
""" % locals()

def read_arguments(self, args: argparse.Namespace) -> None:
if args.no_redirect_stdio is not None:
Expand Down
14 changes: 4 additions & 10 deletions synapse/config/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,13 +828,10 @@ def generate_config_section(
).lstrip()

if not unsecure_listeners:
unsecure_http_bindings = (
"""- port: %(unsecure_port)s
unsecure_http_bindings = """- port: %(unsecure_port)s
tls: false
type: http
x_forwarded: true"""
% locals()
)
x_forwarded: true""" % locals()

if not open_private_ports:
unsecure_http_bindings += (
Expand All @@ -853,16 +850,13 @@ def generate_config_section(
if not secure_listeners:
secure_http_bindings = ""

return (
"""\
return """\
server_name: "%(server_name)s"
pid_file: %(pid_file)s
listeners:
%(secure_http_bindings)s
%(unsecure_http_bindings)s
"""
% locals()
)
""" % locals()

def read_arguments(self, args: argparse.Namespace) -> None:
if args.manhole is not None:
Expand Down
9 changes: 5 additions & 4 deletions synapse/config/workers.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,11 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:
)

# type-ignore: the expression `Union[A, B]` is not a Type[Union[A, B]] currently
self.instance_map: Dict[
str, InstanceLocationConfig
] = parse_and_validate_mapping(
instance_map, InstanceLocationConfig # type: ignore[arg-type]
self.instance_map: Dict[str, InstanceLocationConfig] = (
parse_and_validate_mapping(
instance_map,
InstanceLocationConfig, # type: ignore[arg-type]
)
)

# Map from type of streams to source, c.f. WriterLocations.
Expand Down
3 changes: 2 additions & 1 deletion synapse/event_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,8 @@ def _check_power_levels(
raise SynapseError(400, f"{v!r} must be an integer.")
if k in {"events", "notifications", "users"}:
if not isinstance(v, collections.abc.Mapping) or not all(
type(v) is int for v in v.values() # noqa: E721
type(v) is int
for v in v.values() # noqa: E721
):
raise SynapseError(
400,
Expand Down
2 changes: 1 addition & 1 deletion synapse/events/presence_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def load_legacy_presence_router(hs: "HomeServer") -> None:
# All methods that the module provides should be async, but this wasn't enforced
# in the old module system, so we wrap them if needed
def async_wrapper(
f: Optional[Callable[P, R]]
f: Optional[Callable[P, R]],
) -> Optional[Callable[P, Awaitable[R]]]:
# f might be None if the callback isn't implemented by the module. In this
# case we don't want to register a callback at all so we return None.
Expand Down
6 changes: 3 additions & 3 deletions synapse/events/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ def _build_state_group_deltas(self) -> Dict[Tuple[int, int], StateMap]:


def _encode_state_group_delta(
state_group_delta: Dict[Tuple[int, int], StateMap[str]]
state_group_delta: Dict[Tuple[int, int], StateMap[str]],
) -> List[Tuple[int, int, Optional[List[Tuple[str, str, str]]]]]:
if not state_group_delta:
return []
Expand All @@ -517,7 +517,7 @@ def _encode_state_group_delta(


def _decode_state_group_delta(
input: List[Tuple[int, int, List[Tuple[str, str, str]]]]
input: List[Tuple[int, int, List[Tuple[str, str, str]]]],
) -> Dict[Tuple[int, int], StateMap[str]]:
if not input:
return {}
Expand All @@ -544,7 +544,7 @@ def _encode_state_dict(


def _decode_state_dict(
input: Optional[List[Tuple[str, str, str]]]
input: Optional[List[Tuple[str, str, str]]],
) -> Optional[StateMap[str]]:
"""Decodes a state dict encoded using `_encode_state_dict` above"""
if input is None:
Expand Down
3 changes: 1 addition & 2 deletions synapse/federation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@
#
#

""" This package includes all the federation specific logic.
"""
"""This package includes all the federation specific logic."""
2 changes: 1 addition & 1 deletion synapse/federation/persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#
#

""" This module contains all the persistence actions done by the federation
"""This module contains all the persistence actions done by the federation
package.
These actions are mostly only used by the :py:mod:`.replication` module.
Expand Down
1 change: 0 additions & 1 deletion synapse/federation/transport/server/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,6 @@ async def on_GET(
request: SynapseRequest,
media_id: str,
) -> None:

width = parse_integer(request, "width", required=True)
height = parse_integer(request, "height", required=True)
method = parse_string(request, "method", "scale")
Expand Down
2 changes: 1 addition & 1 deletion synapse/federation/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#
#

""" Defines the JSON structure of the protocol units used by the server to
"""Defines the JSON structure of the protocol units used by the server to
server protocol.
"""

Expand Down
8 changes: 4 additions & 4 deletions synapse/handlers/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ async def _get_local_account_status(self, user_id: UserID) -> JsonDict:
}

if self._use_account_validity_in_account_status:
status["org.matrix.expired"] = (
await self._account_validity_handler.is_user_expired(
user_id.to_string()
)
status[
"org.matrix.expired"
] = await self._account_validity_handler.is_user_expired(
user_id.to_string()
)

return status
Expand Down
Loading

0 comments on commit 7d52ce7

Please sign in to comment.