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

line-length for formatter doesn't follow the E501 lint rules. #9289

Closed
jungwookim opened this issue Dec 27, 2023 · 1 comment
Closed

line-length for formatter doesn't follow the E501 lint rules. #9289

jungwookim opened this issue Dec 27, 2023 · 1 comment
Labels
question Asking for support or clarification

Comments

@jungwookim
Copy link

jungwookim commented Dec 27, 2023

I want to set the proper line length but its behaviour is pretty weird. I've attached the reproducible example code and my configurations below.

$ ruff --version
ruff 0.1.8

$ ruff check a.py
a.py:5:81: E501 Line too long (108 > 80)
a.py:9:81: E501 Line too long (96 > 80)

After I fix the E501 above with new line, I format it with ruff format a.py. It produces line too long again.

In other words, ruff format doesn't do anything with wrap lines even they violate their own rule (i set line-length = 80 but it rolls back)

pyproject.toml

I skipped some unrelated configurations.

[tool.ruff]
line-length = 80
indent-width = 4

# Assume Python 3.10
# NOTE: Need to be same as DEFAULT_PYTHON_TAG in build_tools/w4.bzl.
target-version = "py310"

[tool.ruff.lint]
select = ["E4", "E7", "E9", "F", "E501"]
ignore = []

[tool.ruff.lint.pycodestyle]
max-line-length = 80

example code

# a.py (origin)
def line_too_long_test_fn():
    config_enums = {'DataType': {'TEST_DATA_1': 0, 'TEST_DATA_2': 1}}
    test_data_1 = {
        'LessThan80': 'This is a test string that is less than 80 characters',
        'MoreThan80CharactersMoreThan80Characters': 'This is a test string that is more than 80 characters',
    }
    test_data = {
        config_enums.DataType.TEST_DATA_1: test_data_1.LessThan80,
        config_enums.DataType.TEST_DATA_2: test_data_1.MoreThan80CharactersMoreThan80Characters,
    }

    return test_data
# a.py (I fixed)
def line_too_long_test_fn():
    config_enums = {'DataType': {'TEST_DATA_1': 0, 'TEST_DATA_2': 1}}
    test_data_1 = {
        'MoreThan80CharactersMoreThan80Characters':
            'This is a test string that is more than 80 characters',
    }
    test_data = {
        config_enums.DataType.TEST_DATA_2:
            test_data_1.MoreThan80CharactersMoreThan80Characters,
    }

    return test_data
# a.py (after ruff format)
def line_too_long_test_fn():
    config_enums = {'DataType': {'TEST_DATA_1': 0, 'TEST_DATA_2': 1}}
    test_data_1 = {
        'MoreThan80CharactersMoreThan80Characters': 'This is a test string that is more than 80 characters',
    }
    test_data = {
        config_enums.DataType.TEST_DATA_2: test_data_1.MoreThan80CharactersMoreThan80Characters,
    }

    return test_data

I guess it happens on dictionary data or edge cases or a bug?..?

@charliermarsh
Copy link
Member

Thanks! I believe this is only handled as part of Black's preview style (#8437) -- note that Black does not reformat in this case: https://black.vercel.app/?version=main&state=_Td6WFoAAATm1rRGAgAhARYAAAB0L-Wj4AJ8AQ5dAD2IimZxl1N_WlbvK5V-T0Ww3PRf-11njtYzMrWsaS3TRmKAkAKHBe-gctPasA09OYB17rb6BAdKGuTIrNpp8UTVoUaMUhqWdUHOHIacwiygEsEI1DsgOkazDIUZxfybAW9FQZlu8DJTBJ5q6Gdk_jG_sPsKaU-TIdllvTjRRqobO4UXHxnKlBsfWgUtFURbh_dDhtxESCpTFoJMyFu2DICb__XKdsd372sWNpwwXBjuY43Hyang3a-xjZR517pphtuOiOt9p_UzNn-G8u-vuJ6ZYL-dp1dpEMI_CC2A937-CbK_SCaTw8ugV5IJWHYCQuE-lMbmvJzVn19GFL3jRSf3xLdpFQ0NgDEBDN2gvwAAABOMzzQ8w--hAAGqAv0EAACT0xdyscRn-wIAAAAABFla -- so I consider it a duplicate of that issue.

Note that this is (to some degree) expected... The formatter uses the line length as a guideline, but no formatter can guarantee that all content will be reformatted within that line length. (We recommend disabling E501 when using the formatter, though strictly speaking it's not considered "incompatible".)

@charliermarsh charliermarsh closed this as not planned Won't fix, can't repro, duplicate, stale Dec 27, 2023
@zanieb zanieb added the question Asking for support or clarification label Dec 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Asking for support or clarification
Projects
None yet
Development

No branches or pull requests

3 participants