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

yapf with --style=pep8 puts imports on one line even if that breaks the character limit #18

Closed
DRMacIver opened this issue Mar 26, 2015 · 5 comments

Comments

@DRMacIver
Copy link

Consider e.g.

from __future__ import division, print_function, absolute_import, unicode_literals

(there's nothing special about this being a from future import, it's just an example of a long import from my project)

This import line is more than 82 characters but yapf puts it all on one line. It does this even if the code was originally broken across multiple lines.

@DRMacIver
Copy link
Author

Confirmed in master.

@bwendling
Copy link
Member

YAPF won't do much with this for these reasons:

  1. They can't be put on separate lines unless a -newline is used, which YAPF intentionally doesn't use.
  2. We could try to add parentheses around the imports, but I don't like that idea much either. Adding or removing elements in a file can be tricky and I feel it falls outside the scope of a reformatter.
  3. If the import lines were separated, they would have to be ordered alphabetically, which again is outside of the scope of a reformatter.

Both (2) and (3) are more along the lines of a refactoring tool (see the rope library). I could see this as a 2to3 fixer in a "contrib" directory. But even that's iffy...

@DRMacIver
Copy link
Author

Ah, so an important detail that you wouldn't know because I completely failed to mention it, sorry, is that the problem here is that yapf is doing something! My objection isn't that yapf isn't fixing it, it's that yapf is breaking it in the first place. The provided example was originally:

from __future__ import division, print_function, absolute_import, \
    unicode_literals

Which is perfectly valid formatting, and yapf produced the single line variant.

@DRMacIver
Copy link
Author

Incidentally, I already handle getting imports correct and sorted using the isort library. In my current work flow I run that before pyformat, but I could change that to solve this problem by running yapf before isort. It's just a shame to introduce errors that weren't present in the original source.

@bwendling
Copy link
Member

Well, as I mentioned we intentionally don't use backslash-newlines. IMHO, they are a blight on proper code. Note that the import statement above goes against the PEP 8 style guide (https://www.python.org/dev/peps/pep-0008/#imports). Also, I believe that import statements are immune from pylint warnings about going over 80 columns.

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

2 participants