Skip to content

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

Open
kwy404 wants to merge 1 commit into
niklasf:masterfrom
kwy404:fix/set-eval-mate-zero
Open

kwy404 wants to merge 1 commit into
niklasf:masterfrom
kwy404:fix/set-eval-mate-zero

Conversation

@kwy404

@kwy404 kwy404 commented Sep 26, 2026

Copy link
Copy Markdown

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.

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.
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.

1 participant