PabloRMira / sql_formatter

A Python based SQL formatter
https://pablormira.github.io/sql_formatter/
Apache License 2.0
43 stars 10 forks source link

UnboundLocalError when trying to format any SQL file #155

Closed guillemborrell closed 3 years ago

guillemborrell commented 3 years ago

Describe the bug

When executing sql-formatter 0.6.0 in any sql file, I get UnboundLocalError: local variable 'out_list' referenced before assignment

To Reproduce

Run sql-formatter on any file on python 3.8.0

Expected behavior

The tool runs without exceptions

Screenshots

$ sql-formatter -m 80 check_for_stage_success.sql 
Traceback (most recent call last):
  File "/home/guillem/dags-airflow/env/bin/sql-formatter", line 11, in <module>
    sys.exit(format_sql_files_cli())
  File "/home/guillem/dags-airflow/env/lib/python3.8/site-packages/sql_formatter/format_file.py", line 197, in format_sql_files_cli
    format_sql_files(files=args.files, recursive=args.recursive, max_len=args.max_line_length)
  File "/home/guillem/dags-airflow/env/lib/python3.8/site-packages/sql_formatter/format_file.py", line 167, in format_sql_files
    exit_codes.append(format_sql_file(file, max_len=max_len))
  File "/home/guillem/dags-airflow/env/lib/python3.8/site-packages/sql_formatter/format_file.py", line 116, in format_sql_file
    formatted_file = format_sql_commands(sql_commands, max_len=max_len)
  File "/home/guillem/dags-airflow/env/lib/python3.8/site-packages/sql_formatter/format_file.py", line 42, in format_sql_commands
    formatted_split_s2 = [
  File "/home/guillem/dags-airflow/env/lib/python3.8/site-packages/sql_formatter/format_file.py", line 43, in <listcomp>
    "\n\n\n" + format_sql(sp, semicolon=True, max_len=max_len).strip()
  File "/home/guillem/dags-airflow/env/lib/python3.8/site-packages/sql_formatter/core.py", line 462, in format_sql
    s = format_simple_sql(s, semicolon=semicolon, max_len=max_len)  # basic query formatting
  File "/home/guillem/dags-airflow/env/lib/python3.8/site-packages/sql_formatter/core.py", line 449, in format_simple_sql
    s = format_statements(s, max_len=max_len)  # format statements
  File "/home/guillem/dags-airflow/env/lib/python3.8/site-packages/sql_formatter/core.py", line 409, in format_statements
    formatted_lines = [
  File "/home/guillem/dags-airflow/env/lib/python3.8/site-packages/sql_formatter/core.py", line 410, in <listcomp>
    format_statement_line(line, **kwargs) for line in statement_lines
  File "/home/guillem/dags-airflow/env/lib/python3.8/site-packages/sql_formatter/core.py", line 402, in format_statement_line
    s = format_func(s, **kwargs)
  File "/home/guillem/dags-airflow/env/lib/python3.8/site-packages/sql_formatter/core.py", line 302, in format_select
    s = "\n".join([
  File "/home/guillem/dags-airflow/env/lib/python3.8/site-packages/sql_formatter/core.py", line 303, in <listcomp>
    reformat_too_long_line(li, max_len=max_len)
  File "/home/guillem/dags-airflow/env/lib/python3.8/site-packages/sql_formatter/core.py", line 258, in reformat_too_long_line
    out_list.append(li[j:].strip())
UnboundLocalError: local variable 'out_list' referenced before assignment
PabloRMira commented 3 years ago

Hi @guillemborrell, the bug was actually fixed by #150

I just did not release it yet, sorry for that!

I'll release soon and let you know ;-)

PabloRMira commented 3 years ago

@guillemborrell : As promised I've just released the bugfix in version 0.6.1, so if you update to the newest version it should work for you

Let me know if this is not the case

guillemborrell commented 3 years ago

@PabloRMira I confirm that the bug is fixed. Thanks a bunch! 👍