Skip to content

numpy uint8 becomes signedinteger on multiplication #14146

@hughperkins

Description

@hughperkins

Bug Report

Multiplying an NDArray[np.uint8] by 1 converts the mypy type into NDArray[signedinteger]

Consider the following code:

import numpy as np
from numpy.typing import NDArray


def get_uint8() -> NDArray[np.uint8]:
    return np.random.rand(3).astype(np.uint8)


a = get_uint8()  # a is correctly NDArray[np.uint8]
b = a * 1        # b is NDArray[signedinteger]

In the last line, b shows as NDArray[signedinteger] instead of NDArray[np.uint8]. There is no obvious reason why multplying an np.uint8 by 1 should change the dtype. Indeed if we add a print:

print('b.dtype', b.dtype)

... this outputs b.dtype uint8

Expected Behavior

  • in the program above, the mypy type for b should be NDArray[np.uint8]

Actual Behavior

  • in the program above, the mypy type for b is NDArray[signedinteger]

Your Environment

$ python -V
Python 3.10.7
$ pip freeze | grep mypy
mypy==0.991
mypy-extensions==0.4.3
$ pip freeze | grep numpy
numpy==1.23.4
$ uname -a
Darwin Hughs-MacBook-Air.local 21.6.0 Darwin Kernel Version 21.6.0: Thu Sep 29 20:11:33 PDT 2022; root:xnu-8020.240.7~1/RELEASE_ARM64_T8110 arm64
$ cat setup.cfg
[mypy]
ignore_missing_imports = True

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions