Thanks to visit codestin.com
Credit goes to github.com

Skip to content

TYP: mypy returns dtype of np.bool[Literal[False]] for array of bools #29245

Open
@bkeryan

Description

@bkeryan

Describe the issue:

When you create an array with dtype=np.bool, mypy says the returned array's dtype is np.dtype[np.bool[Literal[False]]] despite the fact that it's an array, not a literal, and may contain values other than False. Pyright says the returned array's dtype is np.dtype[np.bool[bool]], which makes more sense to me because the array items are bools but they are not necessarily False.

I also filed a bug about TypeError when subscripting np.bool at run time: #29247

Reproduce the code example:

import numpy as np
x = np.array([True, False], np.bool)
reveal_type(x)

Error message:

# mypy 1.16.1
test.py:3: note: Revealed type is "numpy.ndarray[builtins.tuple[Any, ...], numpy.dtype[numpy.bool[Literal[False]]]]"
Success: no issues found in 1 source file

# pyright 1.1.402
test.py:3:13 - information: Type of "x" is "ndarray[tuple[Any, ...], dtype[numpy.bool[builtins.bool]]]"
0 errors, 0 warnings, 1 information

Python and NumPy Versions:

numpy 2.3.0
python 3.11.9 (tags/v3.11.9:de54cf5, Apr 2 2024, 10:12:12) [MSC v.1938 64 bit (AMD64)]

Type-checker version and settings:

mypy 1.16.1, mypy test.py, no configuration options
pyright 1.1.402, pyright test.py, no configuration options

Additional typing packages.

No response

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