Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trailing whitespace changes are made outside of --lines #613

Open
dmytrokyrychuk opened this issue Sep 4, 2018 · 4 comments
Open

Trailing whitespace changes are made outside of --lines #613

dmytrokyrychuk opened this issue Sep 4, 2018 · 4 comments

Comments

@dmytrokyrychuk
Copy link

Description

Trailing whitespace is removed from the whole file when attempting to format a subset of a file.

Steps to reproduce

  1. pip install yapf==0.23

  2. Run the following script:

from yapf.yapflib.yapf_api import FormatCode

source = """\
def with_trailing_whitespace():  
    pass  

def without_trailing_whitespace(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbb):
    return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbb
"""

reformatted_diff, changed = FormatCode(source, lines=[(4, 5)], print_diff=True)
print(reformatted_diff)

Expected output

--- <unknown>   (original)
+++ <unknown>   (reformatted)
@@ -1,5 +1,7 @@
 def with_trailing_whitespace():
     pass

-def without_trailing_whitespace(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbb):
+
+def without_trailing_whitespace(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
+                                bbbbbbbbbb):
     return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbb

Actual output

--- <unknown>   (original)
+++ <unknown>   (reformatted)
@@ -1,5 +1,7 @@
-def with_trailing_whitespace():
-    pass
+def with_trailing_whitespace():
+    pass

-def without_trailing_whitespace(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbb):
+
+def without_trailing_whitespace(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
+                                bbbbbbbbbb):
     return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbb
@braineo
Copy link

braineo commented Jan 2, 2019

I also ran into this issue.

@wwade
Copy link

wwade commented Mar 24, 2020

It appears to be a problem in the PyTreeVisitor implmentation. When building the uwlines for the file, for example with_trailing_whitespace(): will lose the trailing whitespace as part of the Visit_suite method:

 def Visit_suite(self, node):  # pylint: disable=invalid-name
   # A 'suite' starts a new indentation level in Python.
   self._cur_depth += 1
   self._StartNewLine()
   self.DefaultNodeVisit(node)
   self._cur_depth -= 1

Here, node.children[ 0 ] will have a value of \n and a prefix of the trailing whitespace contents, but those trailing whitespace contents will not be added to the _cur_unwrapped_line before finalizing it in _StartNewLine().

@SkyeNygaard
Copy link

This is still an issue and it is preventing me from using yapf on my projects.

@limwz01
Copy link

limwz01 commented Jun 5, 2023

@SkyeNygaard @braineo @dmytrokyrychuk

I've created a fork (PR 1102) that fixes this bug, but it needs extensive testing as making this right is quite difficult. It still wouldn't fix the test case in the initial post, as the whitespace before line 4 will be changed, but at least majority of the issue is fixed.

For convenience, here's the patch: yapf_restrict_lines.diff.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants