Skip to content

MAINT: Change the Scalar check in astype to only accept np objects#31369

Open
mncrftfrcnm wants to merge 3 commits into
numpy:mainfrom
mncrftfrcnm:main
Open

MAINT: Change the Scalar check in astype to only accept np objects#31369
mncrftfrcnm wants to merge 3 commits into
numpy:mainfrom
mncrftfrcnm:main

Conversation

@mncrftfrcnm
Copy link
Copy Markdown

PR summary

So, the issue i found was in the astype function. This PR updates 'np.astype':, so python scalar inputs return attribute error, instead of ending with error 'AttributeError', which is not entirely correct

here is what the fixed version looks like, instead of attribute error
(error message edited a bit, so only the final error message is shown, omiting all other details, like: 'Traceback (most recent call last):
File "", line 1, in ')

>>> import numpy

>>> print(numpy.astype(1, numpy.float64))
TypeError: Input should be a NumPy array or a NumPy scalar. It is a <class 'int'> instead.

>>> print(numpy.astype(1.0, numpy.float64))
TypeError: Input should be a NumPy array or a NumPy scalar. It is a <class 'float'> instead.

>>> print(numpy.astype(True, numpy.float64))
TypeError: Input should be a NumPy array or a NumPy  scalar. It is a <class 'bool'> instead.

>>> print(numpy.astype(numpy.int64(1), numpy.float64))
1.0

>>> print(numpy.astype(numpy.float64(1.0), numpy.float64))
1.0
>>> print(numpy.astype(numpy.datetime64("2024-01-01"), numpy.datetime64))
2024-01-01
>>> print(numpy.astype(numpy.array([1, 2, 3]), numpy.float64))
[1. 2. 3.]

here is what original original function output looked like:

>>> import numpy as np
>>> np.astype(1, np.float64)
AttributeError: 'int' object has no attribute 'astype'

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.

AI Disclosure

No AI tools used

@ngoldbaum
Copy link
Copy Markdown
Member

I don’t think changing the astype implementation is correct, instead we should do a try/except (or the equivalent in C if the the getattr is happening in C) and raise the appropriate error at the site where it happens.

Generally it’s best to open issues about bugs you find before sending in a PR with a fix, particularly for projects you’re new to the internals of.

@mncrftfrcnm
Copy link
Copy Markdown
Author

I don’t think changing the astype implementation is correct, instead we should do a try/except (or the equivalent in C if the the getattr is happening in C) and raise the appropriate error at the site where it happens.

Generally it’s best to open issues about bugs you find before sending in a PR with a fix, particularly for projects you’re new to the internals of.

So, should i do smt like this:

    if not (isinstance(x, np.ndarray) or isscalar(x)):
        raise TypeError(
            "Input should be a NumPy array or scalar. "
            f"It is a {type(x)} instead."
        )
    if device is not None and device != "cpu":
        raise ValueError(
            'Device not understood. Only "cpu" is allowed, but received:'
            f' {device}'
        )
    try:
        return x.astype(dtype, copy=copy)
    except AttributeError:
        raise TypeError(
            "Input should be a NumPy array or scalar."
            f"It is a {type(x)} instead. Python scalars are not supported. "
        )

@mncrftfrcnm
Copy link
Copy Markdown
Author

I don’t think changing the astype implementation is correct, instead we should do a try/except (or the equivalent in C if the the getattr is happening in C) and raise the appropriate error at the site where it happens.

Generally it’s best to open issues about bugs you find before sending in a PR with a fix, particularly for projects you’re new to the internals of.

Also, if I did everything correctly, it will only omit the python native scalars, returning proper error, which is what the previous reviewer asked for in the other PR, where I implemented python-native scalar handling(here's link to that PR: #31365 )

@charris charris changed the title Change the Scalar check in astype so it accepts only np objects MAINT: Change the Scalar check in astype to only accept np objects May 2, 2026
@mncrftfrcnm
Copy link
Copy Markdown
Author

I don’t think changing the astype implementation is correct, instead we should do a try/except (or the equivalent in C if the the getattr is happening in C) and raise the appropriate error at the site where it happens.

Generally it’s best to open issues about bugs you find before sending in a PR with a fix, particularly for projects you’re new to the internals of.

ok, i've changed the way it handles attribute error to try_except

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.

3 participants