Skip to content

was_into_check behavior on multiple kings #1169

Closed

Description

@winapiadmin

was_into_check (L1995-L1997)

    def was_into_check(self) -> bool:
        king = self.king(not self.turn)
        return king is not None and self.is_attacked_by(self.turn, king)

Here it only picks one king.... but if the position loaded has multiple kings, it only return the bool of one king which depends on FEN parsing order, that means - a flipped position can bring different king of the corresponding side to move.

While it isn't too much - this is kind of UB for pseudo-legal FENs, and it affects Board.is_valid, Board.status and possibly its variants.

Activity

  1. EiSiMo commented on Feb 3, 2026

    @EiSiMo

    This reproduces the bug:

    import chess
    
    board = chess.Board(None)
    board.set_piece_at(chess.A1, chess.Piece(chess.ROOK, chess.WHITE))
    board.set_piece_at(chess.A8, chess.Piece(chess.KING, chess.BLACK)) # attacked
    board.set_piece_at(chess.H8, chess.Piece(chess.KING, chess.BLACK)) # safe
    board.turn = chess.WHITE
    
    print(board)
    print(board.was_into_check())

    returns False, but should be True (because the king at A8 is in check)

    I would be happy to submit a PR to fix this (by iterating over the kings bitboard), but I wanted to ask first: is this "single king" assumption kept intentionally for performance reasons?

    Please assign this to me if you would like me to implement the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions