Skip to content

Commit 4980ca2

Browse files
committed
typed lowerbound/upperbound in InfoDict
1 parent 454c537 commit 4980ca2

3 files changed

Lines changed: 62 additions & 33 deletions

File tree

‎CHANGELOG.rst‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
Changelog for python-chess
22
==========================
33

4-
Unreleased
5-
----------
6-
7-
Bugfixes:
8-
9-
* ``chess.engine``: Clear stale ``lowerbound``/``upperbound`` flags from
10-
aggregated info when a subsequent exact score is received. See
11-
`#1086 <https://github.com/niklasf/python-chess/issues/1086>`_.
12-
134
New in v1.11.2 (25th Feb 2025)
145
------------------------------
156

‎chess/engine.py‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ class InfoDict(TypedDict, total=False):
240240
Others: ``tbhits``, ``currmove``, ``currmovenumber``, ``hashfull``,
241241
``cpuload``, ``refutation``, ``currline``, ``ebf`` (effective branching factor),
242242
``wdl`` (a :class:`~chess.engine.PovWdl`), and ``string``.
243+
244+
``lowerbound`` or ``upperbound`` is ``True`` if the ``score`` is only a
245+
bound.
243246
"""
244247

245248
score: PovScore
@@ -260,6 +263,16 @@ class InfoDict(TypedDict, total=False):
260263
ebf: float
261264
wdl: PovWdl
262265
string: str
266+
lowerbound: bool
267+
upperbound: bool
268+
269+
270+
def _merge_info(aggregated: InfoDict, info: InfoDict) -> None:
271+
if "score" in info:
272+
# Bound flags belong to the previous score.
273+
aggregated.pop("lowerbound", None)
274+
aggregated.pop("upperbound", None)
275+
aggregated.update(info)
263276

264277

265278
class PlayResult:
@@ -1784,7 +1797,7 @@ def _readyok(self) -> None:
17841797

17851798
def _info(self, arg: str) -> None:
17861799
if not self.pondering:
1787-
self.info.update(_parse_uci_info(arg, self.engine.board, info))
1800+
_merge_info(self.info, _parse_uci_info(arg, self.engine.board, info))
17881801

17891802
def _bestmove(self, arg: str) -> None:
17901803
if self.pondering:
@@ -1991,8 +2004,11 @@ def _parse_uci_info(arg: str, root_board: chess.Board, selector: Info = INFO_ALL
19912004
kind, remaining_line = _next_token(remaining_line)
19922005
value, remaining_line = _next_token(remaining_line)
19932006
token, remaining_after_token = _next_token(remaining_line)
1994-
if token in ["lowerbound", "upperbound"]:
1995-
info[token] = True # type: ignore
2007+
if token == "lowerbound":
2008+
info["lowerbound"] = True
2009+
remaining_line = remaining_after_token
2010+
elif token == "upperbound":
2011+
info["upperbound"] = True
19962012
remaining_line = remaining_after_token
19972013
if kind == "cp":
19982014
info["score"] = PovScore(Cp(int(value)), root_board.turn)
@@ -2926,10 +2942,7 @@ def post(self, info: InfoDict) -> None:
29262942
multipv = info.get("multipv", 1)
29272943
while len(self.multipv) < multipv:
29282944
self.multipv.append({})
2929-
if "score" in info:
2930-
self.multipv[multipv - 1].pop("lowerbound", None) # type: ignore[typeddict-item]
2931-
self.multipv[multipv - 1].pop("upperbound", None) # type: ignore[typeddict-item]
2932-
self.multipv[multipv - 1].update(info)
2945+
_merge_info(self.multipv[multipv - 1], info)
29332946

29342947
self._queue.put_nowait(info)
29352948

‎test.py‎

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4474,12 +4474,15 @@ async def main():
44744474
mock.expect("ucinewgame")
44754475
mock.expect("isready", ["readyok"])
44764476
mock.expect("position startpos")
4477-
mock.expect("go depth 5", [
4478-
"info depth 5 score cp 30 lowerbound pv e2e4",
4479-
"info depth 5 score cp 20 upperbound pv e2e4",
4480-
"info depth 5 score cp 25 pv e2e4",
4481-
"bestmove e2e4",
4482-
])
4477+
mock.expect(
4478+
"go depth 5",
4479+
[
4480+
"info depth 5 score cp 30 lowerbound pv e2e4",
4481+
"info depth 5 score cp 20 upperbound pv e2e4",
4482+
"info depth 5 score cp 25 pv e2e4",
4483+
"bestmove e2e4",
4484+
],
4485+
)
44834486
info = await protocol.analyse(board, chess.engine.Limit(depth=5))
44844487
self.assertEqual(info["score"], chess.engine.PovScore(chess.engine.Cp(25), chess.WHITE))
44854488
self.assertNotIn("lowerbound", info)
@@ -4488,11 +4491,14 @@ async def main():
44884491

44894492
# Non-score info lines should preserve the current bound flag.
44904493
mock.expect("position startpos")
4491-
mock.expect("go depth 6", [
4492-
"info depth 6 score cp 40 lowerbound pv d2d4",
4493-
"info currmove d2d4",
4494-
"bestmove d2d4",
4495-
])
4494+
mock.expect(
4495+
"go depth 6",
4496+
[
4497+
"info depth 6 score cp 40 lowerbound pv d2d4",
4498+
"info currmove d2d4",
4499+
"bestmove d2d4",
4500+
],
4501+
)
44964502
info = await protocol.analyse(board, chess.engine.Limit(depth=6))
44974503
self.assertEqual(info["score"], chess.engine.PovScore(chess.engine.Cp(40), chess.WHITE))
44984504
self.assertTrue(info.get("lowerbound"))
@@ -4502,12 +4508,15 @@ async def main():
45024508
# Bound flags are cleared per-multipv line independently.
45034509
mock.expect("setoption name MultiPV value 2")
45044510
mock.expect("position startpos")
4505-
mock.expect("go depth 5", [
4506-
"info depth 5 multipv 1 score cp 30 lowerbound pv e2e4",
4507-
"info depth 5 multipv 2 score cp 10 upperbound pv d2d4",
4508-
"info depth 5 multipv 1 score cp 25 pv e2e4",
4509-
"bestmove e2e4",
4510-
])
4511+
mock.expect(
4512+
"go depth 5",
4513+
[
4514+
"info depth 5 multipv 1 score cp 30 lowerbound pv e2e4",
4515+
"info depth 5 multipv 2 score cp 10 upperbound pv d2d4",
4516+
"info depth 5 multipv 1 score cp 25 pv e2e4",
4517+
"bestmove e2e4",
4518+
],
4519+
)
45114520
infos = await protocol.analyse(board, chess.engine.Limit(depth=5), multipv=2)
45124521
self.assertEqual(infos[0]["score"], chess.engine.PovScore(chess.engine.Cp(25), chess.WHITE))
45134522
self.assertNotIn("lowerbound", infos[0])
@@ -4517,6 +4526,22 @@ async def main():
45174526
self.assertNotIn("lowerbound", infos[1])
45184527
mock.assert_done()
45194528

4529+
# Same for info aggregated by play().
4530+
mock.expect("setoption name MultiPV value 1")
4531+
mock.expect("position startpos")
4532+
mock.expect(
4533+
"go depth 5",
4534+
[
4535+
"info depth 5 score cp 30 lowerbound pv e2e4",
4536+
"info depth 5 score cp 25 pv e2e4",
4537+
"bestmove e2e4",
4538+
],
4539+
)
4540+
result = await protocol.play(board, chess.engine.Limit(depth=5), info=chess.engine.INFO_ALL)
4541+
self.assertEqual(result.info["score"], chess.engine.PovScore(chess.engine.Cp(25), chess.WHITE))
4542+
self.assertNotIn("lowerbound", result.info)
4543+
mock.assert_done()
4544+
45204545
asyncio.run(main())
45214546

45224547
def test_uci_result(self):

0 commit comments

Comments
 (0)