-
Notifications
You must be signed in to change notification settings - Fork 467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add more type hints #952
Add more type hints #952
Conversation
There are 8 errors remaining.
After I clean the mess in EDIT: Nevermind, using |
The only uses of EDIT: I type hinted some part of homemade engines, but mypy isn't able to check those types. I believe they are correct. Completing |
lib/engine_wrapper.py
Outdated
time_left = msec(game.state[f"{wb}time"]) | ||
time_left = msec(game.state["wtime"]) if board.turn == chess.WHITE else msec(game.state["btime"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this change made (and similar changes throughout like line 984)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because game.state
only supports wtime
and btime
and not all {text}time
options. We know that wb
has a value of "w"
or "b"
, so f"{wb}time"
will be either wtime
or btime
, but mypy doesn't know this. I could attempt to fix this using literals, like this:
# OPTION A
wb: Literal["w", "b"] = "w" if if board.turn == chess.WHITE else "b"
time_left = msec(game.state[f"{wb}time"])
# OPTION B
wbtime: Literal["wtime", "btime"] = "wtime" if if board.turn == chess.WHITE else "btime"
time_left = msec(game.state[wbtime])
I don't know if these options work, but I can try later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the type is Literal
then the assigned value has to be a literal. This works:
is_white = board.turn == chess.WHITE
wbtime: Literal["wtime", "btime"] = "wtime" if is_white else "btime"
time_left = msec(game.state[wbtime])
Since we need wtime
and btime
everywhere, I think a function would work better and keep the code simple with many fewer Literal
declarations:
def wbtime(board: chess.Board) -> Literal["wtime", "btime"]:
return "wtime" if board.turn == chess.WHITE else "btime"
Similar functions for winc
/binc
and others would be useful, as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the function for wbinc
in engine_wrapper
even though it's not used there, just so it is next to wbtime
.
Merge if there are no more changes. |
a74a74d
into
lichess-bot-devs:master
Type of pull request:
Description:
Adds more type hints using
TypedDict
.Related Issues:
closes #921
Checklist:
Screenshots/logs (if applicable):