Skip to content

Commit

Permalink
Type some Lichess API methods as returning None (#933)
Browse files Browse the repository at this point in the history
* Add OKResponse type

* flake8

* flake8 more

* Remove comment about aligning types

* Switch to None

* Formatting

* Remove comments

* Remove last comment
  • Loading branch information
greg-finley authored Mar 21, 2024
1 parent 4889861 commit e974778
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 38 deletions.
40 changes: 19 additions & 21 deletions lib/lichess.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)."""
Expand All @@ -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)."""
Expand Down Expand Up @@ -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)."""
Expand Down
32 changes: 15 additions & 17 deletions test_bot/lichess.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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`."""
Expand All @@ -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."""
Expand All @@ -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."""
Expand All @@ -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."""
Expand Down

0 comments on commit e974778

Please sign in to comment.