From 37bf1b8dcf93ef15168ce9347185c55489b05cb4 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Fri, 29 Oct 2021 20:39:41 +0000 Subject: [PATCH 1/4] TYP: numba stub --- pandas/core/_numba/kernels/shared.py | 8 +++++- pyproject.toml | 1 + stubs/numba.pyi | 41 ++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 stubs/numba.pyi diff --git a/pandas/core/_numba/kernels/shared.py b/pandas/core/_numba/kernels/shared.py index d84e409ca879d..ec25e78a8d897 100644 --- a/pandas/core/_numba/kernels/shared.py +++ b/pandas/core/_numba/kernels/shared.py @@ -2,7 +2,13 @@ import numpy as np -@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] + nopython=True, + nogil=True, + parallel=False, +) def is_monotonic_increasing(bounds: np.ndarray) -> bool: """Check if int64 values are monotonically increasing.""" n = len(bounds) diff --git a/pyproject.toml b/pyproject.toml index ae68e54ce1346..f77d93865b96a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -60,6 +60,7 @@ markers = [ [tool.mypy] # Import discovery +mypy_path = "stubs" namespace_packages = false explicit_package_bases = false ignore_missing_imports = true diff --git a/stubs/numba.pyi b/stubs/numba.pyi new file mode 100644 index 0000000000000..d6a2729d36db3 --- /dev/null +++ b/stubs/numba.pyi @@ -0,0 +1,41 @@ +from typing import ( + Any, + Callable, + Literal, + overload, +) + +import numba + +from pandas._typing import F + +def __getattr__(name: str) -> Any: ... # incomplete +@overload +def jit( + signature_or_function: F = ..., +) -> F: ... +@overload +def jit( + signature_or_function: str + | list[str] + | numba.core.types.abstract.Type + | list[numba.core.types.abstract.Type] = ..., + locals: dict = ..., # TODO: Mapping of local variable names to Numba types + cache: bool = ..., + pipeline_class: numba.compiler.CompilerBase = ..., + boundscheck: bool | None = ..., + *, + nopython: bool = ..., + forceobj: bool = ..., + looplift: bool = ..., + error_model: Literal["python", "numpy"] = ..., + inline: Literal["never", "always"] | Callable = ..., + # 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 + nogil: bool = ..., + parallel: bool = ..., +) -> Callable[[F], F]: ... + +njit = jit From 72d666b8c50207db54f631e969b8a6e111e61629 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sat, 30 Oct 2021 17:00:28 +0000 Subject: [PATCH 2/4] remove unused "type: ignore" comments --- pandas/core/_numba/kernels/mean_.py | 9 +++------ pandas/core/_numba/kernels/sum_.py | 9 +++------ 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/pandas/core/_numba/kernels/mean_.py b/pandas/core/_numba/kernels/mean_.py index f3a2c7c2170a8..8f67dd9b51c06 100644 --- a/pandas/core/_numba/kernels/mean_.py +++ b/pandas/core/_numba/kernels/mean_.py @@ -14,8 +14,7 @@ from pandas.core._numba.kernels.shared import is_monotonic_increasing -# error: Untyped decorator makes function "add_mean" untyped -@numba.jit(nopython=True, nogil=True, parallel=False) # type: ignore[misc] +@numba.jit(nopython=True, nogil=True, parallel=False) def add_mean( val: float, nobs: int, sum_x: float, neg_ct: int, compensation: float ) -> tuple[int, float, int, float]: @@ -30,8 +29,7 @@ def add_mean( return nobs, sum_x, neg_ct, compensation -# error: Untyped decorator makes function "remove_mean" untyped -@numba.jit(nopython=True, nogil=True, parallel=False) # type: ignore[misc] +@numba.jit(nopython=True, nogil=True, parallel=False) def remove_mean( val: float, nobs: int, sum_x: float, neg_ct: int, compensation: float ) -> tuple[int, float, int, float]: @@ -46,8 +44,7 @@ def remove_mean( return nobs, sum_x, neg_ct, compensation -# error: Untyped decorator makes function "sliding_mean" untyped -@numba.jit(nopython=True, nogil=True, parallel=False) # type: ignore[misc] +@numba.jit(nopython=True, nogil=True, parallel=False) def sliding_mean( values: np.ndarray, start: np.ndarray, diff --git a/pandas/core/_numba/kernels/sum_.py b/pandas/core/_numba/kernels/sum_.py index 66a1587c49f3f..c2e81b4990ba9 100644 --- a/pandas/core/_numba/kernels/sum_.py +++ b/pandas/core/_numba/kernels/sum_.py @@ -14,8 +14,7 @@ from pandas.core._numba.kernels.shared import is_monotonic_increasing -# error: Untyped decorator makes function "add_sum" untyped -@numba.jit(nopython=True, nogil=True, parallel=False) # type: ignore[misc] +@numba.jit(nopython=True, nogil=True, parallel=False) def add_sum( val: float, nobs: int, sum_x: float, compensation: float ) -> tuple[int, float, float]: @@ -28,8 +27,7 @@ def add_sum( return nobs, sum_x, compensation -# error: Untyped decorator makes function "remove_sum" untyped -@numba.jit(nopython=True, nogil=True, parallel=False) # type: ignore[misc] +@numba.jit(nopython=True, nogil=True, parallel=False) def remove_sum( val: float, nobs: int, sum_x: float, compensation: float ) -> tuple[int, float, float]: @@ -42,8 +40,7 @@ def remove_sum( return nobs, sum_x, compensation -# error: Untyped decorator makes function "sliding_sum" untyped -@numba.jit(nopython=True, nogil=True, parallel=False) # type: ignore[misc] +@numba.jit(nopython=True, nogil=True, parallel=False) def sliding_sum( values: np.ndarray, start: np.ndarray, From 3ba597534e4a7bd991dd8b234e2f827faf6bcfe3 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sat, 30 Oct 2021 17:38:14 +0000 Subject: [PATCH 3/4] rename directory stubs -> typings --- pyproject.toml | 2 +- {stubs => typings}/numba.pyi | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename {stubs => typings}/numba.pyi (100%) diff --git a/pyproject.toml b/pyproject.toml index 118872f086b50..6187215323c7c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -60,7 +60,7 @@ markers = [ [tool.mypy] # Import discovery -mypy_path = "stubs" +mypy_path = "typings" namespace_packages = false explicit_package_bases = false ignore_missing_imports = true diff --git a/stubs/numba.pyi b/typings/numba.pyi similarity index 100% rename from stubs/numba.pyi rename to typings/numba.pyi From a3a42ea723517f13a36a253bafdc2dafa14106e2 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sat, 30 Oct 2021 17:55:05 +0000 Subject: [PATCH 4/4] include stubs in type checks for mypy --- ci/code_checks.sh | 2 +- doc/source/development/contributing_codebase.rst | 2 +- pyproject.toml | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/ci/code_checks.sh b/ci/code_checks.sh index 21250789fde9f..ea9595fd88630 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -106,7 +106,7 @@ if [[ -z "$CHECK" || "$CHECK" == "typing" ]]; then mypy --version MSG='Performing static analysis using mypy' ; echo $MSG - mypy pandas + mypy RET=$(($RET + $?)) ; echo $MSG "DONE" # run pyright, if it is installed diff --git a/doc/source/development/contributing_codebase.rst b/doc/source/development/contributing_codebase.rst index 936db7d1bee88..4cea030546635 100644 --- a/doc/source/development/contributing_codebase.rst +++ b/doc/source/development/contributing_codebase.rst @@ -399,7 +399,7 @@ pandas uses `mypy `_ and `pyright