Skip to content

Commit

Permalink
Declare chess.popcount in pyright compatible way
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf committed Oct 8, 2023
1 parent 1dd3e1e commit 3a99f1b
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions chess/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,12 @@ def scan_reversed(bb: Bitboard) -> Iterator[Square]:
yield r
bb ^= BB_SQUARES[r]

# Python 3.10 or fallback.
popcount: Callable[[Bitboard], int] = getattr(int, "bit_count", lambda bb: bin(bb).count("1"))
try:
# Python 3.10
popcount = Bitboard.bit_count
except AttributeError:
def popcount(self: Bitboard) -> int:
return bin(self).count("1")

def flip_vertical(bb: Bitboard) -> Bitboard:
# https://www.chessprogramming.org/Flipping_Mirroring_and_Rotating#FlipVertically
Expand Down

0 comments on commit 3a99f1b

Please sign in to comment.