Skip to content

Commit 80ff719

Browse files
committed
Ensure syntax errors from yapf are correctly handled.
1 parent 5d83c44 commit 80ff719

5 files changed

Lines changed: 61 additions & 2 deletions

formate/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ def yapf_hook(source: str, formate_global_config: Optional[Mapping] = None, **kw
186186
"""
187187

188188
# 3rd party
189-
from yapf.yapflib.yapf_api import FormatCode # type: ignore[import-untyped]
189+
from yapf.pytree.pytree_utils import ParseCodeToTree # type: ignore[import-untyped]
190+
from yapf.yapflib.yapf_api import FormatTree # type: ignore[import-untyped]
190191

191192
with TemporaryPathPlus() as tmpdir:
192193
config_file = tmpdir / ".style.yapf"
@@ -222,7 +223,8 @@ def yapf_hook(source: str, formate_global_config: Optional[Mapping] = None, **kw
222223
with config_file.open('w') as fp:
223224
config.write(fp)
224225

225-
return FormatCode(source, style_config=str(config_file))[0]
226+
tree = ParseCodeToTree(source)
227+
return FormatTree(tree, style_config=str(config_file))
226228

227229

228230
class Reformatter:

tests/test_utils.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33

44
# 3rd party
55
import pytest
6+
from coincidence.regressions import AdvancedDataRegressionFixture
7+
from coincidence.selectors import max_version, min_version
68

79
# this package
10+
from formate import yapf_hook
811
from formate.classes import Hook
912
from formate.exceptions import HookNotFoundError
1013
from formate.reformat_generics import reformat_generics
@@ -80,3 +83,40 @@ def test_syntaxerror_for_file():
8083
ast.parse("def foo()pass", filename="__init__.py")
8184

8285
assert exc_info.value.filename == "__init__.py"
86+
87+
88+
@pytest.mark.parametrize(
89+
"python_version",
90+
[
91+
pytest.param("3.7", marks=max_version("3.9")),
92+
pytest.param("3.10", marks=[min_version("3.10"), max_version("3.11")]),
93+
pytest.param("3.12", marks=min_version("3.12")),
94+
]
95+
)
96+
def test_syntaxerror_for_file_f_string(python_version, advanced_data_regression: AdvancedDataRegressionFixture):
97+
98+
with pytest.raises(SyntaxError) as exc_info: # noqa: PT012
99+
with syntaxerror_for_file("code.py"):
100+
ast.parse("f'")
101+
102+
exc: SyntaxError = exc_info.value
103+
advanced_data_regression.check({
104+
"msg": exc.msg,
105+
"filename": exc.filename,
106+
"lineno": exc.lineno,
107+
"offset": exc.offset,
108+
"text": exc.text,
109+
})
110+
111+
with pytest.raises(SyntaxError) as exc_info: # noqa: PT012
112+
with syntaxerror_for_file("code.py"):
113+
yapf_hook("f'")
114+
115+
exc: SyntaxError = exc_info.value
116+
advanced_data_regression.check({
117+
"msg": exc.msg,
118+
"filename": exc.filename,
119+
"lineno": exc.lineno,
120+
"offset": exc.offset,
121+
"text": exc.text,
122+
})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
filename: code.py
2+
lineno: 1
3+
msg: unterminated string literal (detected at line 1)
4+
offset: 1
5+
text: f'
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
filename: code.py
2+
lineno: 1
3+
msg: unterminated f-string literal (detected at line 1)
4+
offset: 1
5+
text: f'
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
filename: code.py
2+
lineno: 1
3+
msg: EOL while scanning string literal
4+
offset: 3
5+
text: 'f''
6+
7+
'

0 commit comments

Comments
 (0)