Skip to content

Add type stub for numba.misc.special (prange, pndindex) - #10651

Closed
YuanfengZhang wants to merge 1 commit into
numba:mainfrom
YuanfengZhang:prange_stub
Closed

YuanfengZhang wants to merge 1 commit into
numba:mainfrom
YuanfengZhang:prange_stub

Conversation

@YuanfengZhang

Copy link
Copy Markdown

DESCRIPTION

Add a PEP 561 type stub (special.pyi) for numba.misc.special to fix a false-positive not-iterable diagnostic emitted by static type checkers (ty 0.0.49+, pyright 1.1.410+, mypy) on prange loops.

PROBLEM

prange is implemented as a class whose __new__ delegates to range:

# numba/misc/special.py
class prange(object):
    def __new__(cls, *args):
        return range(*args)

At runtime, prange(n) returns a standard range object which is fully iterable, but the mianstream type checkers don't know it, as they see prange as a class and infer that calling it returns a prange instance, which has no __iter__ method. This triggers a false-positive error on every for i in prange(n): loop:

error[not-iterable]: Object of type `prange` is not iterable
  --> evaluate.py:1818
   |
...  for i in prange(n_sites):
   |          ^^^^^^^^^^^^^^^
   |          It doesn't have an `__iter__` method or a `__getitem__` method

Users then have to add # type: ignore[not-iterable] on every prange line, which is noisy and masks real bugs.

FIX

This PR adds numba/misc/special.pyi with three @overload signatures for prange, matching range's API:

@overload
def prange(stop: int, /) -> range: ...
@overload
def prange(start: int, stop: int, /) -> range: ...
@overload
def prange(start: int, stop: int, step: int, /) -> range: ...

pndindex is also stubbed for completeness (-> np.ndindex).

The existing .pyi files in numba (e.g. _helperlib.pyi, core/decorators.pyi) follow the same PEP 561 pattern, so this is consistent with numba's existing approach to type stubs.

TEST

None unit test is prepared since it's static and won't hurt any other functions. It has been manually verified with ty 0.0.49 and pyright 1.1.410 that the not-iterable diagnostic is resolved on the newest numba release.

Copilot AI review requested due to automatic review settings June 15, 2026 09:07

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds a type stub for numba.misc.special to provide static typing for Numba’s special iteration helpers.

Changes:

  • Introduces numba/misc/special.pyi with overloads for prange.
  • Adds a stub signature for pndindex returning a NumPy ndindex iterator object.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread numba/misc/special.pyi
@@ -0,0 +1,12 @@
from typing import overload
Comment thread numba/misc/special.pyi
@overload
def prange(start: int, stop: int, step: int, /) -> range: ...

def pndindex(*args: int) -> np.ndindex: ...
@DrTodd13

Copy link
Copy Markdown
Contributor

Some discussion about how the Numba mypy checker evidently wasn't seeing this problem before so we need to look there and see if it was failing somehow before and if it wasn't previously seeing this problem then add a test to notice this problem that you did and make sure this PR fixes it. More comments from others to come.

@jorenham

Copy link
Copy Markdown
Contributor

Stubtest currently ignores these stubs, which is why we're not seeing any stubtest errors about the function stubs that are missing and the incorrect pndindex definition here.

@swap357

swap357 commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Thank you for the PR, @YuanfengZhang ! There was a full set of changes with stubs for prange, pndindex and allowlist on #10701 which is now merged, hence this can be closed.

@swap357 swap357 closed this Aug 13, 2026
@YuanfengZhang

Copy link
Copy Markdown
Author

Great. Happy to see it fixed by Pro instead of myself.

@YuanfengZhang
YuanfengZhang deleted the prange_stub branch August 13, 2026 05:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants