Skip to content

TYP: np.random.Generator.integers has incorrect return type int for overload #28736

@kwohlfahrt

Description

@kwohlfahrt

Describe the issue:

rng.integers(low, high) returns an np.int64, but the type hint incorrectly says it returns an int.

This causes problems later, in code such as this:

import torch
import numpy as np

x = rng.integers(0, 10)
torch.set_grad_enabled(x == 4)
# TypeError: set_grad_enabled(): argument 'enabled' (position 1) must be bool, not numpy.bool_

Reproduce the code example:

import numpy as np

rng = np.random.default_rng()
x = rng.integers(0, 10)

assert isinstance(x.item(), int) # fails at type-checking
assert isinstance(x, int)        # fails at runtime

Error message:

# mypy error
test.py:6: error: "int" has no attribute "item"  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

# runtime error
Traceback (most recent call last):
  File "/app/test.py", line 7, in <module>
    assert isinstance(x, int)
           ~~~~~~~~~~^^^^^^^^
AssertionError

Python and NumPy Versions:

numpy: 2.2.4
Python: 3.13.2 (main, Feb 6 2025, 23:44:09) [GCC 12.2.0]

Type-checker version and settings:

Mypy version:

# mypy --version
mypy 1.15.0 (compiled: yes)

Mypy was run with mypy . in an otherwise empty directory (in python:3 Docker image).

Additional typing packages.

Packages were installed with pip install mypy numpy, resulting in the following installations:

Successfully installed mypy-1.15.0 mypy_extensions-1.0.0 numpy-2.2.4 typing_extensions-4.13.2

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions