8000 TYP: improve ``np.sum`` and ``np.mean`` return types with given ``dtype`` · Issue #27638 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

TYP: improve np.sum and np.mean return types with given dtype #27638

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
bersbersbers opened this issue Oct 24, 2024 · 2 comments · Fixed by #27683
Closed

TYP: improve np.sum and np.mean return types with given dtype #27638

bersbersbers opened this issue Oct 24, 2024 · 2 comments · Fixed by #27683

Comments

@bersbersbers
Copy link
Contributor
bersbersbers commented Oct 24, 2024

Describe the issue:

In some cases, sum and mean return Any according to the typing information. I propose the following improvements:

Reproduce the code example:

diff --git a/numpy/_core/fromnumeric.pyi b/numpy/_core/fromnumeric.pyi
index 32f8a06f7b..185b9472ec 100644
--- a/numpy/_core/fromnumeric.pyi
+++ b/numpy/_core/fromnumeric.pyi
@@ -597,15 +597,25 @@ def sum(
     where: _ArrayLikeBool_co = ...,
 ) -> _SCT: ...
 @overload
+def sum(
+    a: _ArrayLike,
+    axis: None = ...,
+    dtype: _DTypeLike[_SCT] = ...,
+    out: None = ...,
+    keepdims: bool = ...,
+    initial: _NumberLike_co = ...,
+    where: _ArrayLikeBool_co = ...,
+) -> _SCT: ...
+@overload
 def sum(
     a: ArrayLike,
     axis: None | _ShapeLike = ...,
-    dtype: DTypeLike = ...,
+    dtype: _DTypeLike[_SCT] = ...,
     out: None  = ...,
     keepdims: bool = ...,
     initial: _NumberLike_co = ...,
     where: _ArrayLikeBool_co = ...,
-) -> Any: ...
+) -> _SCT | NDArray[_SCT]: ...
 @overload
 def sum(
     a: ArrayLike,
@@ -1210,12 +1220,12 @@ def mean(
 def mean(
     a: _ArrayLikeComplex_co | _ArrayLikeObject_co,
     axis: None | _ShapeLike = ...,
-    dtype: DTypeLike = ...,
+    dtype: _DTypeLike[_SCT] = ...,
     out: None = ...,
     keepdims: bool = ...,
     *,
     where: _ArrayLikeBool_co = ...,
-) -> Any: ...
+) -> _SCT | NDArray[_SCT]: ...
 @overload
 def mean(
     a: _ArrayLikeComplex_co | _ArrayLikeObject_co,

Error message:

No response

Python and NumPy Versions:

2.1.2
3.13.0 (main, Oct 16 2024, 00:33:24) [MSC v.1929 64 bit (AMD64)]

Type-checker version and settings:

mypy

Additional typing packages.

No response

@bersbersbers bersbersbers changed the title TYP: improve np.sums and np.mean's return types with given dtype TYP: improve np.sum and np.mean's return types with given dtype Oct 24, 2024
@bersbersbers
Copy link
Contributor Author

Related: #21907, #19252

@jorenham
Copy link
Member

Thanks for reporting this! Would you mind submitting this as a PR?

One quick tip before you do; _DTypeLike[_SCT] does not cover everything that DTypeLike does. So to be complete about this, an additional @overload will be needed.

@charris charris changed the title TYP: improve np.sum and np.mean's return types with given dtype TYP: improve np.sum and np.mean return types with given dtype Nov 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
@jorenham 2C51 @bersbersbers
0