Skip to content

Commit

Permalink
Add fallback for override decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf committed Jul 31, 2024
1 parent 5d8e82d commit 3bc53b5
Showing 1 changed file with 33 additions and 26 deletions.
59 changes: 33 additions & 26 deletions chess/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
from types import TracebackType
from typing import Any, Callable, Coroutine, Deque, Dict, Generator, Generic, Iterable, Iterator, List, Literal, Mapping, MutableMapping, Optional, Tuple, Type, TypedDict, TypeVar, Union

try:
from typing import override
except:
# Before Python 3.12
def override(fn): # type: ignore
return fn

if typing.TYPE_CHECKING:
from typing_extensions import Self

Expand Down Expand Up @@ -1317,16 +1324,16 @@ def __init__(self, engine: UciProtocol):
super().__init__(engine)
self.engine = engine

@typing.override
@override
def check_initialized(self) -> None:
if self.engine.initialized:
raise EngineError("engine already initialized")

@typing.override
@override
def start(self) -> None:
self.engine.send_line("uci")

@typing.override
@override
def line_received(self, line: str) -> None:
token, remaining = _next_token(line)
if line.strip() == "uciok" and not self.result.done():
Expand Down Expand Up @@ -1415,7 +1422,7 @@ def __init__(self, engine: UciProtocol) -> None:
def start(self) -> None:
self.engine._isready()

@typing.override
@override
def line_received(self, line: str) -> None:
if line.strip() == "readyok":
self.result.set_result(None)
Expand Down Expand Up @@ -1574,7 +1581,7 @@ def __init__(self, engine: UciProtocol):
# MultiPV never change between pondering play commands.
engine.may_ponderhit = board if ponder and not engine.first_game and game == engine.game and not engine._changed_options(new_options) else None

@typing.override
@override
def start(self) -> None:
self.info: InfoDict = {}
self.pondering: Optional[chess.Board] = None
Expand Down Expand Up @@ -1605,7 +1612,7 @@ def start(self) -> None:
else:
self._readyok()

@typing.override
@override
def line_received(self, line: str) -> None:
token, remaining = _next_token(line)
if token == "info":
Expand Down Expand Up @@ -1662,15 +1669,15 @@ def end(self) -> None:
engine.may_ponderhit = None
self.set_finished()

@typing.override
@override
def cancel(self) -> None:
if self.engine.may_ponderhit and self.pondering and self.engine.may_ponderhit.move_stack == self.pondering.move_stack and self.engine.may_ponderhit == self.pondering:
self.engine.ponderhit = True
self.end()
else:
self.engine.send_line("stop")

@typing.override
@override
def engine_terminated(self, exc: Exception) -> None:
# Allow terminating engine while pondering.
if not self.result.done():
Expand Down Expand Up @@ -1705,7 +1712,7 @@ def start(self) -> None:
else:
self._readyok()

@typing.override
@override
def line_received(self, line: str) -> None:
token, remaining = _next_token(line)
if token == "info":
Expand Down Expand Up @@ -1738,11 +1745,11 @@ def _bestmove(self, arg: str) -> None:
self.set_finished()
self.analysis.set_finished(best)

@typing.override
@override
def cancel(self) -> None:
self.engine.send_line("stop")

@typing.override
@override
def engine_terminated(self, exc: Exception) -> None:
LOGGER.debug("%s: Closing analysis because engine has been terminated (error: %s)", self.engine, exc)
self.analysis.set_exception(exc)
Expand Down Expand Up @@ -1978,12 +1985,12 @@ def __init__(self, engine: XBoardProtocol):
super().__init__(engine)
self.engine = engine

@typing.override
@override
def check_initialized(self) -> None:
if self.engine.initialized:
raise EngineError("engine already initialized")

@typing.override
@override
def start(self) -> None:
self.engine.send_line("xboard")
self.engine.send_line("protover 2")
Expand All @@ -1993,7 +2000,7 @@ def timeout(self) -> None:
LOGGER.error("%s: Timeout during initialization", self.engine)
self.end()

@typing.override
@override
def line_received(self, line: str) -> None:
token, remaining = _next_token(line)
if token.startswith("#"):
Expand Down Expand Up @@ -2152,13 +2159,13 @@ def __init__(self, engine: XBoardProtocol):
super().__init__(engine)
self.engine = engine

@typing.override
@override
def start(self) -> None:
n = id(self) & 0xffff
self.pong = f"pong {n}"
self.engine._ping(n)

@typing.override
@override
def line_received(self, line: str) -> None:
if line == self.pong:
self.result.set_result(None)
Expand All @@ -2179,7 +2186,7 @@ def __init__(self, engine: XBoardProtocol):
super().__init__(engine)
self.engine = engine

@typing.override
@override
def start(self) -> None:
self.play_result = PlayResult(None, None)
self.stopped = False
Expand Down Expand Up @@ -2221,7 +2228,7 @@ def start(self) -> None:
self.engine.send_line("hard" if ponder else "easy")
self.engine.send_line("go")

@typing.override
@override
def line_received(self, line: str) -> None:
token, remaining = _next_token(line)
if token == "move":
Expand Down Expand Up @@ -2301,7 +2308,7 @@ def _ping_after_move(self) -> None:
self.pong_after_move = f"pong {n}"
self.engine._ping(n)

@typing.override
@override
def cancel(self) -> None:
if self.stopped:
return
Expand All @@ -2317,7 +2324,7 @@ def cancel(self) -> None:
self.pong_after_ponder = f"pong {n}"
self.engine._ping(n)

@typing.override
@override
def engine_terminated(self, exc: Exception) -> None:
# Allow terminating engine while pondering.
if not self.result.done():
Expand All @@ -2337,7 +2344,7 @@ def __init__(self, engine: XBoardProtocol):
super().__init__(engine)
self.engine = engine

@typing.override
@override
def start(self) -> None:
self.stopped = False
self.best_move: Optional[chess.Move] = None
Expand All @@ -2364,7 +2371,7 @@ def start(self) -> None:
else:
self.time_limit_handle = None

@typing.override
@override
def line_received(self, line: str) -> None:
token, remaining = _next_token(line)
if token.startswith("#"):
Expand Down Expand Up @@ -2405,7 +2412,7 @@ def end(self) -> None:
self.set_finished()
self.analysis.set_finished(BestMove(self.best_move, None))

@typing.override
@override
def cancel(self) -> None:
if self.stopped:
return
Expand All @@ -2418,7 +2425,7 @@ def cancel(self) -> None:
self.final_pong = f"pong {n}"
self.engine._ping(n)

@typing.override
@override
def engine_terminated(self, exc: Exception) -> None:
LOGGER.debug("%s: Closing analysis because engine has been terminated (error: %s)", self.engine, exc)

Expand Down Expand Up @@ -2466,7 +2473,7 @@ def __init__(self, engine: XBoardProtocol):
super().__init__(engine)
self.engine = engine

@typing.override
@override
def start(self) -> None:
self.engine._configure(options)
self.engine.target_config.update({name: value for name, value in options.items() if value is not None})
Expand Down Expand Up @@ -2497,7 +2504,7 @@ def __init__(self, engine: XBoardProtocol):
super().__init__(engine)
self.engine = engine

@typing.override
@override
def start(self) -> None:
if game_ending and any(c in game_ending for c in "{}\n\r"):
raise EngineError(f"invalid line break or curly braces in game ending message: {game_ending!r}")
Expand Down

0 comments on commit 3bc53b5

Please sign in to comment.