Skip to content

Commit 6e09f0f

Browse files
committed
Add type hints for all examples
1 parent ccb0632 commit 6e09f0f

6 files changed

Lines changed: 31 additions & 15 deletions

File tree

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ script:
8686
- if [[ $LATEST -eq 1 ]]; then python -m mypy --strict chess/syzygy.py; fi
8787
- if [[ $LATEST -eq 1 ]]; then python -m mypy --strict chess/variant.py; fi
8888
- # Typing of examples
89+
- if [[ $LATEST -eq 1 ]]; then python -m mypy --strict examples/bratko_kopec/bratko_kopec.py || true; fi
90+
- if [[ $LATEST -eq 1 ]]; then python -m mypy --strict examples/chess960_pos_list.py; fi
91+
- if [[ $LATEST -eq 1 ]]; then python -m mypy examples/perft/perft.py; fi
92+
- if [[ $LATEST -eq 1 ]]; then python -m mypy examples/polyglot_tree.py; fi
93+
- if [[ $LATEST -eq 1 ]]; then python -m mypy --strict examples/push_san.py; fi
8994
- if [[ $LATEST -eq 1 ]]; then python -m mypy --strict examples/xray_attacks.py; fi
9095
- # Perft tests
9196
- if [[ $LATEST -eq 1 ]]; then python examples/perft/perft.py -t 1 examples/perft/random.perft --max-nodes 10000; fi

examples/bratko_kopec/bratko_kopec.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
import logging
1010
import sys
1111

12+
from typing import Type
13+
1214
import chess
1315
import chess.engine
1416
import chess.variant
1517

1618

17-
async def test_epd(engine, epd, VariantBoard, movetime):
19+
async def test_epd(engine: chess.engine.EngineProtocol, epd: str, VariantBoard: Type[chess.Board], movetime: float) -> float:
1820
board, epd_info = VariantBoard.from_epd(epd)
1921
epd_string = epd_info.get("id", board.fen())
2022
if "am" in epd_info:
@@ -25,7 +27,10 @@ async def test_epd(engine, epd, VariantBoard, movetime):
2527
limit = chess.engine.Limit(time=movetime)
2628
result = await engine.play(board, limit, game=object())
2729

28-
if "am" in epd_info and result.move in epd_info["am"]:
30+
if not result.move:
31+
print(f"{epd_string}: -- | +0")
32+
return 0.0
33+
elif "am" in epd_info and result.move in epd_info["am"]:
2934
print(f"{epd_string}: {board.san(result.move)} | +0")
3035
return 0.0
3136
elif "bm" in epd_info and result.move not in epd_info["bm"]:
@@ -36,7 +41,7 @@ async def test_epd(engine, epd, VariantBoard, movetime):
3641
return 1.0
3742

3843

39-
async def test_epd_with_fractional_scores(engine, epd, VariantBoard, movetime):
44+
async def test_epd_with_fractional_scores(engine: chess.engine.EngineProtocol, epd: str, VariantBoard: Type[chess.Board], movetime: float) -> float:
4045
board, epd_info = VariantBoard.from_epd(epd)
4146
epd_string = epd_info.get("id", board.fen())
4247
if "am" in epd_info:
@@ -71,7 +76,7 @@ async def test_epd_with_fractional_scores(engine, epd, VariantBoard, movetime):
7176
return score
7277

7378

74-
async def main():
79+
async def main() -> None:
7580
# Parse command line arguments.
7681
parser = argparse.ArgumentParser(description=__doc__)
7782

examples/chess960_pos_list.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import chess
88

99

10-
def main(bench_only=False):
10+
def main(bench_only: bool = False) -> None:
1111
board = chess.Board.empty(chess960=True)
1212

1313
for sharnagl in range(0, 960):

examples/perft/perft.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@
44
Run perft test to check correctness and speed of the legal move generator.
55
"""
66

7-
import chess
8-
import chess.variant
97
import multiprocessing
108
import functools
119
import time
1210
import argparse
1311
import sys
1412

13+
from typing import Callable, Iterator, Optional, TextIO, Type
14+
15+
import chess
16+
import chess.variant
17+
1518

16-
def perft(depth, board):
19+
def perft(depth: int, board: chess.Board) -> int:
1720
if depth == 1:
1821
return board.legal_moves.count()
1922
elif depth > 1:
@@ -29,11 +32,11 @@ def perft(depth, board):
2932
return 1
3033

3134

32-
def parallel_perft(pool, depth, board):
35+
def parallel_perft(pool, depth: int, board: chess.Board) -> int:
3336
if depth == 1:
3437
return board.legal_moves.count()
3538
elif depth > 1:
36-
def successors(board):
39+
def successors(board: chess.Board) -> Iterator[chess.Board]:
3740
for move in board.legal_moves:
3841
board_after = board.copy(stack=False)
3942
board_after.push(move)
@@ -44,14 +47,14 @@ def successors(board):
4447
return 1
4548

4649

47-
def sdiv(a, b):
50+
def sdiv(a: float, b: float) -> float:
4851
try:
4952
return a / b
5053
except ZeroDivisionError:
5154
return float("Inf")
5255

5356

54-
def main(perft_file, VariantBoard, perft_f, max_depth, max_nodes):
57+
def main(perft_file: TextIO, VariantBoard: Type[chess.Board], perft_f: Callable[[int, chess.Board], int], max_depth: Optional[int], max_nodes: Optional[int]) -> None:
5558
current_id = None
5659
board = VariantBoard(chess960=True)
5760
column = 0

examples/polyglot_tree.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
"""Print a Polyglot opening book in tree form."""
44

5+
import argparse
6+
7+
from typing import Set
8+
59
import chess
610
import chess.polyglot
7-
import argparse
811

912

10-
def print_tree(args, visited, level=0):
13+
def print_tree(args: argparse.Namespace, visited: Set[int], level: int = 0) -> None:
1114
if level >= args.depth:
1215
return
1316

examples/push_san.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import timeit
77

88

9-
def play_immortal_game():
9+
def play_immortal_game() -> None:
1010
board = chess.Board()
1111

1212
# 1. e4 e5

0 commit comments

Comments
 (0)