8000 BUG: Revert `np.vectorize` casting to legacy behavior by francois-rozet · Pull Request #29196 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Revert np.vectorize casting to legacy behavior #29196

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
Jun 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion numpy/lib/_function_base_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2573,6 +2573,7 @@ def _get_ufunc_and_otypes(self, func, args):
# the subsequent call when the ufunc is evaluated.
# Assumes that ufunc first evaluates the 0th elements in the input
# arrays (the input values are not checked to ensure this)
args = [asarray(a) for a in args]
if builtins.any(arg.size == 0 for arg in args):
raise ValueError('cannot call `vectorize` on size 0 inputs '
'unless `otypes` is set')
Expand Down Expand Up @@ -2618,8 +2619,9 @@ def _vectorize_call(self, func, args):
elif not args:
res = func()
else:
args = [asanyarray(a, dtype=object) for a in args]
ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args)
# gh-29196: `dtype=object` should eventually be removed
args = [asanyarray(a, dtype=object) for a in args]
outputs = ufunc(*args, out=...)

if ufunc.nout == 1:
Expand Down
9 changes: 9 additions & 0 deletions numpy/lib/tests/test_function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1732,6 +1732,15 @@ def test_string_ticket_1892(self):
s = '0123456789' * 10
assert_equal(s, f(s))

def test_dtype_promotion_gh_29189(self):
# dtype should not be silently promoted (int32 -> int64)
dtypes = [np.int16, np.int32, np.int64, np.float16, np.float32, np.float64]

for dtype in dtypes:
x = np.asarray([1, 2, 3], dtype=dtype)
y = np.vectorize(lambda x: x + x)(x)
assert x.dtype == y.dtype

def test_cache(self):
# Ensure that vectorized func called exactly once per argument.
_calls = [0]
Expand Down
Loading
0