Repository navigation
Add type stub for numba.misc.special (prange, pndindex) - #10651
YuanfengZhang wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
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.pyiwith overloads forprange. - Adds a stub signature for
pndindexreturning a NumPyndindexiterator object.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -0,0 +1,12 @@ | |||
| from typing import overload | |||
| @overload | ||
| def prange(start: int, stop: int, step: int, /) -> range: ... | ||
|
|
||
| def pndindex(*args: int) -> np.ndindex: ... |
|
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. |
|
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 |
|
Thank you for the PR, @YuanfengZhang ! There was a full set of changes with stubs for |
|
Great. Happy to see it fixed by Pro instead of myself. |
DESCRIPTION
Add a PEP 561 type stub (
special.pyi) fornumba.misc.specialto fix a false-positivenot-iterablediagnostic emitted by static type checkers (ty 0.0.49+, pyright 1.1.410+, mypy) onprangeloops.PROBLEM
prangeis implemented as a class whose__new__delegates torange:At runtime,
prange(n)returns a standardrangeobject which is fully iterable, but the mianstream type checkers don't know it, as they seeprangeas a class and infer that calling it returns aprangeinstance, which has no__iter__method. This triggers a false-positive error on everyfor i in prange(n):loop:Users then have to add
# type: ignore[not-iterable]on everyprangeline, which is noisy and masks real bugs.FIX
This PR adds
numba/misc/special.pyiwith three@overloadsignatures forprange, matchingrange's API:pndindexis also stubbed for completeness (-> np.ndindex).The existing
.pyifiles 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-iterablediagnostic is resolved on the newest numba release.