Skip to content
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

Make u.Magnitude hashable #12979

Open
olebole opened this issue Mar 23, 2022 · 3 comments
Open

Make u.Magnitude hashable #12979

olebole opened this issue Mar 23, 2022 · 3 comments

Comments

@olebole
Copy link
Member

olebole commented Mar 23, 2022

Description

Currently, u.Magnitude is not hashable, different from u.mag:

>>> import astropy.units as u
>>> mags = u.Magnitude([1., 2., 3.])
>>> mags
<Magnitude [1., 2., 3.] mag>
>>> mags.physical
<Quantity [0.39810717, 0.15848932, 0.06309573]>
>>> set(mags)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'Magnitude'

It would be useful to have magnitudes hashable, to allow them f.e. as a parameter of a function decorated with @functools.cache.
The workaround of using u.mag here is ugly, since it lacks the physical property:

>>> import astropy.units as u
>>> mags = [1., 2., 3.]*u.mag
>>> mags
<Quantity [1., 2., 3.] mag>
>>> set(mags)
{<Quantity 1. mag>, <Quantity 2. mag>, <Quantity 3. mag>}
>>> mags.physical
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'RegularFunctionUnit' object has no attribute 'physical'

Specifically I have

    @functools.cache
    def foo(self, mag, …):
        return* mag.physical
@nstarman
Copy link
Member

I think this is NumPy's fault. I don't think Quantity is hashable, because ndarray is not... ?

@nstarman
Copy link
Member

@mhvk, correct me if I'm wrong.

@olebole
Copy link
Member Author

olebole commented Mar 23, 2022

As in my example, a (non-array) Quantity is hashable. It works with u.Quantity, but not with u.Magnitude.

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

No branches or pull requests

3 participants