Wrong reformatting when line cannot be analyzed when USE_TABS=true
- Dominant language
- Python
- Stars
- 14k
- Forks
- 904
- PR merge metrics
- No merged PRs in 30d
Description
example file, indented using tabs:
```
def test():
return Paginator(
q,
order_by=(
z,
y,
x,
),
cursor_value=lambda invitation: (b, a,),
)
```
setup.cfg:
```
[yapf]
continuation_indent_width=4
indent_width=4
use_tabs=True
```
Using latest master, yields the following diff:
```
--- foo.py (original)
+++ foo.py (reformatted)
@@ -1,10 +1,10 @@
def test():
return Paginator(
- q,
- order_by=(
- z,
- y,
- x,
- ),
- cursor_value=lambda invitation: (b, a,),
+ q,
+ order_by=(
+ z,
+ y,
+ x,
+ ),
+ cursor_value=lambda invitation: (b, a,),
)
```
This is because the allow_multiline_lambas is false by default, and yapf tries to split the lambda and fails. This in turn causes _AnalyzeSolutionSpace(state) to return False in reformatter.py, and yapf tries to re-emit the line as is. However the _RetainHorizontalSpacing function tries to compute the indentation levels which end up in the code below (format_token.py:195):
`self.spaces_required_before = (self.column - first_column + depth * style.Get('INDENT_WIDTH'))`
I'm not sure about the fix, but it seems this doesn't take into account the tab width properly. If i change to (multiply everything by indent_width):
`self.spaces_required_before = (self.column - first_column + depth) * style.Get('INDENT_WIDTH')`
this particular case is properly formatted, but i'm not sure what's the impact of this change on other cases.
In any case it seems this part of the code doesn't take into account tabs properly when trying to re-emit code unmodified.
Contributor guide
Assessment
This issue has not been assessed yet.