Skip to content

Post-numpy-2.0 changes in repr/str #15793

Open

Description

@mhvk

What is the problem this feature will solve?

Numpy 2.0 is becoming much more explicit with how scalars are represented. In most cases, we can just adjust our docs - see #15065 - but some become decidedly ugly, and we should change our default __str__ and __repr__. Specific cases:

  1. StructuredUnit. Some of its properties such as .physical_type return a np.void structure. This used to be represented as
((PhysicalType('length'), PhysicalType({'speed', 'velocity'})), PhysicalType('time'))

but with numpy 2.0, this becomes

astropy.units.structured.Structure((astropy.units.structured.Structure((PhysicalType('length'), PhysicalType({'speed', 'velocity'})), dtype=[('f0', 'O'), ('f1', 'O')]), PhysicalType('time')), dtype=[('f0', 'O'), ('f1', 'O')])

Which is not very useful. @nstarman's suggestion in #15065 (comment) is to make __str__ look like the old repr and create a new __repr__ that at the very least corrects the name to astropy.units.Structure. Since for those Structure, the dtype is always object, that information can be omitted as well, in which case it would become,

astropy.units.Structure((astropy.units.Structure((PhysicalType('length'), PhysicalType({'speed', 'velocity'})), PhysicalType('time')))

though perhaps this goes a bit too far, since now we have lost the names as well.

Describe the desired outcome

We have clean and informative __str__ and __repr__, the latter ideally sufficient to reproduce the item in question.

Additional context

No response

Activity

  1. pllim commented on Jan 3, 2024

    @pllim
    Member

    This sounds like a lot of work if we are going to also cover things like io.fits and wcs. What is the scope of this issue?

  2. mhvk commented on Jan 4, 2024

    @mhvk
    ContributorAuthor

    I think in-scope is only representations of our own classes which have changed due to the numpy 2.0 changes. I think this is mostly subclasses of np.void...

  3. pllim commented on Jan 4, 2024

    @pllim
    Member

    I remember seeing WCS print being ugly while I had numpy 2.0.dev installed but I am not sure if I want to fix it lol.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions