Skip to content

Commit 0c4902f

Browse files
committed
Descend into subgroups when removing whitespace (fixes #782).
1 parent a8de06e commit 0c4902f

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

CHANGELOG

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Bug Fixes
1717
* Fix error when splitting statements that contain multiple CASE clauses
1818
within a BEGIN block (issue784).
1919

20+
* Fix whitespace removal with nested expressions (issue782).
21+
2022

2123
Release 0.5.0 (Apr 13, 2024)
2224
----------------------------

sqlparse/filters/others.py

+4
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ def _stripws_parenthesis(self, tlist):
9191
tlist.tokens.pop(1)
9292
while tlist.tokens[-2].is_whitespace:
9393
tlist.tokens.pop(-2)
94+
if tlist.tokens[-2].is_group:
95+
# save to remove the last whitespace
96+
while tlist.tokens[-2].tokens[-1].is_whitespace:
97+
tlist.tokens[-2].tokens.pop(-1)
9498
self._stripws_default(tlist)
9599

96100
def process(self, stmt, depth=0):

tests/test_format.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def test_strip_comments_preserves_linebreak(self):
100100
sql = 'select * -- a comment\n\nfrom foo'
101101
res = sqlparse.format(sql, strip_comments=True)
102102
assert res == 'select *\n\nfrom foo'
103-
103+
104104
def test_strip_comments_preserves_whitespace(self):
105105
sql = 'SELECT 1/*bar*/ AS foo' # see issue772
106106
res = sqlparse.format(sql, strip_comments=True)
@@ -734,8 +734,8 @@ def test_format_json_ops(): # issue542
734734
"select foo->'bar', foo->'bar';", reindent=True)
735735
expected = "select foo->'bar',\n foo->'bar';"
736736
assert formatted == expected
737-
738-
737+
738+
739739
@pytest.mark.parametrize('sql, expected_normal, expected_compact', [
740740
('case when foo then 1 else bar end',
741741
'case\n when foo then 1\n else bar\nend',
@@ -745,3 +745,10 @@ def test_compact(sql, expected_normal, expected_compact): # issue783
745745
formatted_compact = sqlparse.format(sql, reindent=True, compact=True)
746746
assert formatted_normal == expected_normal
747747
assert formatted_compact == expected_compact
748+
749+
750+
def test_strip_ws_removes_trailing_ws_in_groups(): # issue782
751+
formatted = sqlparse.format('( where foo = bar ) from',
752+
strip_whitespace=True)
753+
expected = '(where foo = bar) from'
754+
assert formatted == expected

0 commit comments

Comments
 (0)