Skip to content

fix: properly strip line comments and convert tabs to spaces in sqlparse.format#21

Open
label-hook[bot] wants to merge 1 commit into
masterfrom
phoenix/issue-16
Open

fix: properly strip line comments and convert tabs to spaces in sqlparse.format#21
label-hook[bot] wants to merge 1 commit into
masterfrom
phoenix/issue-16

Conversation

@label-hook
Copy link
Copy Markdown

@label-hook label-hook Bot commented Apr 7, 2026

Summary

Fixes two issues in sqlparse.format() when using strip_comments=True and reindent=True:

  1. Line comments starting with -- were not being properly removed
  2. Tabs were not being converted to spaces during reindentation

Closes #16

Changes

StripCommentsFilter (sqlparse/filters/others.py)

  • Fixed the _process method to properly identify and remove line comments (--)
  • Enhanced comment detection logic to handle line comments that appear after SQL code on the same line
  • Preserved newlines and statement structure while removing comment content

ReindentFilter (sqlparse/filters/reindent.py)

  • Added tab-to-space conversion in the reindentation process
  • Modified the _process method to normalize tabs to spaces before applying indentation
  • Ensures consistent spacing when reindent=True is used

Testing

  • Created comprehensive test cases covering both line comment removal and tab conversion
  • Tested with the original issue.sql file to verify both problems are resolved
  • Verified that strip_comments=True now properly removes all -- comments
  • Confirmed that reindent=True converts tabs to spaces for consistent indentation
  • All existing tests continue to pass, ensuring no regression in functionality

Closes #16

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai:review Phoenix AI: PR ready for review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sqlparse.format does not strip -- comments and fails to convert tabs to spaces when reindent=True

1 participant