Skip to content
New issue

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

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

Already on GitHub? # to your account

Exempt concatenated string literals for black --preview compatibility #32

Closed
andersk opened this issue Sep 12, 2023 · 3 comments · Fixed by #34
Closed

Exempt concatenated string literals for black --preview compatibility #32

andersk opened this issue Sep 12, 2023 · 3 comments · Fixed by #34

Comments

@andersk
Copy link

andersk commented Sep 12, 2023

black --preview enforces parentheses for concatenated string literals within comma-separated expressions:

items = [
    "item",
    "item",
    (
        "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt"
        " ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation"
        " ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in"
        " reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur"
        " sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id"
        " est laborum."
    ),
    "item",
]

These extra parentheses make it much easier to catch bugs where you’re missing commas in a list of strings, but flake8-picky-parentheses flags them as redundant with PAR001. Consider exempting parens around a concatenated string literal followed by a comma.

@robsdedude
Copy link
Owner

Hi and thanks for bringing this up.

I agree that these parenthesis are helpful and am inclined to add an exception for them. The exact rules when to exempt them from the lint and when not to need some thinking on my side (and playing around with black to see the black authors' option on this) .

I'm curious: why would you use a linter like flake8 in conjunction with black? Isn't black supposed to reformat the code, (almost) no questions asked? So why would one want to run a linter after that?

@andersk
Copy link
Author

andersk commented Sep 13, 2023

Linters go much farther than surface-level formatting! See for example the list of rules supported by Ruff (a faster Rust-based successor to Flake8 plus dozens of plugins). They cover a huge spectrum of issues from performance to readability to security, and sometimes help teach you things about obscure corners of Python that you didn’t know you didn’t know.

As for your particular plugin, I used it to find a subtle bug in Zulip that had slipped past Black, Ruff, mypy, and human review:

So I’d love to help improve the logic to eliminate false positives, and then get it ported to Ruff so we can use it in our CI.

@robsdedude
Copy link
Owner

Has been released in version 0.5.1. Thanks again for the input.

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

Successfully merging a pull request may close this issue.

2 participants