`Quantity.__ilshift__` throws exception with `dtype=int`
See original GitHub issueDescription
The astropy.units.quantity_input
decorator throws a UFuncTypeError
when used on a function that returns a Quantity
with dtype=int
and a return type annotation.
Expected behavior
For the function to return a Quantity
with dtype=int
with the appropriate units or to throw an exception if the output units are of the wrong type.
Actual behavior
Using the decorator results in a UFuncTypeError
Steps to Reproduce
import astropy.units as u
@u.quantity_input
def foo()->u.pix: return u.Quantity(1, 'pix', dtype=int)
foo()
gives
---------------------------------------------------------------------------
UFuncTypeError Traceback (most recent call last)
Input In [26], in <cell line: 1>()
----> 1 foofoo()
File ~/anaconda/envs/aiapy-dev/lib/python3.9/site-packages/astropy/units/decorators.py:320, in QuantityInput.__call__.<locals>.wrapper(*func_args, **func_kwargs)
316 _validate_arg_value("return", wrapped_function.__name__,
317 return_, valid_targets, self.equivalencies,
318 self.strict_dimensionless)
319 if len(valid_targets) > 0:
--> 320 return_ <<= valid_targets[0]
321 return return_
File ~/anaconda/envs/aiapy-dev/lib/python3.9/site-packages/astropy/units/quantity.py:1087, in Quantity.__ilshift__(self, other)
1084 self.view(np.ndarray)[...] = value
1086 else:
-> 1087 self.view(np.ndarray)[...] *= factor
1089 self._set_unit(other)
1090 return self
UFuncTypeError: Cannot cast ufunc 'multiply' output from dtype('float64') to dtype('int64') with casting rule 'same_kind'
System Details
macOS-10.16-x86_64-i386-64bit
Python 3.9.7 (default, Sep 16 2021, 08:50:36)
[Clang 10.0.0 ]
Numpy 1.22.3
pyerfa 2.0.0.1
astropy 5.0.2
Scipy 1.8.0
Matplotlib 3.5.1
Issue Analytics
- State:
- Created 2 years ago
- Comments:11 (9 by maintainers)
Top Results From Across the Web
How to throw an exception - Python Morsels
The sqrt function in Python's math module raises an exception when it's given a negative number. Sometimes exceptions are more appropriate ...
Read more >Release 1 A. Ratnani - Pyccel's documentation!
The Python interpreter has a number of functions built into it ... Pyccel uses the parse library to retrieve error messages from Pylint....
Read more >NumPy Reference
dtype=int ) # offset = 1*itemsize, i.e. skip first element ... __nonzero__, which raises an error if the number of ... __ilshift__().
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I changed the issue name to reflect the source of the error.
Yes, numpy cannot change in-place since also the number of bytes is not quaranteed to be the same (
int32
can only be represented safely asfloat64
).On second thought about the whole issue, though, I think it may make more sense to give up the guarantee of shared memory. In the end, what the user wants is quite clear. And in a lot of python, if
a <<= b
does not work, it returnsNotImplemented
, and then one getsb.__rlshift(a)
instead. Indeed, this is howarray <<= unit
is able to return a quantity.