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

Strange placement of one-digit index in boolean expression #2947

Closed
costika1234 opened this issue Mar 22, 2022 · 3 comments
Closed

Strange placement of one-digit index in boolean expression #2947

costika1234 opened this issue Mar 22, 2022 · 3 comments
Labels
F: linebreak How should we split up lines? T: style What do we want Blackened code to look like?

Comments

@costika1234
Copy link

Describe the style change
I've just encountered an odd situation where black places a list index on a completely separately line.

Examples in the current Black style
Under default parameters, the following code snippet

def f(special_chars):
    return (len([c for c in special_chars[1:] if c.isupper()]) > 0) and special_chars[0].islower()

is converted by black 22.1.0 to:

def f(special_chars):
    return (len([c for c in special_chars[1:] if c.isupper()]) > 0) and special_chars[
        0
    ].islower()

Desired style
The above is clearly not ideal as 0 is placed on its own line. I imagined that black would format the original function along the lines of:

def f(special_chars):
    return (
        (len([c for c in special_chars[1:] if c.isupper()]) > 0)
        and special_chars[0].islower()
    )

Additional context
That being said, if the original snippet is changed to:

def f(special_chars):
    return len([c for c in special_chars[1:] if c.isupper()]) > 0 and special_chars[0].islower()

(that is, if we remove the parentheses around the inequality condition), then black formats everything correctly:

def f(special_chars):
    return (
        len([c for c in special_chars[1:] if c.isupper()]) > 0
        and special_chars[0].islower()
    )

Is this a bug or not?

@costika1234 costika1234 added the T: style What do we want Blackened code to look like? label Mar 22, 2022
@JelleZijlstra JelleZijlstra added the F: linebreak How should we split up lines? label Mar 22, 2022
@FichteFoll
Copy link

FichteFoll commented Jul 20, 2022

Looks related to #1094 and #2156.

@wbolster
Copy link
Contributor

this particular example can also be rewritten so that it actually fits on one line 🙃

    return any(c.isupper() for c in special_chars[1:]) and special_chars[0].islower()

anyway, i believe this can be closed as a duplicate of #2156. if there are more than 2 expressions the expression will be split over multiple lines, e.g. with 3

    return (
        any(c.isupper() for c in special_chars[1:])
        and special_chars[0].islower()
        and True
    )

and 4 and so on:

    return (
        any(c.isupper() for c in special_chars[1:])
        and special_chars[0].islower()
        and True
        and True
    )

@felix-hilden
Copy link
Collaborator

Also related to #236. Feel free to close if you'd be happy with any of the proposed duplicate issues being resolved!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: linebreak How should we split up lines? T: style What do we want Blackened code to look like?
Projects
None yet
Development

No branches or pull requests

5 participants