From e974778da2e049b2df1ff2f5268a75369df757ec Mon Sep 17 00:00:00 2001 From: Greg Finley Date: Thu, 21 Mar 2024 07:49:01 -0700 Subject: [PATCH] Type some Lichess API methods as returning None (#933) * Add OKResponse type * flake8 * flake8 more * Remove comment about aligning types * Switch to None * Formatting * Remove comments * Remove last comment --- lib/lichess.py | 40 +++++++++++++++++++--------------------- test_bot/lichess.py | 32 +++++++++++++++----------------- 2 files changed, 34 insertions(+), 38 deletions(-) diff --git a/lib/lichess.py b/lib/lichess.py index ff2dde7b0..34c1c1686 100644 --- a/lib/lichess.py +++ b/lib/lichess.py @@ -253,21 +253,21 @@ def rate_limit_time_left(self, path_template: str) -> datetime.timedelta: """How much time is left until we can use the path template normally.""" return self.rate_limit_timers[path_template].time_until_expiration() - def upgrade_to_bot_account(self) -> JSON_REPLY_TYPE: + def upgrade_to_bot_account(self) -> None: """Upgrade the account to a BOT account.""" - return self.api_post("upgrade") + self.api_post("upgrade") - def make_move(self, game_id: str, move: chess.engine.PlayResult) -> JSON_REPLY_TYPE: + def make_move(self, game_id: str, move: chess.engine.PlayResult) -> None: """ Make a move. :param game_id: The id of the game. :param move: The move to make. """ - return self.api_post("move", game_id, move.move, - params={"offeringDraw": str(move.draw_offered).lower()}) + self.api_post("move", game_id, move.move, + params={"offeringDraw": str(move.draw_offered).lower()}) - def chat(self, game_id: str, room: str, text: str) -> JSON_REPLY_TYPE: + def chat(self, game_id: str, room: str, text: str) -> None: """ Send a message to the chat. @@ -279,14 +279,13 @@ def chat(self, game_id: str, room: str, text: str) -> JSON_REPLY_TYPE: logger.warning(f"This chat message is {len(text)} characters, which is longer " f"than the maximum of {MAX_CHAT_MESSAGE_LEN}. It will not be sent.") logger.warning(f"Message: {text}") - return {} payload = {"room": room, "text": text} - return self.api_post("chat", game_id, data=payload) + self.api_post("chat", game_id, data=payload) - def abort(self, game_id: str) -> JSON_REPLY_TYPE: + def abort(self, game_id: str) -> None: """Aborts a game.""" - return self.api_post("abort", game_id) + self.api_post("abort", game_id) def get_event_stream(self) -> requests.models.Response: """Get a stream of the events (e.g. challenge, gameStart).""" @@ -296,20 +295,19 @@ def get_game_stream(self, game_id: str) -> requests.models.Response: """Get stream of the in-game events (e.g. moves by the opponent).""" return self.api_get("stream", game_id, stream=True, timeout=15) - def accept_challenge(self, challenge_id: str) -> JSON_REPLY_TYPE: + def accept_challenge(self, challenge_id: str) -> None: """Accept a challenge.""" - return self.api_post("accept", challenge_id) + self.api_post("accept", challenge_id) - def decline_challenge(self, challenge_id: str, reason: str = "generic") -> JSON_REPLY_TYPE: + def decline_challenge(self, challenge_id: str, reason: str = "generic") -> None: """Decline a challenge.""" try: - return self.api_post("decline", challenge_id, - data=f"reason={reason}", - headers={"Content-Type": - "application/x-www-form-urlencoded"}, - raise_for_status=False) + self.api_post("decline", challenge_id, + data=f"reason={reason}", + headers={"Content-Type": "application/x-www-form-urlencoded"}, + raise_for_status=False) except Exception: - return {} + pass def get_profile(self) -> JSON_REPLY_TYPE: """Get the bot's profile (e.g. username).""" @@ -355,9 +353,9 @@ def challenge(self, username: str, payload: REQUESTS_PAYLOAD_TYPE) -> JSON_REPLY """Create a challenge.""" return self.api_post("challenge", username, payload=payload, raise_for_status=False) - def cancel(self, challenge_id: str) -> JSON_REPLY_TYPE: + def cancel(self, challenge_id: str) -> None: """Cancel a challenge.""" - return self.api_post("cancel", challenge_id, raise_for_status=False) + self.api_post("cancel", challenge_id, raise_for_status=False) def online_book_get(self, path: str, params: Optional[dict[str, Any]] = None, stream: bool = False) -> JSON_REPLY_TYPE: """Get an external move from online sources (chessdb or lichess.org).""" diff --git a/test_bot/lichess.py b/test_bot/lichess.py index 204148187..f3cdddc09 100644 --- a/test_bot/lichess.py +++ b/test_bot/lichess.py @@ -9,8 +9,7 @@ from queue import Queue from typing import Union, Any, Optional, Generator from lib.timer import to_msec -JSON_REPLY_TYPE = dict[str, Any] -REQUESTS_PAYLOAD_TYPE = dict[str, Any] +from lib.lichess import JSON_REPLY_TYPE, REQUESTS_PAYLOAD_TYPE logger = logging.getLogger(__name__) @@ -147,22 +146,21 @@ def __init__(self, self.sent_game = False self.started_game_stream = False - def upgrade_to_bot_account(self) -> JSON_REPLY_TYPE: + def upgrade_to_bot_account(self) -> None: """Isn't used in tests.""" - return {} + pass - def make_move(self, game_id: str, move: chess.engine.PlayResult) -> JSON_REPLY_TYPE: + def make_move(self, game_id: str, move: chess.engine.PlayResult) -> None: """Send a move to the opponent engine thread.""" self.move_queue.put(move.move) - return {} - def chat(self, game_id: str, room: str, text: str) -> JSON_REPLY_TYPE: + def chat(self, game_id: str, room: str, text: str) -> None: """Isn't used in tests.""" - return {} + pass - def abort(self, game_id: str) -> JSON_REPLY_TYPE: + def abort(self, game_id: str) -> None: """Isn't used in tests.""" - return {} + pass def get_event_stream(self) -> EventStream: """Send the `EventStream`.""" @@ -177,13 +175,13 @@ def get_game_stream(self, game_id: str) -> GameStream: self.started_game_stream = True return GameStream(self.board_queue, self.clock_queue) - def accept_challenge(self, challenge_id: str) -> JSON_REPLY_TYPE: + def accept_challenge(self, challenge_id: str) -> None: """Isn't used in tests.""" - return {} + pass - def decline_challenge(self, challenge_id: str, reason: str = "generic") -> JSON_REPLY_TYPE: + def decline_challenge(self, challenge_id: str, reason: str = "generic") -> None: """Isn't used in tests.""" - return {} + pass def get_profile(self) -> dict[str, Union[str, bool, dict[str, str]]]: """Return a simple profile for the bot that lichess-bot uses when testing.""" @@ -204,7 +202,7 @@ def get_ongoing_games(self) -> list[dict[str, Any]]: def resign(self, game_id: str) -> None: """Isn't used in tests.""" - return + pass def get_game_pgn(self, game_id: str) -> str: """Return a simple PGN.""" @@ -228,9 +226,9 @@ def challenge(self, username: str, payload: REQUESTS_PAYLOAD_TYPE) -> JSON_REPLY """Isn't used in tests.""" return {} - def cancel(self, challenge_id: str) -> JSON_REPLY_TYPE: + def cancel(self, challenge_id: str) -> None: """Isn't used in tests.""" - return {} + pass def online_book_get(self, path: str, params: Optional[dict[str, Any]] = None, stream: bool = False) -> JSON_REPLY_TYPE: """Isn't used in tests."""