Skip to content

Commit 571658b

Browse files
committed
Use typing_extensions.Self
1 parent 7a59a36 commit 571658b

File tree

5 files changed

+37
-29
lines changed

5 files changed

+37
-29
lines changed

chess/__init__.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from typing import ClassVar, Callable, Counter, Dict, Generic, Hashable, Iterable, Iterator, List, Literal, Mapping, Optional, SupportsInt, Tuple, Type, TypeVar, Union
2626

2727
if typing.TYPE_CHECKING:
28-
from typing_extensions import TypeAlias
28+
from typing_extensions import Self, TypeAlias
2929

3030

3131
EnPassantSpec = Literal["legal", "fen", "xfen"]
@@ -1455,7 +1455,7 @@ def apply_transform(self, f: Callable[[Bitboard], Bitboard]) -> None:
14551455
self.occupied = f(self.occupied)
14561456
self.promoted = f(self.promoted)
14571457

1458-
def transform(self: BaseBoardT, f: Callable[[Bitboard], Bitboard]) -> BaseBoardT:
1458+
def transform(self, f: Callable[[Bitboard], Bitboard]) -> Self:
14591459
"""
14601460
Returns a transformed copy of the board (without move stack)
14611461
by applying a bitboard transformation function.
@@ -1473,11 +1473,11 @@ def transform(self: BaseBoardT, f: Callable[[Bitboard], Bitboard]) -> BaseBoardT
14731473
board.apply_transform(f)
14741474
return board
14751475

1476-
def apply_mirror(self: BaseBoardT) -> None:
1476+
def apply_mirror(self) -> None:
14771477
self.apply_transform(flip_vertical)
14781478
self.occupied_co[WHITE], self.occupied_co[BLACK] = self.occupied_co[BLACK], self.occupied_co[WHITE]
14791479

1480-
def mirror(self: BaseBoardT) -> BaseBoardT:
1480+
def mirror(self) -> Self:
14811481
"""
14821482
Returns a mirrored copy of the board (without move stack).
14831483
@@ -1491,7 +1491,7 @@ def mirror(self: BaseBoardT) -> BaseBoardT:
14911491
board.apply_mirror()
14921492
return board
14931493

1494-
def copy(self: BaseBoardT) -> BaseBoardT:
1494+
def copy(self) -> Self:
14951495
"""Creates a copy of the board."""
14961496
board = type(self)(None)
14971497

@@ -1509,10 +1509,10 @@ def copy(self: BaseBoardT) -> BaseBoardT:
15091509

15101510
return board
15111511

1512-
def __copy__(self: BaseBoardT) -> BaseBoardT:
1512+
def __copy__(self) -> Self:
15131513
return self.copy()
15141514

1515-
def __deepcopy__(self: BaseBoardT, memo: Dict[int, object]) -> BaseBoardT:
1515+
def __deepcopy__(self, memo: Dict[int, object]) -> Self:
15161516
board = self.copy()
15171517
memo[id(self)] = board
15181518
return board
@@ -1694,7 +1694,7 @@ class Board(BaseBoard):
16941694
manipulation.
16951695
"""
16961696

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:
16981698
BaseBoard.__init__(self, None)
16991699

17001700
self.chess960 = chess960
@@ -1786,7 +1786,7 @@ def clear_stack(self) -> None:
17861786
self.move_stack.clear()
17871787
self._stack.clear()
17881788

1789-
def root(self: BoardT) -> BoardT:
1789+
def root(self) -> Self:
17901790
"""Returns a copy of the root position."""
17911791
if self._stack:
17921792
board = type(self)(None, chess960=self.chess960)
@@ -2307,7 +2307,7 @@ def is_repetition(self, count: int = 3) -> bool:
23072307
def _push_capture(self, move: Move, capture_square: Square, piece_type: PieceType, was_promoted: bool) -> None:
23082308
pass
23092309

2310-
def push(self: BoardT, move: Move) -> None:
2310+
def push(self, move: Move) -> None:
23112311
"""
23122312
Updates the position with the given *move* and puts it onto the
23132313
move stack.
@@ -2428,7 +2428,7 @@ def push(self: BoardT, move: Move) -> None:
24282428
# Swap turn.
24292429
self.turn = not self.turn
24302430

2431-
def pop(self: BoardT) -> Move:
2431+
def pop(self) -> Move:
24322432
"""
24332433
Restores the previous position and returns the last move from the stack.
24342434
@@ -2838,7 +2838,7 @@ def _validate_epd_opcode(self, opcode: str) -> None:
28382838
if blacklisted in opcode:
28392839
raise ValueError(f"invalid character {blacklisted!r} in epd opcode: {opcode!r}")
28402840

2841-
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]]]:
28422842
operations: Dict[str, Union[None, str, int, float, Move, List[Move]]] = {}
28432843
state = "opcode"
28442844
opcode = ""
@@ -3831,16 +3831,16 @@ def apply_transform(self, f: Callable[[Bitboard], Bitboard]) -> None:
38313831
self.ep_square = None if self.ep_square is None else msb(f(BB_SQUARES[self.ep_square]))
38323832
self.castling_rights = f(self.castling_rights)
38333833

3834-
def transform(self: BoardT, f: Callable[[Bitboard], Bitboard]) -> BoardT:
3834+
def transform(self, f: Callable[[Bitboard], Bitboard]) -> Self:
38353835
board = self.copy(stack=False)
38363836
board.apply_transform(f)
38373837
return board
38383838

3839-
def apply_mirror(self: BoardT) -> None:
3839+
def apply_mirror(self) -> None:
38403840
super().apply_mirror()
38413841
self.turn = not self.turn
38423842

3843-
def mirror(self: BoardT) -> BoardT:
3843+
def mirror(self) -> Self:
38443844
"""
38453845
Returns a mirrored copy of the board.
38463846
@@ -3855,7 +3855,7 @@ def mirror(self: BoardT) -> BoardT:
38553855
board.apply_mirror()
38563856
return board
38573857

3858-
def copy(self: BoardT, *, stack: Union[bool, int] = True) -> BoardT:
3858+
def copy(self, *, stack: Union[bool, int] = True) -> Self:
38593859
"""
38603860
Creates a copy of the board.
38613861

chess/engine.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,7 @@ def pipe_data_received(self, fd: int, data: Union[bytes, str]) -> None:
970970
def error_line_received(self, line: str) -> None:
971971
LOGGER.warning("%s: stderr >> %s", self, line)
972972

973-
def _line_received(self: Protocol, line: str) -> None:
973+
def _line_received(self, line: str) -> None:
974974
LOGGER.debug("%s: >> %s", self, line)
975975

976976
self.line_received(line)

chess/pgn.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
from typing import Any, Callable, Dict, Generic, Iterable, Iterator, List, Literal, Mapping, MutableMapping, Set, TextIO, Tuple, Type, TypeVar, Optional, Union
1616
from chess import Color, Square
1717

18+
if typing.TYPE_CHECKING:
19+
from typing_extensions import Self
20+
1821

1922
LOGGER = logging.getLogger(__name__)
2023

@@ -1014,10 +1017,10 @@ def __iter__(self) -> Iterator[str]:
10141017
def __len__(self) -> int:
10151018
return len(self._tag_roster) + len(self._others)
10161019

1017-
def copy(self: HeadersT) -> HeadersT:
1020+
def copy(self) -> Self:
10181021
return type(self)(self)
10191022

1020-
def __copy__(self: HeadersT) -> HeadersT:
1023+
def __copy__(self) -> Self:
10211024
return self.copy()
10221025

10231026
def __repr__(self) -> str:

chess/syzygy.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
from types import TracebackType
1515
from typing import Deque, Dict, Iterable, Iterator, List, Optional, Set, Tuple, Type, TypeVar, Union
1616

17+
if typing.TYPE_CHECKING:
18+
from typing_extensions import Self
19+
1720

1821
UINT64_BE = struct.Struct(">Q")
1922
UINT32 = struct.Struct("<I")
@@ -532,8 +535,6 @@ class PawnFileDataDtz:
532535
norm: List[int]
533536

534537

535-
TableT = TypeVar("TableT", bound="Table")
536-
537538
class Table:
538539
size: List[int]
539540

@@ -1025,7 +1026,7 @@ def close(self) -> None:
10251026
self.data.close()
10261027
self.data = None
10271028

1028-
def __enter__(self: TableT) -> TableT:
1029+
def __enter__(self) -> Self:
10291030
return self
10301031

10311032
def __exit__(self, exc_type: Optional[Type[BaseException]], exc_value: Optional[BaseException], traceback: Optional[TracebackType]) -> None:

chess/variant.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
import chess
44
import itertools
5+
import typing
56

67
from typing import Dict, Generic, Hashable, Iterable, Iterator, List, Optional, Type, TypeVar, Union
78

9+
if typing.TYPE_CHECKING:
10+
from typing_extensions import Self
11+
812

913
class SuicideBoard(chess.Board):
1014

@@ -795,23 +799,23 @@ def _transposition_key(self) -> Hashable:
795799
return (super()._transposition_key(),
796800
self.remaining_checks[chess.WHITE], self.remaining_checks[chess.BLACK])
797801

798-
def copy(self: ThreeCheckBoardT, stack: Union[bool, int] = True) -> ThreeCheckBoardT:
802+
def copy(self, stack: Union[bool, int] = True) -> Self:
799803
board = super().copy(stack=stack)
800804
board.remaining_checks = self.remaining_checks.copy()
801805
if stack:
802806
stack = len(self.move_stack) if stack is True else stack
803807
board._three_check_stack = self._three_check_stack[-stack:]
804808
return board
805809

806-
def root(self: ThreeCheckBoardT) -> ThreeCheckBoardT:
810+
def root(self) -> Self:
807811
if self._three_check_stack:
808812
board = super().root()
809813
self._three_check_stack[0].restore(board)
810814
return board
811815
else:
812816
return self.copy(stack=False)
813817

814-
def mirror(self: ThreeCheckBoardT) -> ThreeCheckBoardT:
818+
def mirror(self) -> Self:
815819
board = super().mirror()
816820
board.remaining_checks[chess.WHITE] = self.remaining_checks[chess.BLACK]
817821
board.remaining_checks[chess.BLACK] = self.remaining_checks[chess.WHITE]
@@ -865,7 +869,7 @@ def __len__(self) -> int:
865869
def __repr__(self) -> str:
866870
return f"CrazyhousePocket('{self}')"
867871

868-
def copy(self: CrazyhousePocketT) -> CrazyhousePocketT:
872+
def copy(self) -> Self:
869873
"""Returns a copy of this pocket."""
870874
pocket = type(self)()
871875
pocket._pieces = self._pieces[:]
@@ -1047,7 +1051,7 @@ def epd(self, shredder: bool = False, en_passant: chess.EnPassantSpec = "legal",
10471051
board_part, info_part = epd.split(" ", 1)
10481052
return f"{board_part}[{str(self.pockets[chess.WHITE]).upper()}{self.pockets[chess.BLACK]}] {info_part}"
10491053

1050-
def copy(self: CrazyhouseBoardT, stack: Union[bool, int] = True) -> CrazyhouseBoardT:
1054+
def copy(self, stack: Union[bool, int] = True) -> Self:
10511055
board = super().copy(stack=stack)
10521056
board.pockets[chess.WHITE] = self.pockets[chess.WHITE].copy()
10531057
board.pockets[chess.BLACK] = self.pockets[chess.BLACK].copy()
@@ -1056,15 +1060,15 @@ def copy(self: CrazyhouseBoardT, stack: Union[bool, int] = True) -> CrazyhouseBo
10561060
board._crazyhouse_stack = self._crazyhouse_stack[-stack:]
10571061
return board
10581062

1059-
def root(self: CrazyhouseBoardT) -> CrazyhouseBoardT:
1063+
def root(self) -> Self:
10601064
if self._crazyhouse_stack:
10611065
board = super().root()
10621066
self._crazyhouse_stack[0].restore(board)
10631067
return board
10641068
else:
10651069
return self.copy(stack=False)
10661070

1067-
def mirror(self: CrazyhouseBoardT) -> CrazyhouseBoardT:
1071+
def mirror(self) -> Self:
10681072
board = super().mirror()
10691073
board.pockets[chess.WHITE] = self.pockets[chess.BLACK].copy()
10701074
board.pockets[chess.BLACK] = self.pockets[chess.WHITE].copy()

0 commit comments

Comments
 (0)