Skip to content

TYP: numba stub #44233

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

Merged
merged 6 commits into from
Oct 31, 2021
Merged

TYP: numba stub #44233

merged 6 commits into from
Oct 31, 2021

Conversation

simonjayhawkins
Copy link
Member

partial stub to preserve type signatures for decorated functions.

draft since will rebase once #43828 is merged.

cc @twoertwein @mroeschke

@simonjayhawkins simonjayhawkins added Typing type annotations, mypy/pyright type checking numba numba-accelerated operations labels Oct 29, 2021
@numba.jit(numba.boolean(numba.int64[:]), nopython=True, nogil=True, parallel=False)
@numba.jit(
# error: Any? not callable
numba.boolean(numba.int64[:]), # type: ignore[misc]
Copy link
Member Author

Choose a reason for hiding this comment

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

partial stub does not include numba types so they resolve to Any (and not callable)

@mroeschke not sure of best practice, is it preferable to use the numba types instead of the string representation for production code?

Copy link
Member

Choose a reason for hiding this comment

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

Either is recommended in the docs. Strings just have some parsing overhead looking at the code, but probably just fine to use the string representation if it makes typing here easier.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think we could evolve the stubs as needed so will leave as-is for now.

stubs/numba.pyi Outdated
# TODO: If a callable is provided it will be called with the call expression
# node that is requesting inlining, the caller's IR and callee's IR as
# arguments, it is expected to return Truthy as to whether to inline.
target: Literal["cpu", "gpu", "npyufunc", "cuda"] = ..., # deprecated
Copy link
Member Author

Choose a reason for hiding this comment

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

we could leave this out, we don't currently use this and since it is deprecated may want to not do so.

Copy link
Member

Choose a reason for hiding this comment

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

Generally we only use signature_or_function, nopython, nogil, parallel if that would simplify the typing. looplift may be utilized in the future.

Copy link
Member Author

Choose a reason for hiding this comment

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

There's an open issue on numba tracker numba/numba#7424

The open PR to fix only deals with the decorator preserving types

I will also be using this stub in my numba branch and adding additional types as needed. Will look at the types for the numba types next if this PR is accepted.

It maybe that being more complete helps the community to share effort until official numba stubs are available.

stubs/numba.pyi Outdated
| list[numba.core.types.abstract.Type] = ...,
locals: dict = ..., # TODO: Mapping of local variable names to Numba types
cache: bool = ...,
pipeline_class: numba.compiler.CompilerBase = ...,
Copy link
Member Author

Choose a reason for hiding this comment

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

this will resolve to Any

stubs/numba.pyi Outdated
def jit(
signature_or_function: str
| list[str]
| numba.core.types.abstract.Type
Copy link
Member Author

Choose a reason for hiding this comment

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

This is added for completeness but will resolve to Any.

pyproject.toml Outdated
@@ -60,6 +60,7 @@ markers = [

[tool.mypy]
# Import discovery
mypy_path = "stubs"
Copy link
Member

Choose a reason for hiding this comment

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

microsoft/pyright#2514 (comment)

stubPath = "stubs" for pyright or rename the folder to typings.

Copy link
Member

Choose a reason for hiding this comment

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

Probably also need to append "stubs" to include to make sure that pyright also checks stubs itself for any errors/suggestions.

Copy link
Member Author

Choose a reason for hiding this comment

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

done for mypy. what do I need to change for pyright?

Copy link
Member

@twoertwein twoertwein Oct 30, 2021

Choose a reason for hiding this comment

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

I think the following should work

include = ["pandas", "stubs"]
stubPath = "stubs"

You can probably also remove the pyright file-based ignore comments: pandas/core/window/numba_.py and pandas/core/util/numba_.py

Copy link
Member

Choose a reason for hiding this comment

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

If it is setup correctly, pyright will complain about the incomplete __getattr__ (add a file-based ignore comment).

@simonjayhawkins simonjayhawkins marked this pull request as ready for review October 30, 2021 18:05
@twoertwein
Copy link
Member

twoertwein commented Oct 30, 2021

There are a few more cases of numba.jit that have ignored comments.

pandas/core/window/online.py: @numba.jit(nopython=nopython, nogil=nogil, parallel=parallel) # type: ignore[misc]
pandas/core/window/numba_.py: @numba.jit(nopython=nopython, nogil=nogil, parallel=parallel) # type: ignore[misc]
pandas/core/window/numba_.py: @numba.jit(nopython=nopython, nogil=nogil, parallel=parallel) # type: ignore[misc]
pandas/core/window/numba_.py: @numba.jit(nopython=nopython, nogil=nogil, parallel=parallel) # type: ignore[misc]
pandas/core/window/numba_.py: @numba.jit(nopython=nopython, nogil=nogil, parallel=parallel) # type: ignore[misc]
pandas/core/numba/executor.py: @numba.jit(nopython=nopython, nogil=nogil, parallel=parallel) # type: ignore[misc]
pandas/core/groupby/numba
.py: @numba.jit(nopython=nopython, nogil=nogil, parallel=parallel) # type: ignore[misc]
pandas/core/groupby/numba_.py: @numba.jit(nopython=nopython, nogil=nogil, parallel=parallel) # type: ignore[misc]

edit: because of import_optional_dependency

@jreback jreback added this to the 1.4 milestone Oct 30, 2021
@jreback
Copy link
Contributor

jreback commented Oct 30, 2021

@twoertwein merge when ready here. can certainly be followups to fix more stuff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
numba numba-accelerated operations Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants