Skip to content

mypy / type hints: np.bool_ and bool incompatible #18876

@mlondschien

Description

@mlondschien

Reproducing code example:

# example.py
import numpy as np

def f(x: bool) -> bool:
    return x

y = np.bool_(True)

print(f(y))

yields

$ mypy example.py
example.py:9: error: Argument 1 to "f" has incompatible type "bool_"; expected "bool"
Found 1 error in 1 file (checked 1 source file)

The same happens for the inverse:

# example.py
import numpy as np

def f(x: np.bool_) -> np.bool_:
    return x

y = True

print(f(y))

results in

example.py:9: error: Argument 1 to "f" has incompatible type "bool"; expected "bool_"

np.float_ and np.int_ work fine.

NumPy/Python version information:

1.20.2 3.8.8 | packaged by conda-forge | (default, Feb 20 2021, 16:22:27)
[GCC 9.3.0]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions