Skip to content

Stops using astropy's sanitize_slices - #995

Merged
Cadair merged 3 commits into
sunpy:mainfrom
nabobalis:sanitize-slices
Oct 22, 2026
Merged

Cadair merged 3 commits into
sunpy:mainfrom
nabobalis:sanitize-slices

Conversation

@nabobalis

@nabobalis nabobalis commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

PR Description

Closes #994.

astropy 8.0.2 reimplemented sanitize_slices with simplify_basic_index, which rejects a negative index when the shape is unknown, so cube[:, -3:-1] raises (#994); astropy 8.1 deprecates sanitize_slices.

AI Assistance Disclosure

AI tools were used for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding
  • No AI tools were used

Regardless of AI use, the human contributor remains fully responsible for correctness, design choices, licensing compatibility, and long-term maintainability.

https://claude.ai/code/session_01TutVtk4wvj3M6PHKJLBbKe

@nabobalis nabobalis changed the title Stop using astropy's deprecated sanitize_slices Stops using astropy's deprecated sanitize_slices Oct 19, 2026

@alasdairwilson alasdairwilson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why adopt the code rather than calling the replacement with a version check on astropy?

@nabobalis

Copy link
Copy Markdown
Member Author

simplify_basic_index requires a concrete size for every dimension, 8.0.2 is the first release to accept None entries and ndcube's minimum is 6.1. ExtraCoords._getitem_wcs and SkyCoordTableCoordinate.__getitem__ only pass a dimension count and a standalone ExtraCoords has no cube shape.

@alasdairwilson

Copy link
Copy Markdown
Member

simplify_basic_index requires a concrete size for every dimension, 8.0.2 is the first release to accept None entries and ndcube's minimum is 6.1. ExtraCoords._getitem_wcs and SkyCoordTableCoordinate.__getitem__ only pass a dimension count and a standalone ExtraCoords has no cube shape.

yeah meant swap between sanitise slices and simplifiy_basic_index for pre and post 8.0.2

@nabobalis

Copy link
Copy Markdown
Member Author

Oh, I didn't put two and two together. I will do that instead.

@nabobalis
nabobalis marked this pull request as draft October 19, 2026 16:23
@nabobalis nabobalis changed the title Stops using astropy's deprecated sanitize_slices Stops using astropy's sanitize_slices Oct 19, 2026
@nabobalis

Copy link
Copy Markdown
Member Author

I made it worse.

@alasdairwilson alasdairwilson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since regular cube slicing rejects non‑unit steps, we should add that same guard to index_as_cube. Using that, slice.indices() returns (start, stop, step), and we only take the start and end (with [:2]) and drop the step before the per‑cube slices reach the cube‑level check. That means index_as_cube[::2] or even ::-1 will silently return every row.

Could you apply the same check step with item[common_axis].step != 1 before before calling slice.indices (and add tests for non unitary steps on index_as_cube)

@alasdairwilson

alasdairwilson commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

Since regular cube slicing rejects non‑unit steps, we should add that same guard to index_as_cube. Using that, slice.indices() returns (start, stop, step), and we only take the start and end (with [:2]) and drop the step before the per‑cube slices reach the cube‑level check. That means index_as_cube[::2] or even ::-1 will silently return every row.

Could you apply the same check step with item[common_axis].step != 1 before before calling slice.indices (and add tests for non unitary steps on index_as_cube)

I guess in #979 rather than here?

Whatever you want.

@nabobalis

Copy link
Copy Markdown
Member Author

Well I want to make your life easier. Not mine.

@alasdairwilson

Copy link
Copy Markdown
Member

Well I want to make your life easier. Not mine.

do it in 979 then and rebase this ontop

@nabobalis
nabobalis force-pushed the sanitize-slices branch 3 times, most recently from 7e63481 to 50a5ed2 Compare October 20, 2026 21:04
@nabobalis

Copy link
Copy Markdown
Member Author

Should be done now.

@pllim

pllim commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

@astrofrog , should we roll back deprecating sanitize_slices upstream? 🤔

Comment thread ndcube/utils/misc.py Outdated


if minversion("astropy", "8.0.2"):
def sanitize_slices(slices, ndim):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe keep the compat layer private so you don't have to deprecate it later? 🤣

@pllim

pllim commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

We could also do what we did to clobber in FITS, by changing it to pending deprecation and have it pending for a long time. 😆

@astrofrog

Copy link
Copy Markdown

@nabobalis would it be helpful if we hold back from introducing the deprecation until 9.0?

@nabobalis

Copy link
Copy Markdown
Member Author

Nah, I don't see the point. Ndcube has to handle it regardless as it does all this crazy slicing. I wouldn't block any astropy changes based on this.

@Cadair Cadair added this to the 2.4.3 milestone Oct 22, 2026

@alasdairwilson alasdairwilson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name it _sanitize_slices, and leave it out of all

Vendors in the new simplify_basic_index for old versions of astropy
@Cadair
Cadair requested a review from astrofrog October 22, 2026 11:14
@Cadair
Cadair marked this pull request as ready for review October 22, 2026 11:14
@Cadair
Cadair requested a review from alasdairwilson October 22, 2026 11:14
@Cadair

Cadair commented Oct 22, 2026

Copy link
Copy Markdown
Member

I have largely re-written this to be a lot more sane.

Comment thread ndcube/extra_coords/table_coord.py Outdated
import scipy.interpolate
except ImportError:
pass
from ndcube.utils.misc import simplify_basic_index

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think this should be private?

item = tuple(slice(*i.indices(n)[:2]) if isinstance(i, slice) else range(n)[i]
for i, n in zip(item, self.shape))
# Drop the step: nested WCS slicing rejects an explicit step before astropy 7.2.3/8.0.2.
item = tuple(slice(i.start, i.stop) if isinstance(i, slice) else i for i in item)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to avoid having removable code in multiple places, you could patch the vendored version of simplify_basic_index to change steps to None if they are 1, then you don't need this here since any step that is present is definitely not None or 1.


# Slice cube.
item = tuple(sanitize_slices(item, len(self.shape)))
# TODO: Remove when astropy>7.2.3

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this refer to now the surrounding code has changed?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The drop the step part below isn't needed once we have patched-astropy

Comment thread ndcube/mixins/ndslicing.py
Comment on lines +22 to +24
# Error early if a step is passed
if any(isinstance(i, slice) and i.step not in (None, 1) for i in item):
raise IndexError("Slicing WCS with a step is not supported.")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good, I like this

Comment thread ndcube/extra_coords/table_coord.py Outdated
@Cadair
Cadair enabled auto-merge October 22, 2026 12:13
@Cadair
Cadair merged commit 4a1eb94 into sunpy:main Oct 22, 2026
22 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compatibility with Astropy 8.0.2 (2026-10-15)

5 participants