Skip to content

Handle Python scalars consistently in np.astype#31365

Closed
mncrftfrcnm wants to merge 5 commits into
numpy:mainfrom
mncrftfrcnm:main
Closed

Handle Python scalars consistently in np.astype#31365
mncrftfrcnm wants to merge 5 commits into
numpy:mainfrom
mncrftfrcnm:main

Conversation

@mncrftfrcnm
Copy link
Copy Markdown

PR summary

First time committer introduction

I use numpy as a machine learner, and data-analytic. I also use it for a lot of other tasks, but primarily for ML.
I was interested in reviewing the code of numpy, and how it works, and i saw a bug in astype.

So, the issue i found was in the astype function. This PR update 'np.astype':, so python scalar inputs are also handled, instead of ending with error 'AttributeError'

import numpy as np
np.astype(np.int64(1), np.float64)
np.float64(1.0)
np.astype(1, np.float64)
Traceback (most recent call last):
File "", line 1, in
File "C:\Users\vov\anaconda3\Lib\site-packages\numpy_core\numeric.py", line 2730, in astype
return x.astype(dtype, copy=copy)
^^^^^^^^
AttributeError: 'int' object has no attribute 'astype'

So, as python integers do not have a .astype attribute, in my edits, if the integer is scalar, it is converted by the np.asarray , which does not raise an issue, and makes the python-native scalars work fine

AI Disclosure

No AI tools used

Comment thread numpy/_core/numeric.py Outdated
Comment thread numpy/_core/numeric.py Outdated
Co-authored-by: Joren Hammudoglu <jhammudoglu@gmail.com>
@seberg
Copy link
Copy Markdown
Member

seberg commented Apr 30, 2026

Not sure we should do this, since this function really exists only for the Array API, I would say, so I am not sure there is a reason to broaden it up beyond such use.

@mncrftfrcnm
Copy link
Copy Markdown
Author

Not sure we should do this, since this function really exists only for the Array API, I would say, so I am not sure there is a reason to broaden it up beyond such use.

but still, it may be an issue?

Co-authored-by: Joren Hammudoglu <jhammudoglu@gmail.com>
@seberg
Copy link
Copy Markdown
Member

seberg commented May 1, 2026

To me it isn't an issue until someone asks from it from a usability perspective outside of the original context. But if other maintainers prefer to do coerction to array here because NumPy usually does, sure...

@mncrftfrcnm
Copy link
Copy Markdown
Author

mncrftfrcnm commented May 1, 2026

To me it isn't an issue until someone asks from it from a usability perspective outside of the original context. But if other maintainers prefer to do coerction to array here because NumPy usually does, sure...

That makes sense, and i also don't think anyone would often use this outside of what it is intended for.
How about returning a typeError(like for the rest issues) if the python int is passed, instead of attributeError?

(raise TypeError(
"Input should be a NumPy array or scalar. "
f"It is a {type(x)} instead."
))

@jorenham
Copy link
Copy Markdown
Member

jorenham commented May 1, 2026

To me it isn't an issue until someone asks from it from a usability perspective outside of the original context.

I agree. In the stubs it's even stricter currently, because it only accepts ndarray, so no scalars at all:

numpy/numpy/_core/numeric.pyi

Lines 1349 to 1366 in 5cba3b4

@overload
def astype[ShapeT: _Shape, ScalarT: np.generic](
x: ndarray[ShapeT],
dtype: _DTypeLike[ScalarT],
/,
*,
copy: py_bool = True,
device: L["cpu"] | None = None,
) -> ndarray[ShapeT, dtype[ScalarT]]: ...
@overload
def astype[ShapeT: _Shape](
x: ndarray[ShapeT],
dtype: DTypeLike | None,
/,
*,
copy: py_bool = True,
device: L["cpu"] | None = None,
) -> ndarray[ShapeT]: ...

How about returning a typeError(like for the rest issues) if the python int is passed, instead of attributeError?

Sounds good to me

@mncrftfrcnm
Copy link
Copy Markdown
Author

To me it isn't an issue until someone asks from it from a usability perspective outside of the original context.

I agree. In the stubs it's even stricter currently, because it only accepts ndarray, so no scalars at all:

numpy/numpy/_core/numeric.pyi

Lines 1349 to 1366 in 5cba3b4

@overload
def astype[ShapeT: _Shape, ScalarT: np.generic](
x: ndarray[ShapeT],
dtype: _DTypeLike[ScalarT],
/,
*,
copy: py_bool = True,
device: L["cpu"] | None = None,
) -> ndarray[ShapeT, dtype[ScalarT]]: ...
@overload
def astype[ShapeT: _Shape](
x: ndarray[ShapeT],
dtype: DTypeLike | None,
/,
*,
copy: py_bool = True,
device: L["cpu"] | None = None,
) -> ndarray[ShapeT]: ...

How about returning a typeError(like for the rest issues) if the python int is passed, instead of attributeError?

Sounds good to me

So, should I create a new branch/PR, where the code will return attribute error for python-native numbers, or should I make changes on this one?

@jorenham
Copy link
Copy Markdown
Member

jorenham commented May 1, 2026

So, should I create a new branch/PR, where the code will return attribute error for python-native numbers, or should I make changes on this one?

Whatever you prefer

@mncrftfrcnm
Copy link
Copy Markdown
Author

ok, i will create a new branch with PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants