From a49add3d021e0745ddc7c888faeb67400259ccab Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Fri, 27 Oct 2023 19:06:34 -0700 Subject: [PATCH] lint: Replace Black with Ruff. Signed-off-by: Anders Kaseorg --- pyproject.toml | 1 + requirements.txt | 1 - tools/deploy | 4 +--- tools/lint | 15 ++++++--------- tools/provision | 6 ++++-- .../integrations/jabber/jabber_mirror_backend.py | 12 +++--------- .../integrations/perforce/zulip_change-commit.py | 4 +--- zulip/tests/test_default_arguments.py | 5 +++-- .../zulip_bots/bots/beeminder/test_beeminder.py | 4 +--- zulip_bots/zulip_bots/bots/chessbot/chessbot.py | 3 +-- .../zulip_bots/bots/idonethis/test_idonethis.py | 4 +--- .../zulip_bots/bots/merels/libraries/mechanics.py | 4 +--- zulip_bots/zulip_bots/game_handler.py | 8 ++------ zulip_bots/zulip_bots/lib.py | 12 +++--------- zulip_bots/zulip_bots/simple_lib.py | 12 +++--------- 15 files changed, 31 insertions(+), 64 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 1d3fa2132..ea90f90dd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -110,4 +110,5 @@ src = [ "zulip_bots", "zulip_botserver", ] +target-version = "py38" line-length = 100 diff --git a/requirements.txt b/requirements.txt index a11f2c04c..78970ab39 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,5 @@ crayons twine -black~=23.10.1 mock pytest pytest-cov diff --git a/tools/deploy b/tools/deploy index 2b5f1c072..1a63ebb4a 100755 --- a/tools/deploy +++ b/tools/deploy @@ -59,9 +59,7 @@ def pack(options: argparse.Namespace) -> None: [deploy] bot={} zuliprc=zuliprc - """.format( - options.main - ) + """.format(options.main) ) zip_file.writestr("config.ini", bot_config) zip_file.close() diff --git a/tools/lint b/tools/lint index 9eb8dfd66..7b383de5d 100755 --- a/tools/lint +++ b/tools/lint @@ -1,7 +1,6 @@ #! /usr/bin/env python3 import argparse -import re import sys from zulint.command import LinterConfig, add_default_linter_arguments @@ -36,16 +35,14 @@ def run() -> None: "ruff", ["ruff", "check", "--quiet"], ["py"], fix_arg="--fix", description="Python linter" ) linter_config.external_linter( - "gitlint", ["tools/lint-commits"], description="Git Lint for commit messages" + "ruff-format", + ["ruff", "format", "--quiet"], + ["py"], + check_arg="--check", + description="Python formatter", ) linter_config.external_linter( - "black", - ["black"], - ["py"], - description="Reformats Python code", - check_arg=["--check"], - suppress_line=lambda line: line == "All done! ✨ 🍰 ✨\n" - or re.fullmatch(r"\d+ files? would be left unchanged\.\n", line) is not None, + "gitlint", ["tools/lint-commits"], description="Git Lint for commit messages" ) @linter_config.lint diff --git a/tools/provision b/tools/provision index 6ec9015b3..77de61790 100755 --- a/tools/provision +++ b/tools/provision @@ -45,8 +45,10 @@ the Python version this command is executed with.""" if py_version <= (3, 1) and (not options.force): print( - red + "Provision failed: Cannot create venv with outdated Python version ({}).\n" - "Maybe try `python3 tools/provision`.".format(py_version_output.strip()) + end_format + red + + "Provision failed: Cannot create venv with outdated Python version ({}).\n" + "Maybe try `python3 tools/provision`.".format(py_version_output.strip()) + + end_format ) sys.exit(1) diff --git a/zulip/integrations/jabber/jabber_mirror_backend.py b/zulip/integrations/jabber/jabber_mirror_backend.py index 05758ba40..e080eb938 100755 --- a/zulip/integrations/jabber/jabber_mirror_backend.py +++ b/zulip/integrations/jabber/jabber_mirror_backend.py @@ -301,9 +301,7 @@ def config_error(msg: str) -> None: zulip configuration file under the jabber_mirror section (exceptions are noted in their help sections). Keys have the same name as options with hyphens replaced with underscores. Zulip configuration options go in the api section, -as normal.""".replace( - "\n", " " - ) +as normal.""".replace("\n", " ") ) parser.add_option( "--mode", @@ -314,9 +312,7 @@ def config_error(msg: str) -> None: all messages they send on Zulip to Jabber and all private Jabber messages to Zulip. In "public" mode, the mirror uses the credentials for a dedicated mirror user and mirrors messages sent to Jabber rooms to Zulip. Defaults to -"personal"'''.replace( - "\n", " " - ), +"personal"'''.replace("\n", " "), ) parser.add_option( "--zulip-email-suffix", @@ -327,9 +323,7 @@ def config_error(msg: str) -> None: suffix before sending requests to the Jabber server. For example, specifying "+foo" will cause messages that are sent to the "bar" room by nickname "qux" to be mirrored to the "bar/xmpp" stream in Zulip by user "qux+foo@example.com". This -option does not affect login credentials.""".replace( - "\n", " " - ), +option does not affect login credentials.""".replace("\n", " "), ) parser.add_option( "-d", diff --git a/zulip/integrations/perforce/zulip_change-commit.py b/zulip/integrations/perforce/zulip_change-commit.py index ad434f73e..0ca830798 100755 --- a/zulip/integrations/perforce/zulip_change-commit.py +++ b/zulip/integrations/perforce/zulip_change-commit.py @@ -84,9 +84,7 @@ ```quote {desc} ``` -""".format( - user=metadata["user"], change=change, path=changeroot, desc=metadata["desc"] -) +""".format(user=metadata["user"], change=change, path=changeroot, desc=metadata["desc"]) message_data: Dict[str, Any] = { "type": "stream", diff --git a/zulip/tests/test_default_arguments.py b/zulip/tests/test_default_arguments.py index 03a7d0bcf..bc076af7d 100755 --- a/zulip/tests/test_default_arguments.py +++ b/zulip/tests/test_default_arguments.py @@ -41,8 +41,9 @@ def test_config_path_with_tilde(self, mock_os_path_exists: bool) -> None: expanded_test_path = os.path.abspath(os.path.expanduser(test_path)) self.assertEqual( str(cm.exception), - "api_key or email not specified and " - "file {} does not exist".format(expanded_test_path), + "api_key or email not specified and " "file {} does not exist".format( + expanded_test_path + ), ) diff --git a/zulip_bots/zulip_bots/bots/beeminder/test_beeminder.py b/zulip_bots/zulip_bots/bots/beeminder/test_beeminder.py index da3761c4c..f8af54a1a 100644 --- a/zulip_bots/zulip_bots/bots/beeminder/test_beeminder.py +++ b/zulip_bots/zulip_bots/bots/beeminder/test_beeminder.py @@ -79,9 +79,7 @@ def test_invalid_when_handle_message(self) -> None: {"auth_token": "someInvalidKey", "username": "aaron", "goalname": "goal"} ), patch("requests.get", side_effect=ConnectionError()), self.mock_http_conversation( "test_invalid_when_handle_message" - ), patch( - "logging.exception" - ): + ), patch("logging.exception"): self.verify_reply("5", "Error. Check your key!") def test_error(self) -> None: diff --git a/zulip_bots/zulip_bots/bots/chessbot/chessbot.py b/zulip_bots/zulip_bots/bots/chessbot/chessbot.py index d1409e83f..fb5a0e5e9 100644 --- a/zulip_bots/zulip_bots/bots/chessbot/chessbot.py +++ b/zulip_bots/zulip_bots/bots/chessbot/chessbot.py @@ -54,8 +54,7 @@ def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> No if bot_handler.storage.contains("is_with_computer"): is_with_computer = ( # `bot_handler`'s `storage` only accepts `str` values. - bot_handler.storage.get("is_with_computer") - == str(True) + bot_handler.storage.get("is_with_computer") == str(True) ) if bot_handler.storage.contains("last_fen"): diff --git a/zulip_bots/zulip_bots/bots/idonethis/test_idonethis.py b/zulip_bots/zulip_bots/bots/idonethis/test_idonethis.py index d714ab885..7a16b9ffd 100644 --- a/zulip_bots/zulip_bots/bots/idonethis/test_idonethis.py +++ b/zulip_bots/zulip_bots/bots/idonethis/test_idonethis.py @@ -46,9 +46,7 @@ def test_bad_key(self) -> None: {"api_key": "87654321", "default_team": "testing team 1"} ), self.mock_http_conversation("test_401"), patch( "zulip_bots.bots.idonethis.idonethis.api_noop" - ), patch( - "logging.error" - ): + ), patch("logging.error"): self.verify_reply( "list teams", "I can't currently authenticate with idonethis. Can you check that your API key is correct? " diff --git a/zulip_bots/zulip_bots/bots/merels/libraries/mechanics.py b/zulip_bots/zulip_bots/bots/merels/libraries/mechanics.py index ecd2d7564..a95e1aaa2 100644 --- a/zulip_bots/zulip_bots/bots/merels/libraries/mechanics.py +++ b/zulip_bots/zulip_bots/bots/merels/libraries/mechanics.py @@ -311,9 +311,7 @@ def display_game(topic_name, merels_storage): response += interface.graph_grid(data.grid()) + "\n" response += """Phase {}. Take mode: {}. X taken: {}, O taken: {}. - """.format( - data.get_phase(), take, data.x_taken, data.o_taken - ) + """.format(data.get_phase(), take, data.x_taken, data.o_taken) return response diff --git a/zulip_bots/zulip_bots/game_handler.py b/zulip_bots/zulip_bots/game_handler.py index a3cacf5a6..39516288f 100644 --- a/zulip_bots/zulip_bots/game_handler.py +++ b/zulip_bots/zulip_bots/game_handler.py @@ -116,9 +116,7 @@ def help_message_single_player(self) -> str: `quit` * To see rules of this game, type `rules` -{}""".format( - self.game_name, self.get_bot_username(), self.move_help_message - ) +{}""".format(self.game_name, self.get_bot_username(), self.move_help_message) def get_commands(self) -> Dict[str, str]: action = self.help_message_single_player() @@ -644,9 +642,7 @@ def parse_message(self, message: Dict[str, Any]) -> None: message, "Your current game is not in this subject. \n\ To move subjects, send your message again, otherwise join the game using the link below.\n\n\ -{}".format( - self.get_formatted_game_object(game_id) - ), +{}".format(self.get_formatted_game_object(game_id)), ) self.pending_subject_changes.append(game_id) return diff --git a/zulip_bots/zulip_bots/lib.py b/zulip_bots/zulip_bots/lib.py index 412477845..f651716a9 100644 --- a/zulip_bots/zulip_bots/lib.py +++ b/zulip_bots/zulip_bots/lib.py @@ -227,9 +227,7 @@ def __init__( Have you not started the server? Or did you mis-specify the URL? - """.format( - e - ) + """.format(e) ) sys.exit(1) @@ -238,9 +236,7 @@ def __init__( print( """ ERROR: {} - """.format( - msg - ) + """.format(msg) ) sys.exit(1) @@ -338,9 +334,7 @@ def get_config_info(self, bot_name: str, optional: bool = False) -> Dict[str, st The suggested name is {}.conf We will proceed anyway. - """.format( - self.bot_config_file, bot_name - ) + """.format(self.bot_config_file, bot_name) ) # We expect the caller to pass in None if the user does diff --git a/zulip_bots/zulip_bots/simple_lib.py b/zulip_bots/zulip_bots/simple_lib.py index ed82f2b83..9b5edb31b 100644 --- a/zulip_bots/zulip_bots/simple_lib.py +++ b/zulip_bots/zulip_bots/simple_lib.py @@ -74,18 +74,14 @@ def send_message(self, message: Dict[str, Any]) -> Dict[str, Any]: """ stream: {} topic: {} {} - """.format( - message["to"], message["subject"], message["content"] - ) + """.format(message["to"], message["subject"], message["content"]) ) else: print( """ PM response: {} - """.format( - message["content"] - ) + """.format(message["content"]) ) # Note that message_server is only responsible for storing and assigning an # id to the message instead of actually displaying it. @@ -113,9 +109,7 @@ def update_message(self, message: Dict[str, Any]) -> None: """ update to message #{}: {} - """.format( - message["message_id"], message["content"] - ) + """.format(message["message_id"], message["content"]) ) def upload_file_from_path(self, file_path: str) -> Dict[str, Any]: