Skip to content

Commit

Permalink
fix/#536/jinjafmt multiline string (#537)
Browse files Browse the repository at this point in the history
* chore: bump deps

* fix: detect multiline strings inside jinja tags

* fix: catch syntax errors in ast parse
  • Loading branch information
tconbeer authored Dec 19, 2023
1 parent fff50ef commit a15fc26
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 207 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ default_language_version:
python: python3.10
repos:
- repo: https://github.com/psf/black
rev: 23.3.0
rev: 23.12.0
hooks:
- id: black
- repo: https://github.com/PyCQA/flake8
rev: 5.0.4
hooks:
- id: flake8
- repo: https://github.com/PyCQA/isort
rev: 5.11.5
rev: 5.13.2
hooks:
- id: isort
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.4.1
rev: v1.7.1
hooks:
- id: mypy
additional_dependencies:
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Bug Fixes

- Fixes a bug where extra indentation was added inside multiline jinja tags if those jinja tags contained a python multiline string ([#536](https://github.com/tconbeer/sqlfmt/issues/500) - thank you [@yassun7010](https://github.com/yassun7010)!).
## [0.21.0] - 2023-10-20

### Bug Fixes
Expand Down
3 changes: 1 addition & 2 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[mypy]
python_version=3.10
mypy_path = $MYPY_CONFIG_FILE_DIR/stubs
files=
src/sqlfmt/**/*.py,
Expand Down Expand Up @@ -28,4 +27,4 @@ warn_unused_configs=True

no_implicit_reexport=True
strict_equality=True
strict_concatenate=True
extra_checks=True
364 changes: 165 additions & 199 deletions poetry.lock

Large diffs are not rendered by default.

29 changes: 26 additions & 3 deletions src/sqlfmt/jinjafmt.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import ast
import keyword
import re
from dataclasses import dataclass, field
from importlib import import_module
from itertools import chain, product
from types import ModuleType
from typing import Dict, List, NamedTuple, Optional, Tuple
from typing import Dict, List, MutableSet, NamedTuple, Optional, Tuple

from sqlfmt.line import Line
from sqlfmt.mode import Mode
Expand Down Expand Up @@ -259,6 +260,7 @@ def _multiline_str(self) -> str:
will already be indented to the proper depth (because of the Line).
"""
indent = " " * 4 * (self.depth[0] + self.depth[1])
no_indent_lines = self._find_multiline_python_str_lines()
code_lines = iter(self.code.splitlines(keepends=False))

if self.verb:
Expand All @@ -268,8 +270,10 @@ def _multiline_str(self) -> str:
lines = [f"{self.opening_marker}"]
extra_indent = " " * 4

for code_line in code_lines:
lines.append(f"{indent}{extra_indent}{code_line}")
for i, code_line in enumerate(code_lines, start=1 if self.verb else 0):
lines.append(
f"{indent}{'' if i in no_indent_lines else extra_indent}{code_line}"
)

if self.verb:
lines[-1] = f"{indent}{lines[-1].lstrip()} {self.closing_marker}"
Expand All @@ -281,6 +285,25 @@ def _multiline_str(self) -> str:
def _basic_str(self) -> str:
return f"{self.opening_marker} {self.verb}{self.code} {self.closing_marker}"

def _find_multiline_python_str_lines(self) -> MutableSet[int]:
try:
tree = ast.parse(self.code, mode="eval")
except SyntaxError:
# this jinja isn't quite python, so give up here.
return set()

line_indicies: MutableSet[int] = set()
for node in ast.walk(tree):
if (
isinstance(node, ast.Constant)
and isinstance(node.value, str)
and "\n" in node.value
and node.end_lineno is not None
):
line_indicies |= set(range(node.lineno, node.end_lineno))

return line_indicies

def _remove_trailing_comma(self) -> None:
"""
dbt Jinja doesn't allow trailing commas in macro definitions. Mutates
Expand Down
18 changes: 18 additions & 0 deletions tests/data/preformatted/302_jinjafmt_multiline_str.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{{
config(
materialized="incremental",
pre_hook="""
delete from
dwh.user as t using (
select distinct campaign_name, date
from datalake.conversion
where date_part = date('{{ execution_date }}')
) as s
where
t.campaign_name = s.campaign_name
and to_date(t.imported_at) <= s.date_part
""",
)
}}

select campaign_name, date_part, count(distinct user_id) as users
1 change: 1 addition & 0 deletions tests/functional_tests/test_general_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"preformatted/006_fmt_off_447.sql",
"preformatted/007_fmt_off_comments.sql",
"preformatted/301_multiline_jinjafmt.sql",
"preformatted/302_jinjafmt_multiline_str.sql",
"preformatted/400_create_table.sql",
"unformatted/100_select_case.sql",
"unformatted/101_multiline.sql",
Expand Down
16 changes: 16 additions & 0 deletions tests/unit_tests/test_jinjafmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,3 +481,19 @@ def test_preprocess_and_postprocess_are_inverse_ops(source_string: str) -> None:
assert BlackWrapper._postprocess_string(
*BlackWrapper._preprocess_string(source_string)
).replace(" ", "") == source_string.replace(" ", "")


@pytest.mark.parametrize(
"source_string",
[
"""{{\n config(\n foo="bar",\n )\n}}""",
'''{{\n config(\n foo="""\n\nbar\n\n""",\n )\n}}''',
],
)
def test_multiline_str(source_string: str) -> None:
tag = JinjaTag.from_string(source_string=source_string, depth=(0, 0))
tag.code, tag.is_blackened = BlackWrapper().format_string(
source_string=tag.code, max_length=88
)
assert tag.is_blackened
assert str(tag) == source_string

0 comments on commit a15fc26

Please sign in to comment.