Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Alphabetical list of contributors:
* ali-tny <aliteeney@googlemail.com>
* andrew deryabin <github@djsf.com>
* Andrew Tipton <andrew.tipton@compareglobalgroup.com>
* Anshul Singh <https://github.com/ekanshul>
* atronah <atronah.ds@gmail.com>
* casey <casey@cloudera.com>
* Cauê Beloni <cbeloni@gmail.com>
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Development Version
-------------------

Nothing yet.
* Fix IndexError in ``strip_whitespace`` for parenthesis groups that
collapse to a single nested child (e.g. ``( AS )``). See issue885.


Release 0.6.0 (Aug 13, 2026)
Expand Down
7 changes: 6 additions & 1 deletion sqlparse/filters/others.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,18 @@ def _stripws_identifierlist(self, tlist):
return self._stripws_default(tlist)

def _stripws_parenthesis(self, tlist):
# Well-formed groups look like "(", content..., ")". Some inputs
# (e.g. "( AS )") collapse to a single nested child, so there is
# no tokens[1] / tokens[-2] to strip.
if len(tlist.tokens) < 3:
return self._stripws_default(tlist)
while tlist.tokens[1].is_whitespace:
tlist.tokens.pop(1)
while tlist.tokens[-2].is_whitespace:
tlist.tokens.pop(-2)
if tlist.tokens[-2].is_group:
# save to remove the last whitespace
while tlist.tokens[-2].tokens[-1].is_whitespace:
while tlist.tokens[-2].tokens and tlist.tokens[-2].tokens[-1].is_whitespace:
tlist.tokens[-2].tokens.pop(-1)
self._stripws_default(tlist)

Expand Down
6 changes: 6 additions & 0 deletions tests/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@ def test_issue213_leadingws():
assert sqlparse.format(sql, strip_whitespace=True) == "select * from foo"


def test_issue885_strip_ws_malformed_parenthesis():
# Parenthesis grouping can collapse "( AS )" to a single nested child.
# strip_whitespace must not assume tokens[1] / tokens[-2] exist.
assert sqlparse.format("( AS )", strip_whitespace=True) == "( AS )"


def test_issue227_gettype_cte():
select_stmt = sqlparse.parse('SELECT 1, 2, 3 FROM foo;')
assert select_stmt[0].get_type() == 'SELECT'
Expand Down