Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kwohlfahrt opened this issue Apr 16, 2025 · 1 comment · Fixed by #28738
Closed

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

kwohlfahrt opened this issue Apr 16, 2025 · 1 comment · Fixed by #28738
Assignees

Comments

@kwohlfahrt
Copy link

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
@jorenham
Copy link
Member

Thanks for reporting this @kwohlfahrt. This will be fixed in the upcoming NumPy 2.2.5 release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants