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

Difference in line-length handling between ruff and black when line ends in # #8531

Closed
dwyatte opened this issue Nov 6, 2023 · 7 comments
Closed
Labels
formatter Related to the formatter question Asking for support or clarification

Comments

@dwyatte
Copy link

dwyatte commented Nov 6, 2023

Settings: Default ruff 0.1.3

Note: there is something specific about the example line below in terms of variable string length vs actual assignment value such that reproduction becomes difficult when changing the name/value, but this example captures the behavior I'm interested in raising in this issue

Example

AAAAA_BBBBBBB_CCCC = "xxxxxxxx_xxxxxxxxxx_xxx_xxxxxxxx_xx_xxxx_xxxxxxx_xxxx_xxxx_xxxx_xxxxxxx_xxxx_xxxxx"

When the line above is suffixed with a # e.g., to disable linting / formatting, black will not break it:
https://black.vercel.app/?version=stable&state=_Td6WFoAAATm1rRGAgAhARYAAAB0L-Wj4AFEAGhdAD2IimZxl1N_WjPZmMnerqoaitTnd2a09E8Bi61KJWbnQshHGxfdai0l17K2DqAdXFD89RHKQ6jT30nA8Il4tW9g8JRaFzqeaQ-5TntovkAApVkiDMflIUXe64zjHKvCTRtNm3SZQwHmAKLERh2XSkjRAAGEAcUCAAD5YQM5scRn-wIAAAAABFla

Ruff however, will always break this line into

AAAAA_BBBBBBB_CCCC = (
    "xxxxxxxx_xxxxxxxxxx_xxx_xxxxxxxx_xx_xxxx_xxxxxxx_xxxx_xxxx_xxxx_xxxxxxx_xxxx_xxxxx"  #
)

My code has some long constant names that I'd prefer not to split and apparently # noqa: E501 is enough to exclude them from flake8 as well as black's formatting by virtue of how it handles lines ending in #

@zanieb
Copy link
Member

zanieb commented Nov 6, 2023

In general, we're far more careful with comment handling and therefore can wrap lines like that without fear of dropping comments accidentally.

We explicitly exclude pragma comments from the line length #6772

We provide a # fmt: skip pragma to skip formatting of statements, which appears to be what you need here?

@zanieb zanieb added question Asking for support or clarification formatter Related to the formatter labels Nov 6, 2023
@dwyatte
Copy link
Author

dwyatte commented Nov 6, 2023

We provide a # fmt: skip pragma to skip formatting of statements, which appears to be what you need here?

Thanks, I think this will get me what I need although I suppose it's not clear how to support both disabling linting E501 and formatting on a single line -- I wonder if that's something ruff should support since it does both linting and formatting? Similar black feature that was recently implemented: psf/black#3959

Seems this is a known divergence from black as you mention though.

@zanieb
Copy link
Member

zanieb commented Nov 7, 2023

Hm yeah I don't see a good way to do that cc @MichaReiser

@charliermarsh
Copy link
Member

This works as expected, which seems reasonable to me?

AAAAA_BBBBBBB_CCCC = "xxxxxxxx_xxxxxxxxxx_xxx_xxxxxxxx_xx_xxxx_xxxxxxx_xxxx_xxxx_xxxx_xxxxxxx_xxxx_xxxxx"  # fmt: skip  # noqa: E501

@zanieb
Copy link
Member

zanieb commented Nov 7, 2023

@charliermarsh that is formatted for me with

❯ ruff version
ruff 0.1.3 (3e7b92991 2023-10-26)

but it looks fixed in 0.1.4 so @dwyatte perhaps upgrade

@charliermarsh
Copy link
Member

Ah okay, interesting. I'll close for now then. It does require that the skip comes before the noqa, but I think that's okay.

@dwyatte
Copy link
Author

dwyatte commented Nov 7, 2023

Ah okay, interesting. I'll close for now then. It does require that the skip comes before the noqa, but I think that's okay.

Perfect, thanks all!

@MichaReiser MichaReiser added this to the Formatter: Stable milestone Nov 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatter Related to the formatter question Asking for support or clarification
Projects
None yet
Development

No branches or pull requests

4 participants