Skip to content

Allow NormalizedSlice as an indexer.#642

Open
kaushikcfd wants to merge 3 commits intomainfrom
fix_gh_633
Open

Allow NormalizedSlice as an indexer.#642
kaushikcfd wants to merge 3 commits intomainfrom
fix_gh_633

Conversation

@kaushikcfd
Copy link
Collaborator

@kaushikcfd kaushikcfd commented Mar 4, 2026

Note:

  • The easy parts were done by Claude. I asked it: "Fix this issue <https://github.com/inducer/pytato/issues/633>". I only fixed the types in the body of normalize_slice_len, which I figured was easier than telling Claude about the _is_non_(negative|positive).

Closes #633.

@kaushikcfd kaushikcfd requested review from inducer March 4, 2026 03:31
@kaushikcfd kaushikcfd changed the title Allow NormalizedSlice as an indexee. Allow NormalizedSlice as an indexer. Mar 4, 2026
Note that the "slice" class does not support subclassing hence we had to
add NormalizedSlice to ConvertibleToIndexExpr.
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR extends pytato’s indexing interface to accept NormalizedSlice objects directly (in addition to built-in slice), addressing #633 and enabling NormalizedSlice to be used as a user-facing indexer.

Changes:

  • Allow NormalizedSlice to be passed into Array.__getitem__ by widening ConvertibleToIndexExpr.
  • Update _index_into / _normalize_slice to accept and normalize NormalizedSlice in addition to slice.
  • Add a regression test ensuring NormalizedSlice can be used as an indexer and update the pyright baseline.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
pytato/array.py Extends index typing to include NormalizedSlice as a valid input index expression.
pytato/utils.py Accepts NormalizedSlice in indexing validation/normalization and updates slice normalization logic.
test/test_pytato.py Adds a test covering NormalizedSlice being usable as an indexer.
.basedpyright/baseline.json Removes resolved/changed type-check baseline entries.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +473 to +478
if _is_non_negative(stop + axis_len) and not _is_non_positive(axis_len - stop):
stop = stop % axis_len
elif _is_non_negative(stop - axis_len):
stop = axis_len if step > 0 else axis_len - 1
else:
raise NotImplementedError
stop = 0 if step > 0 else -1
assert 3*x == new_expr


def test_normalized_slice_is_valid_indexee():
Comment on lines +2069 to +2072
a = pt.make_placeholder("a", 10)
assert a[NormalizedSlice(0, 10, 1)] == a[:]


Comment on lines +461 to +468
if _is_non_negative(start + axis_len) and not _is_non_positive(
axis_len - start
):
start = start % axis_len
elif _is_non_negative(start - axis_len):
start = axis_len if step > 0 else axis_len - 1
else:
raise NotImplementedError
start = 0 if step > 0 else -1
@inducer
Copy link
Owner

inducer commented Mar 17, 2026

These sound like reasonable comments for the most part. @kaushikcfd, could you take a look?

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.

Normalized slice should be subclasses for slice.

3 participants