Skip to content

Fix GameNode.set_eval() dropping mate 0 scores - #1213

Merged
niklasf merged 2 commits into
niklasf:masterfrom
kwy404:fix/set-eval-mate-zero
Nov 22, 2026
Merged

niklasf merged 2 commits into
niklasf:masterfrom
kwy404:fix/set-eval-mate-zero

Conversation

@kwy404

@kwy404 kwy404 commented Sep 26, 2026

Copy link
Copy Markdown
Contributor

Root cause: set_eval() only writes a mate annotation when score.white().mate() is truthy. For a mated position the score is Mate(0) (or MateGiven from the other side), so mate() returns 0, the branch is skipped and nothing is written. Worse, _replace_or_add_annotation("") then removes an existing [%eval ...] from the comment. Since e32f9e1, GameNode.eval() parses [%eval #0], so the reader and writer disagree:

game = chess.pgn.read_game(io.StringIO("1. f3 e5 2. g4 Qh4# { [%eval #0] } 0-1"))
node = game.end()
node.set_eval(node.eval())
node.comments  # [] instead of ["[%eval #0]"]

The same happens when annotating a game with an engine: for the final checkmate position the engine reports score mate 0, and node.set_eval(info["score"]) writes nothing.

Fix: check mate() is not None instead of truthiness, so Mate(0) and MateGiven are written as [%eval #0], which eval() already reads back as the side to move being mated.

Test: test_eval_mate_zero reads the PGN above, writes the parsed eval back and checks that the comment is still [%eval #0] and that eval() still returns Mate(0). It fails on master with Lists differ: [] != ['[%eval #0]'] and passes with the fix. The full python test.py suite passes.

kwy404 and others added 2 commits September 26, 2026 02:46
set_eval() only wrote a mate annotation when score.white().mate() was
truthy, so Mate(0) and MateGiven were silently discarded, and an existing
[%eval ...] annotation was removed instead of being replaced by
[%eval #0]. GameNode.eval() already parses [%eval #0], so writing back the
parsed score lost it.
@niklasf

niklasf commented Nov 22, 2026

Copy link
Copy Markdown
Owner

Thanks.

Merging with one tweak: In variants where the side to move can also be the winner, the score still needs to be dropped instead of writing #0.

@niklasf
niklasf merged commit 1467eb8 into niklasf:master Nov 22, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants