Skip to content

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

@Bassem-Akrout

Description

@Bassem-Akrout

Describe the bug
When using

result = sqlparse.format(
        sql=sql_text, strip_comments=True, keyword_case="upper", reindent=True
    )

On this SQL issue.sql we still have tabulations in format of "\t" and not spaces + we have comments that stay and aren't stripped
To Reproduce
Steps to reproduce the behavior.
Use this script

import sqlparse


def strip_sql_comments(sql_text: str, reindent=False) -> str:
    """
    Strip comments from SQL.

    Args:
        sql_text (str): unformatted SQL.

    Returns:
        str: formatted SQL.
    """
    result = sqlparse.format(
        sql=sql_text, strip_comments=True, keyword_case="upper", reindent=reindent
    )
    return result

sql_path="issue.sql"
with open(sql_path, "r", encoding="utf-8") as file:
    sql_content = file.read()
# Run the function
cleaned_sql = strip_sql_comments(sql_content, reindent=True)
print(cleaned_sql)

What's the concrete error:
No error/traceback occurs; the function runs successfully but does not strip the comments or normalize tabs as expected.
Expected behavior
All -- comments should be fully removed when strip_comments=True. Tabs should be converted to spaces during reindentation for consistent indentation.

Versions (please complete the following information):

  • Python 3.12.0
  • sqlparse 0.5.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions