Skip to content

Reject repeated numbers in slice components - #387

Open
KennyMcSimpson wants to merge 1 commit into
jmespath:developfrom
KennyMcSimpson:fix/reject-repeated-slice-components
Open

KennyMcSimpson wants to merge 1 commit into
jmespath:developfrom
KennyMcSimpson:fix/reject-repeated-slice-components

Conversation

@KennyMcSimpson

Copy link
Copy Markdown

The slice parser currently accepts multiple numbers in the same component and silently keeps only the last one. For example, jmespath.search('[0:1 3]', list(range(6))) returns [0, 1, 2], treating the expression as [0:3]. [0:1-2] and [::1 2] are accepted similarly.

The slice grammar permits at most one number in each component. Reject a second number through the existing syntax-error path instead of overwriting the component. Checking against None also correctly rejects repeated numbers when the previous number is zero.

Add eight invalid-syntax compliance cases covering repeated end/step values, signed values, zero values, and both field and root slices. Add valid whitespace and zero-endpoint controls, and update the changelog.

Validation on Windows / Python 3.13:

  • Before the fix: all eight new syntax cases fail because the invalid expressions parse successfully; the other 991 tests pass, with one existing skip.
  • python -m tox -e py313: 1001 passed, 1 skipped.
  • With the exact pinned CI dependencies and CI coverage command: 1001 passed, 1 skipped, 99% total coverage.
  • Directly verified all eight invalid expressions raise ParseError at the second number.

Other Python versions and operating systems are left to CI. This contribution was prepared with AI assistance.

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.

1 participant