25
25
from typing import ClassVar , Callable , Counter , Dict , Generic , Hashable , Iterable , Iterator , List , Literal , Mapping , Optional , SupportsInt , Tuple , Type , TypeVar , Union
26
26
27
27
if typing .TYPE_CHECKING :
28
- from typing_extensions import TypeAlias
28
+ from typing_extensions import Self , TypeAlias
29
29
30
30
31
31
EnPassantSpec = Literal ["legal" , "fen" , "xfen" ]
@@ -1455,7 +1455,7 @@ def apply_transform(self, f: Callable[[Bitboard], Bitboard]) -> None:
1455
1455
self .occupied = f (self .occupied )
1456
1456
self .promoted = f (self .promoted )
1457
1457
1458
- def transform (self : BaseBoardT , f : Callable [[Bitboard ], Bitboard ]) -> BaseBoardT :
1458
+ def transform (self , f : Callable [[Bitboard ], Bitboard ]) -> Self :
1459
1459
"""
1460
1460
Returns a transformed copy of the board (without move stack)
1461
1461
by applying a bitboard transformation function.
@@ -1473,11 +1473,11 @@ def transform(self: BaseBoardT, f: Callable[[Bitboard], Bitboard]) -> BaseBoardT
1473
1473
board .apply_transform (f )
1474
1474
return board
1475
1475
1476
- def apply_mirror (self : BaseBoardT ) -> None :
1476
+ def apply_mirror (self ) -> None :
1477
1477
self .apply_transform (flip_vertical )
1478
1478
self .occupied_co [WHITE ], self .occupied_co [BLACK ] = self .occupied_co [BLACK ], self .occupied_co [WHITE ]
1479
1479
1480
- def mirror (self : BaseBoardT ) -> BaseBoardT :
1480
+ def mirror (self ) -> Self :
1481
1481
"""
1482
1482
Returns a mirrored copy of the board (without move stack).
1483
1483
@@ -1491,7 +1491,7 @@ def mirror(self: BaseBoardT) -> BaseBoardT:
1491
1491
board .apply_mirror ()
1492
1492
return board
1493
1493
1494
- def copy (self : BaseBoardT ) -> BaseBoardT :
1494
+ def copy (self ) -> Self :
1495
1495
"""Creates a copy of the board."""
1496
1496
board = type (self )(None )
1497
1497
@@ -1509,10 +1509,10 @@ def copy(self: BaseBoardT) -> BaseBoardT:
1509
1509
1510
1510
return board
1511
1511
1512
- def __copy__ (self : BaseBoardT ) -> BaseBoardT :
1512
+ def __copy__ (self ) -> Self :
1513
1513
return self .copy ()
1514
1514
1515
- def __deepcopy__ (self : BaseBoardT , memo : Dict [int , object ]) -> BaseBoardT :
1515
+ def __deepcopy__ (self , memo : Dict [int , object ]) -> Self :
1516
1516
board = self .copy ()
1517
1517
memo [id (self )] = board
1518
1518
return board
@@ -1542,9 +1542,9 @@ def from_chess960_pos(cls: Type[BaseBoardT], scharnagl: int) -> BaseBoardT:
1542
1542
1543
1543
BoardT = TypeVar ("BoardT" , bound = "Board" )
1544
1544
1545
- class _BoardState ( Generic [ BoardT ]) :
1545
+ class _BoardState :
1546
1546
1547
- def __init__ (self , board : BoardT ) -> None :
1547
+ def __init__ (self , board : Board ) -> None :
1548
1548
self .pawns = board .pawns
1549
1549
self .knights = board .knights
1550
1550
self .bishops = board .bishops
@@ -1564,7 +1564,7 @@ def __init__(self, board: BoardT) -> None:
1564
1564
self .halfmove_clock = board .halfmove_clock
1565
1565
self .fullmove_number = board .fullmove_number
1566
1566
1567
- def restore (self , board : BoardT ) -> None :
1567
+ def restore (self , board : Board ) -> None :
1568
1568
board .pawns = self .pawns
1569
1569
board .knights = self .knights
1570
1570
board .bishops = self .bishops
@@ -1694,14 +1694,14 @@ class Board(BaseBoard):
1694
1694
manipulation.
1695
1695
"""
1696
1696
1697
- def __init__ (self : BoardT , fen : Optional [str ] = STARTING_FEN , * , chess960 : bool = False ) -> None :
1697
+ def __init__ (self , fen : Optional [str ] = STARTING_FEN , * , chess960 : bool = False ) -> None :
1698
1698
BaseBoard .__init__ (self , None )
1699
1699
1700
1700
self .chess960 = chess960
1701
1701
1702
1702
self .ep_square = None
1703
1703
self .move_stack = []
1704
- self ._stack : List [_BoardState [ BoardT ] ] = []
1704
+ self ._stack : List [_BoardState ] = []
1705
1705
1706
1706
if fen is None :
1707
1707
self .clear ()
@@ -1786,7 +1786,7 @@ def clear_stack(self) -> None:
1786
1786
self .move_stack .clear ()
1787
1787
self ._stack .clear ()
1788
1788
1789
- def root (self : BoardT ) -> BoardT :
1789
+ def root (self ) -> Self :
1790
1790
"""Returns a copy of the root position."""
1791
1791
if self ._stack :
1792
1792
board = type (self )(None , chess960 = self .chess960 )
@@ -2304,13 +2304,10 @@ def is_repetition(self, count: int = 3) -> bool:
2304
2304
2305
2305
return False
2306
2306
2307
- def _board_state (self : BoardT ) -> _BoardState [BoardT ]:
2308
- return _BoardState (self )
2309
-
2310
2307
def _push_capture (self , move : Move , capture_square : Square , piece_type : PieceType , was_promoted : bool ) -> None :
2311
2308
pass
2312
2309
2313
- def push (self : BoardT , move : Move ) -> None :
2310
+ def push (self , move : Move ) -> None :
2314
2311
"""
2315
2312
Updates the position with the given *move* and puts it onto the
2316
2313
move stack.
@@ -2335,7 +2332,7 @@ def push(self: BoardT, move: Move) -> None:
2335
2332
"""
2336
2333
# Push move and remember board state.
2337
2334
move = self ._to_chess960 (move )
2338
- board_state = self . _board_state ( )
2335
+ board_state = _BoardState ( self )
2339
2336
self .castling_rights = self .clean_castling_rights () # Before pushing stack
2340
2337
self .move_stack .append (self ._from_chess960 (self .chess960 , move .from_square , move .to_square , move .promotion , move .drop ))
2341
2338
self ._stack .append (board_state )
@@ -2431,7 +2428,7 @@ def push(self: BoardT, move: Move) -> None:
2431
2428
# Swap turn.
2432
2429
self .turn = not self .turn
2433
2430
2434
- def pop (self : BoardT ) -> Move :
2431
+ def pop (self ) -> Move :
2435
2432
"""
2436
2433
Restores the previous position and returns the last move from the stack.
2437
2434
@@ -2841,7 +2838,7 @@ def _validate_epd_opcode(self, opcode: str) -> None:
2841
2838
if blacklisted in opcode :
2842
2839
raise ValueError (f"invalid character { blacklisted !r} in epd opcode: { opcode !r} " )
2843
2840
2844
- def _parse_epd_ops (self : BoardT , operation_part : str , make_board : Callable [[], BoardT ]) -> Dict [str , Union [None , str , int , float , Move , List [Move ]]]:
2841
+ def _parse_epd_ops (self , operation_part : str , make_board : Callable [[], Self ]) -> Dict [str , Union [None , str , int , float , Move , List [Move ]]]:
2845
2842
operations : Dict [str , Union [None , str , int , float , Move , List [Move ]]] = {}
2846
2843
state = "opcode"
2847
2844
opcode = ""
@@ -3834,16 +3831,16 @@ def apply_transform(self, f: Callable[[Bitboard], Bitboard]) -> None:
3834
3831
self .ep_square = None if self .ep_square is None else msb (f (BB_SQUARES [self .ep_square ]))
3835
3832
self .castling_rights = f (self .castling_rights )
3836
3833
3837
- def transform (self : BoardT , f : Callable [[Bitboard ], Bitboard ]) -> BoardT :
3834
+ def transform (self , f : Callable [[Bitboard ], Bitboard ]) -> Self :
3838
3835
board = self .copy (stack = False )
3839
3836
board .apply_transform (f )
3840
3837
return board
3841
3838
3842
- def apply_mirror (self : BoardT ) -> None :
3839
+ def apply_mirror (self ) -> None :
3843
3840
super ().apply_mirror ()
3844
3841
self .turn = not self .turn
3845
3842
3846
- def mirror (self : BoardT ) -> BoardT :
3843
+ def mirror (self ) -> Self :
3847
3844
"""
3848
3845
Returns a mirrored copy of the board.
3849
3846
@@ -3858,7 +3855,7 @@ def mirror(self: BoardT) -> BoardT:
3858
3855
board .apply_mirror ()
3859
3856
return board
3860
3857
3861
- def copy (self : BoardT , * , stack : Union [bool , int ] = True ) -> BoardT :
3858
+ def copy (self , * , stack : Union [bool , int ] = True ) -> Self :
3862
3859
"""
3863
3860
Creates a copy of the board.
3864
3861
0 commit comments