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

Bug: linebreak in comparison in parens with comments in conditional is not stable #638

Open
Zac-HD opened this issue Dec 12, 2018 · 4 comments
Labels
C: invalid code Black destroyed a valid Python file F: comments The syntactic kind. Not in the language grammar, always on our minds. Best bugs. T: bug Something isn't working

Comments

@Zac-HD
Copy link
Contributor

Zac-HD commented Dec 12, 2018

Hypothesis is planning to adopt Black (HypothesisWorks/hypothesis#1686), but I noticed a weird linebreak when a condition follows a comment. Originally this was in this example, but the simple repro is as follows:

# Expected form
(
    # comment
    1 == 2
)
# Result of running Black on expected form
(
    # comment
    1
    == 2
)

Operating system: Windows
Python version: 3.6.5
Black version: 18.9b0
Does also happen on master: yes, commit 0b40a7

(and thanks so much to everyone contributing to Black - it's made Python development noticeably more fun by taking formatting entirely out of my brain 😍)

@Zac-HD
Copy link
Contributor Author

Zac-HD commented Dec 16, 2018

Even better: applying Black to this code

if (
    # pre
    (1 == 2)
    # post
): ...

removes the inner parens, which then inserts the linebreak above. This in turn makes Black abort in --safe mode!

This more complex form only occurs if the parens are part of a more complex statement (if, while, for, etc) and does not happen in the 'expected form' above - if inner parens are added, they are retained by Black.

@Zac-HD Zac-HD changed the title Weird linebreak before comparison in parens with comment Bug: linebreak in comparison in parens with comments in conditional is not stable Dec 16, 2018
@zsol zsol added T: bug Something isn't working C: unstable formatting Formatting changed on the second pass labels Feb 14, 2019
@ichard26 ichard26 removed the C: unstable formatting Formatting changed on the second pass label Apr 2, 2021
@ichard26
Copy link
Collaborator

ichard26 commented Apr 2, 2021

Just rechecked, Black is no longer unstable, but is now deleting comments D:

@ichard26 ichard26 added the C: invalid code Black destroyed a valid Python file label Apr 2, 2021
@JelleZijlstra JelleZijlstra added the F: comments The syntactic kind. Not in the language grammar, always on our minds. Best bugs. label May 30, 2021
@JelleZijlstra
Copy link
Collaborator

The example from the first post is no longer unstable. In the second example, Black now removes the # pre comment, which is still a bug.

@cnpryer
Copy link

cnpryer commented Jun 19, 2023

Just rechecked, Black is no longer unstable, but is now deleting comments D:

I noticed this today. With

black, 23.3.0 (compiled: yes)
Python (CPython) 3.11.3

This statement will lose its # First inner comment:

# Some assert
assert (
    # First inner comment
    (1 + 1)  # Test's trailing comment 
    # Below the expression
    # Last inner comment
), "Some string"  # Message's trailing comment
# Below the assert

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: invalid code Black destroyed a valid Python file F: comments The syntactic kind. Not in the language grammar, always on our minds. Best bugs. T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants