Skip to content

TYP: frompyfunc shape-typing and more accurate dtypes - #32753

Merged
charris merged 4 commits into
numpy:mainfrom
jorenham:typing/frompyfunc/shape-typing
Sep 22, 2026
Merged

charris merged 4 commits into
numpy:mainfrom
jorenham:typing/frompyfunc/shape-typing

Conversation

@jorenham

@jorenham jorenham commented Sep 22, 2026 •

Copy link
Copy Markdown
Member

The ufunc returned by np.frompyfunc now returns shape-typed arrays (or scalars) for (broadcasted combinations of) <=2 array-like input with more accurate object_ dtypes (the function's return type is now used as generic object type).

def double(x: float) -> float: ...
def add(a: int, b: int) -> int: ...

u = np.frompyfunc(double, 1, 1)
v = np.frompyfunc(add, 2, 1)

reveal_type(u(np.ones((2, 3))))
# before: float | NDArray[object_[Any]]
# after:  ndarray[tuple[int, int], dtype[object_[float]]]

reveal_type(v(np.arange(3), 1))
# before: int | NDArray[object_[Any]]
# after:  ndarray[tuple[int], dtype[object_[int]]]

reveal_type(v.reduce([1, 2, 3]))
# before: int | NDArray[object_[Any]]
# after:  int

Pair programmed with AI.

@jorenham jorenham added this to the 2.6.0 Release milestone Sep 22, 2026
@jorenham

Copy link
Copy Markdown
Member Author

This is the last one from the main numpy. namespace, BTW :)

@charris
charris merged commit 4a463e6 into numpy:main Sep 22, 2026
16 checks passed
@charris

charris commented Sep 22, 2026

Copy link
Copy Markdown
Member

Thanks Joren.

@jorenham
jorenham deleted the typing/frompyfunc/shape-typing branch September 22, 2026 18:10
Riaz1729 pushed a commit to Riaz1729/numpy that referenced this pull request Sep 23, 2026
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.

2 participants