Skip to content

Quantity.__class_getitem__ cannot use super on ndarray.__class_getitem__ on python >=3.10 #12510

Closed

Description

@mhvk

All PRs are currently failing their python 3.10 + devdeps tests because the interaction between Quantity.__class_getitem__ and ndarray.__class_getitem__ is not correct. E.g.,

In [2]: u.Quantity[u.m]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-f0c088963c1a> in <module>
----> 1 u.Quantity[u.m]

~/data/mhvk/packages/astropy/astropy/units/quantity.py in __class_getitem__(cls, unit_shape_dtype)
    404         # metadata using Annotated.
    405         if not NUMPY_LT_1_22:
--> 406             cls = super().__class_getitem__((cls, *shape_dtype))
    407         return Annotated.__class_getitem__((cls, unit))
    408 

TypeError: Too few arguments for Quantity

It seems that ndarray.__class_getitem__ cannot be called without an actual (shape, dtype). Those also should be meaningful, meaning that the current test u.Quantity[u.m, "more"] would seem to incorrectly associate this "more" with the ndarray aspect.

In [13]: u.Quantity[u.m, "more"]
Out[13]: typing.Annotated[astropy.units.quantity.Quantity[astropy.units.quantity.Quantity, 'more'], Unit("m")]

My sense is to remove the super() call... @nstarman??

Activity

  1. mhvk commented on Nov 21, 2021

    @mhvk
    ContributorAuthor

    See #12511 for a PR that implements my suggestion of just not using NDArray.__class_getitem__ for now.

  2. nstarman commented on Nov 22, 2021

    @nstarman
    Member

    Sounds good to me. We can re-add it with some tests on python 3.10. and numpy 1.22+.
    I also think the u.Quantity[u.m, "more"] should be changed to fail regardless. As in we should always have the arguments be (unit, shape, dtype) regardless of whether the latter two are functional.

  3. self-assigned this
    on Nov 25, 2021
  4. pllim commented on Dec 6, 2022

    @pllim
    Member

    Is this resolved?

  5. mhvk commented on Dec 6, 2022

    @mhvk
    ContributorAuthor

    I thought the fix I put in was meant to only be temporary. Should Quantity.__class_getitem__ not be compatible with numpy.__class_getitem__?

  6. pllim commented on Dec 6, 2022

    @pllim
    Member

    Ah, okay. Maybe the title needs to be updated. "numpy dev" as of Nov 2021 would have been stable release by now.

  7. changed the title [-]Tests failing with numpy-dev due to Typing[/-] [+]Quantity.__class_getitem__ cannot use super on ndarray.__class_getitem__ on python >=3.10[/+] on Dec 6, 2022
  8. mhvk commented on Dec 6, 2022

    @mhvk
    ContributorAuthor

    Good point. I changed the title.

  9. mhvk commented on Dec 6, 2022

    @mhvk
    ContributorAuthor

    Also removed numpy-dev, but perhaps this is a case for upstream-fix-required? @nstarman?

  10. mhvk commented on Apr 7, 2023

    @mhvk
    ContributorAuthor

    @nstarman - seeing your typing updates in #14286, just a reminder that perhaps this issue can be closed with the subsequent update to the minimum version of numpy (even if we just add something that only works for not NUMPY_LT_1_22).

  11. neutrinoceros commented on Feb 21, 2024

    @neutrinoceros
    Contributor

    @mhvk that would be #15706 ?
    so, should we just close this one ?

  12. added
    Close?Tell stale bot that this issue/PR is stale
    on Feb 21, 2024
  13. mhvk commented on Feb 21, 2024

    @mhvk
    ContributorAuthor

    Let's ping @nstarman about this - right now, we are not calling ndarray.__class_getitem__ ever, which means we cannot indicate a shape or dtype in the typing. Should we change that?

  14. github-actions commented on Feb 22, 2024

    @github-actions
    Contributor

    Hi humans 👋 - this issue was labeled as Close? approximately 15 hours ago. If you think this issue should not be closed, a maintainer should remove the Close? label - otherwise, I will close this issue in 7 days.

    If you believe I commented on this issue incorrectly, please report this here

  15. nstarman commented on Feb 22, 2024

    @nstarman
    Member

    Yes. This kept getting pushed from my attention because I think I made a mistake in setting up the type hint. IMO what we should do (explained https://github.com/nstarman/astropy-APEs/blob/units-quantity-2.0/APE25/report.pdf) is make it Quantity[ValueType, UnitStuff], so one would do Quantity[NDArray[float], "length"].
    For backwards compatibility we should override __class_getitem__ and implement a QuantityU (or similarly named) GenericAlias that is parametrized only by the unit stuff — akin to how numpy.typing.NDArray[dtype] drops the shape stuff from numpy.ndarray[shape, dtype].

  16. mhvk commented on Feb 23, 2024

    @mhvk
    ContributorAuthor

    That all makes sense. So, just to be sure, one would still be able to do Quantity["length"], right? I.e., no import of QuantityU needed -- that would just be used internally?

  17. github-actions commented on Feb 29, 2024

    @github-actions
    Contributor

    I'm going to close this issue as per my previous message, but if you feel that this issue should stay open, then feel free to re-open and remove the Close? label.

    If this is the first time I am commenting on this issue, or if you believe I closed this issue incorrectly, please report this here

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

Metadata

Metadata

Assignees

Labels

BugClose?Tell stale bot that this issue/PR is staleclosed-by-botClosed by stale botunits

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions