fix: is_ordered_sublist matching substrings across list items - #2286
Conversation
is_ordered_sublist joined both lists with spaces and did a substring test, so a needle item could match part of a haystack item (for example ['b'] was reported as contained in ['abc']). Compare the items directly as a contiguous run instead, which matches the documented behavior and the existing examples. Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
|
review from Claude: Review: #2286 — Fix
|
|
|
Replaces the explicit scan loop with any(), which is what 'hatch fmt --linter --check' was flagging (SIM110). Behavior is unchanged, including the empty-needle and needle-longer-than-haystack cases. Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
|
Thanks @degenaro, fixed in f7b9849. The failure was ruff SIM110 on the scan loop in return any(haystack[start:start + n_items] == needle for start in range(len(haystack) - n_items + 1))
Behavior is unchanged. I checked the old and new versions against each other over the edge cases (empty needle, needle longer than haystack, repeated elements, both docstring examples, and the One note in case it comes up separately: |
Whitespace only. Makes 'ruff format --check' clean for this file as well as the linter. Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
|
Follow-up on my note about the formatter: I was wrong that it was worth leaving alone.
|
|
@arpitjain099 are you on our slack channel? [#oscal-compass-trestle-agileauthoring-c2p](https://cloud-native.slack.com/archives/C06F3PEPNBW) |
@degenaro |
@degenaro can you help me get an invite to slack? I can share my email (rather not here, publicly) |
|
@arpitjain099 Have you tried https://cncf-slack.netlify.app/ ? |
@degenaro Joined :) |
|
@degenaro this PR is ready to merge. |
* Fix is_ordered_sublist matching substrings across list items is_ordered_sublist joined both lists with spaces and did a substring test, so a needle item could match part of a haystack item (for example ['b'] was reported as contained in ['abc']). Compare the items directly as a contiguous run instead, which matches the documented behavior and the existing examples. Signed-off-by: Arpit Jain <arpitjain099@gmail.com> * fix: satisfy ruff SIM110 in is_ordered_sublist Replaces the explicit scan loop with any(), which is what 'hatch fmt --linter --check' was flagging (SIM110). Behavior is unchanged, including the empty-needle and needle-longer-than-haystack cases. Signed-off-by: Arpit Jain <arpitjain099@gmail.com> * style: apply ruff format to the slice expression Whitespace only. Makes 'ruff format --check' clean for this file as well as the linter. Signed-off-by: Arpit Jain <arpitjain099@gmail.com> --------- Signed-off-by: Arpit Jain <arpitjain099@gmail.com> Co-authored-by: Lou DeGenaro <lou.degenaro@gmail.com> Signed-off-by: Lou DeGenaro <lou.degenaro@gmail.com>

Types of changes
Summary
While reading through
trestle/common/list_utils.pyI noticedis_ordered_sublistjoins both lists with spaces and then does a plain substring test. That lets a needle item match part of a haystack item rather than a whole item, sois_ordered_sublist(['b'], ['abc'])returns True andis_ordered_sublist(['a', 'b'], ['xa', 'by'])does too. The docstring and its examples describe an item-wise contiguous match, so these are false positives. This function is used in the xlsx component-definition task (xlsx_helper.py) to detect the NIST mappings column from header tokens, where a partial-token match could pick the wrong column.The fix compares the items directly as a contiguous run instead of using string containment. I added a few cases to the existing test covering the substring situations and a needle longer than the haystack. The updated
list_utilstests and thexlsx_to_oscal_cdtask tests both pass locally.Key links: