Fix Path slicing to match standard sequence semantics - #301
Open
gaoflow wants to merge 1 commit into
Open
Conversation
Path.__getitem__ built the sliced ops by hand: it scaled the start/stop
indices into the internal (op, value) layout but never clamped
out-of-range bounds and mishandled negative steps. As a result a slice
such as Path('a','b','c')[-5:] or [::-1] returned a corrupt Path whose
.values() disagreed with the equivalent tuple slice, and empty slices
produced a Path with a misaligned ops tuple.
Handle the slice branch separately using range(*i.indices(len(self))),
which yields exactly the component indices Python sequence slicing would,
then reassemble the (op, value) pairs for those components. The integer
index branch is unchanged.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #301 +/- ##
=======================================
Coverage 98.22% 98.23%
=======================================
Files 27 27
Lines 4391 4407 +16
Branches 612 617 +5
=======================================
+ Hits 4313 4329 +16
Misses 53 53
Partials 25 25 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Path.__getitem__handles slicing by scaling thestart/stopindices intothe path's internal
(op, value)ops layout, but it never clamps out-of-rangebounds and mishandles negative steps. The result is a
Pathwhose.values()disagrees with the equivalent tuple slice, and empty slices produce a
Pathwith a misaligned ops tuple:
Integer indexing already clamps/raises correctly; only the slice branch is
affected. This is the sibling of the recent integer-index fix (GH-299).
Fix
Split the slice branch out and drive it with
range(*i.indices(len(self))),which yields exactly the component indices standard sequence slicing would
(clamped bounds, negative steps honored). The
(op, value)pairs for thosecomponents are then reassembled. The integer-index branch is unchanged.
Tests
test_path_slices_match_sequence_semanticschecks clamped negative bounds,negative steps, empty-slice validity, and an exhaustive cross-check of
Path(...)[key].values() == values_tuple[key]over paths of length 0–4 and agrid of start/stop/step values.
pytest glom/test/test_path_and_t.pypasses (the 3 unrelatedtest_cli.pyfailures in my environment are a missing optional YAML dependency and reproduce
on a clean checkout).