|
22 | 22 | import itertools |
23 | 23 | import typing |
24 | 24 |
|
25 | | -from typing import ClassVar, Literal, Self, SupportsInt, TypeAlias |
| 25 | +from typing import ClassVar, Literal, Protocol, Self, SupportsInt, TypeAlias, TypeVar |
26 | 26 | from collections.abc import Callable, Hashable, Iterable, Iterator, Mapping |
27 | 27 |
|
28 | 28 |
|
|
34 | 34 | BLACK: Color = False |
35 | 35 | COLORS: list[Color] = [WHITE, BLACK] |
36 | 36 | ColorName = Literal["white", "black"] |
37 | | -COLOR_NAMES: list[ColorName] = ["black", "white"] |
| 37 | + |
| 38 | +_T = TypeVar("_T") |
| 39 | + |
| 40 | +class ByColor(Protocol[_T]): |
| 41 | + """A pair of values indexed by :data:`~chess.Color`.""" |
| 42 | + def __getitem__(self, color: Color, /) -> _T: ... |
| 43 | + def __setitem__(self, color: Color, value: _T, /) -> None: ... |
| 44 | + def __iter__(self) -> Iterator[_T]: ... |
| 45 | + |
| 46 | +COLOR_NAMES: ByColor[ColorName] = typing.cast(list[ColorName], ["black", "white"]) |
38 | 47 |
|
39 | 48 | PieceType: TypeAlias = int |
40 | 49 | PAWN: PieceType = 1 |
@@ -679,7 +688,7 @@ def _step_attacks(square: Square, deltas: Iterable[int]) -> Bitboard: |
679 | 688 |
|
680 | 689 | BB_KNIGHT_ATTACKS: list[Bitboard] = [_step_attacks(sq, [17, 15, 10, 6, -17, -15, -10, -6]) for sq in SQUARES] |
681 | 690 | BB_KING_ATTACKS: list[Bitboard] = [_step_attacks(sq, [9, 8, 7, 1, -9, -8, -7, -1]) for sq in SQUARES] |
682 | | -BB_PAWN_ATTACKS: list[list[Bitboard]] = [[_step_attacks(sq, deltas) for sq in SQUARES] for deltas in [[-7, -9], [7, 9]]] |
| 691 | +BB_PAWN_ATTACKS: ByColor[list[Bitboard]] = [[_step_attacks(sq, deltas) for sq in SQUARES] for deltas in [[-7, -9], [7, 9]]] |
683 | 692 |
|
684 | 693 |
|
685 | 694 | def _edges(square: Square) -> Bitboard: |
@@ -957,7 +966,7 @@ class BaseBoard: |
957 | 966 | queens: Bitboard |
958 | 967 | kings: Bitboard |
959 | 968 | promoted: Bitboard |
960 | | - occupied_co: list[Bitboard] |
| 969 | + occupied_co: ByColor[Bitboard] |
961 | 970 | occupied: Bitboard |
962 | 971 |
|
963 | 972 | def __init__(self, board_fen: str | None = STARTING_BOARD_FEN) -> None: |
|
0 commit comments