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

Black does not respect line length in case of long attribute chaining #1237

Closed
adarshk7 opened this issue Jan 20, 2020 · 1 comment
Closed
Labels
F: linetoolong Black makes our lines too long R: duplicate This issue or pull request already exists T: style What do we want Blackened code to look like?

Comments

@adarshk7
Copy link

Describe the bug

In a case where there is code that chains a lot of attributes within a parentheses block (to avoid using backslashes) Black fails to respect the line length supplied to it.

To Reproduce

  1. Take this file
class BlackTestingClass():
    def __getattr__(self):
        return self


def some_function():
    btc = BlackTestingClass()
    (
        btc
        .attribute1.something
        .attribute2.something
        .attribute3.something
        .attribute4.something
        .attribute5.something
    )
  1. Run Black on it with these arguments : black . --diff -l 100
  2. See error
@@ -1,16 +1,11 @@
-class BlackTestingClass():
+class BlackTestingClass:
     def __getattr__(self):
         return self


 def some_function():
     btc = BlackTestingClass()
     (
-        btc
-        .attribute1.something
-        .attribute2.something
-        .attribute3.something
-        .attribute4.something
-        .attribute5.something
+        btc.attribute1.something.attribute2.something.attribute3.something.attribute4.something.attribute5.something
     )

Expected behavior

The existing code in the block should be kept as it is (if it doesn't break any formatting rules otherwise) as it is probably a well thought out pattern by a developer or at least black should not put everything to the same line which exceeds the line-length (in this case the output line is 113 characters whereas the limit supplied to black is 100.)

Environment

  • Version: 19.10b0
  • OS and Python version: macOS 10.14.6/Python 3.7.4

Does this bug also happen on master?

Yes (Verified from the playground)

Additional context

As to where one might encounter this kind of block in real life, mocking and patching specific things in tests might get fairly nested (especially with unittests .return_value) or something like long ORM queries with SQLAlchemy.

@adarshk7 adarshk7 added the T: bug Something isn't working label Jan 20, 2020
@JelleZijlstra JelleZijlstra added F: linetoolong Black makes our lines too long T: style What do we want Blackened code to look like? and removed T: bug Something isn't working labels May 30, 2021
@JelleZijlstra
Copy link
Collaborator

Duplicate of #510

@JelleZijlstra JelleZijlstra marked this as a duplicate of #510 Jan 29, 2022
@ichard26 ichard26 added the R: duplicate This issue or pull request already exists label Jan 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: linetoolong Black makes our lines too long R: duplicate This issue or pull request already exists T: style What do we want Blackened code to look like?
Projects
None yet
Development

No branches or pull requests

3 participants