Skip to content

Fix IndexError in strip_whitespace for collapsed parenthesis groups - #918

Open
ekanshul wants to merge 1 commit into
andialbrecht:masterfrom
ekanshul:fix/strip-whitespace-malformed-parenthesis
Open

ekanshul wants to merge 1 commit into
andialbrecht:masterfrom
ekanshul:fix/strip-whitespace-malformed-parenthesis

Conversation

@ekanshul

@ekanshul ekanshul commented Oct 5, 2026

Copy link
Copy Markdown

Fixes #885.

sqlparse.format("( AS )", strip_whitespace=True) raised IndexError because grouping collapses that input into a Parenthesis with a single nested Identifier. _stripws_parenthesis then indexed tokens[1] and tokens[-2], which do not exist.

This change skips the inner/outer whitespace pops when the group has fewer than three tokens and falls through to the default strip. Well-formed groups such as ( 1 = 2 ) are unchanged.

>>> import sqlparse
>>> sqlparse.format("( AS )", strip_whitespace=True)
'( AS )'
>>> sqlparse.format("select * from foo where ( 1 = 2 )", strip_whitespace=True)
'select * from foo where (1 = 2)'

Added a regression test. Full suite: 507 passed, 2 xfailed, 1 xpassed.

"( AS )" is grouped as a Parenthesis with a single nested Identifier, so
_stripws_parenthesis must not assume tokens[1] and tokens[-2] exist.

Fixes andialbrecht#885
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.

strip_whitespace=True raises IndexError on "( AS )"

1 participant