Search code examples
pythonnumpypython-typingmypy

mypy warning on numpy.apply_along_axis


Edit Oct-18-2024:

An even more trivial reproduction of the problem is shown below.

mypy_arg_type.py:

import numpy as np
from numpy.typing import NDArray
import random

def winner(_: NDArray[np.bytes_]) -> bytes | None:
    return b"." if bool(random.randint(0, 1)) else None

board = np.full((2, 2), ".", "|S1")
for w in np.apply_along_axis(winner, 0, board):
    print(w)

>> python mypy_arg_type.py

b'.'
None

>> mypy mypy_arg_type.py

mypy_arg_type.py:9: error: Argument 1 to "apply_along_axis" has incompatible type "Callable[[ndarray[Any, dtype[bytes_]]], bytes | None]"; expected "Callable[[ndarray[Any, dtype[Any]]], _SupportsArray[dtype[Never]] | _NestedSequence[_SupportsArray[dtype[Never]]]]"  [arg-type]
mypy_arg_type.py:9: note: This is likely because "winner" has named arguments: "_". Consider marking them positional-only
Found 1 error in 1 file (checked 1 source file)

Original question:

I'm working on a problem to determine the winner of a Connect Four game, given the position of the pieces on the board. The board is of size 6x7, and each column is marked with a letter from A to G. The winner, if any, will have 4 pieces of identical color in a row, column, diagonal or anti-diagonal.

Example:

Input: ["A_Red", "B_Yellow", "A_Red", "B_Yellow", "A_Red", "B_Yellow", "G_Red", "B_Yellow"]

Board:

R Y . . . . R
R Y . . . . .
R Y . . . . .
. Y . . . . .
. . . . . . .
. . . . . . .

Winner: Yellow

The following code determines a winner.

import itertools
import numpy as np
from numpy.typing import NDArray

def who_is_winner(pieces: list[str]) -> str:
    def parse_board() -> NDArray[np.bytes_]:
        m, n = 6, 7
        indices = [0] * n
        # https://numpy.org/doc/stable/user/basics.strings.html#fixed-width-data-types
        # One-byte encoding, the byteorder is ‘|’ (not applicable)
        board = np.full((m, n), ".", "|S1")
        for p in pieces:
            col = ord(p[0]) - ord("A")
            board[indices[col], col] = p[2]
            indices[col] += 1

        return board

    def winner(arr: NDArray[np.bytes_]) -> np.bytes_ | None:
        i = len(arr)
        xs = next(
            (xs for j in range(i - 3) if (xs := set(arr[j : j + 4])) < {b"R", b"Y"}),
            {None},
        )
        return xs.pop()

    def axis(x: int) -> np.bytes_ | None:
        # https://numpy.org/doc/2.0/reference/generated/numpy.apply_along_axis.html#numpy-apply-along-axis
        # Axis 0 is column-wise, 1 is row-wise.
        return next(
            (w for w in np.apply_along_axis(winner, x, board) if w is not None), None
        )

    def diag(d: int) -> np.bytes_ | None:
        # https://numpy.org/doc/stable/reference/generated/numpy.diagonal.html#numpy-diagonal
        # Diagonal number is w.r.t. the main diagonal.
        b = board if bool(d) else np.fliplr(board)
        return next(
            (w for d in range(-3, 4) if (w := winner(b.diagonal(d))) is not None), None
        )

    board = parse_board()
    match next(
        (
            w
            for f, i in itertools.product((axis, diag), (0, 1))
            if (w := f(i)) is not None
        ),
        None,
    ):
        case b"Y":
            return "Yellow"
        case b"R":
            return "Red"
        case _:
            return "Draw"

However, this generates a mypy violation as follows:

error: Argument 1 to "apply_along_axis" has incompatible type "Callable[[ndarray[Any, dtype[bytes_]]], bytes_ | None]"; expected "Callable[[ndarray[Any, dtype[Any]]], _SupportsArray[dtype[bytes_]] | _NestedSequence[_SupportsArray[dtype[bytes_]]]]"  [arg-type]
note: This is likely because "winner" has named arguments: "arr". Consider marking them positional-only

According to the documentation of apply_along_axis, it is supposed to return a single value, which is consistent with the code above.

How to fix this violation? Making function winner positional-only makes no difference, except that suggestion is gone.

I'm using Python 3.12.5 with mypy 1.11.2.


Solution

  • By studying the overloaded signatures of apply_along_axis, I came to the conclusion that it is not defined to return None, causing the mypy violation. There's no real reason to not return None though, and I've opened a mypy ticket about it. We will see whether it gets kicked to numpy.

    Overload 1:

    def [_P`-1, _SCT: generic] apply_along_axis(func1d: Callable[[ndarray[Any, dtype[Any]], **_P], _SupportsArray[dtype[_SCT]] | _NestedSequence[_SupportsArray[dtype[_SCT]]]], axis: SupportsIndex, arr: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], *args: _P.args, **kwargs: _P.kwargs) -> ndarray[Any, dtype[_SCT]]
    

    Overload 2:

    def [_P`-1] apply_along_axis(func1d: Callable[[ndarray[Any, dtype[Any]], **_P], Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]], axis: SupportsIndex, arr: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], *args: _P.args, **kwargs: _P.kwargs) -> ndarray[Any, dtype[Any]]
    

    I modified the function winner to return a null byte string (b"") instead of None, and replaced all return types of np.bytes_ to bytes. That fixed the problem.