Skip to content

fix: avoid ValueError on malformed CASE with reindent_aligned - #944

Open
MbappeWU wants to merge 1 commit into
andialbrecht:masterfrom
MbappeWU:fix/886-reindent-aligned-case
Open

MbappeWU wants to merge 1 commit into
andialbrecht:masterfrom
MbappeWU:fix/886-reindent-aligned-case

Conversation

@MbappeWU

@MbappeWU MbappeWU commented Nov 1, 2026

Copy link
Copy Markdown

Summary

Fixes #886.

sqlparse.format(..., reindent_aligned=True) on malformed CASE input such as:

sqlparse.format(\"CASE 'a' := WHERE END SELECT GO # ->>\", reindent_aligned=True)

raised ValueError: None is not in list inside token_index / insert_before.

Cause

AlignedIndentFilter._process_case always appended the END aligner from token_next_by(END). When END is missing as a direct child (here it is nested inside an Assignment subgroup), that token is None, and the next insert_before(None, ...) crashes.

Fix

  • Append the END aligner only when end_token is not None.
  • Skip empty case arms / stmt is None.
  • Tolerate ValueError from insert_before when the token is not a direct child.

Valid CASE alignment is unchanged.

Verification

  • New regression in tests/test_regressions.py + TestFormatReindentAligned
  • Full suite: 508 passed, 2 xfailed, 1 xpassed

AlignedIndentFilter._process_case appended a None END token when END was
missing or nested inside a subgroup, then crashed in token_index during
insert_before. Skip a missing END, guard empty case arms, and tolerate
tokens that are not direct children.

Fixes andialbrecht#886
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-11-01T01:17:37.275148Z
We couldn’t complete this review. Comment @codex review to try again.
e4bbf56 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

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.

reindent_aligned=True raises ValueError (token_index) on a malformed CASE input

1 participant