question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`Quantity.__ilshift__` throws exception with `dtype=int`

See original GitHub issue

Description

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:closed
  • Created 2 years ago
  • Comments:11 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
nstarmancommented, Mar 20, 2022

I changed the issue name to reflect the source of the error.

1reaction
mhvkcommented, Sep 12, 2022

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 as float64).

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 returns NotImplemented, and then one gets b.__rlshift(a) instead. Indeed, this is how array <<= unit is able to return a quantity.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found