peterjc / flake8-black

flake8 plugin to run black for checking Python coding style
MIT License
166 stars 10 forks source link

Let black deal with line length #31

Closed serl closed 3 years ago

serl commented 3 years ago

Hello, I just installed flake8 3.9.2, black 21.6b0, flake8-black 0.2.1 and Django 3.2.4 (latest versions). Then I run black and I configured flake8 using the suggested configuration, but it doesn't spark with joy:

$ flake8
./test_project/settings.py:89:89: E501 line too long (91 > 88 characters)

Seems like sometimes black does not really respect its own line length limit, here's line 89:

AUTH_PASSWORD_VALIDATORS = [
    {
        "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
    },
    # ...
]

My solution was to ask politely flake8 to mind its business and leave line lengths to black. Here's a PR for it. Feel free to improve/change/correct/...

peterjc commented 3 years ago

I think black plans to divide overly long strings, which is how I usually see this happen.

However, unlike you, when this happens I want the E501 message.

serl commented 3 years ago

I see your point.

So, how can I edit this line to pass both black and flake8?

peterjc commented 3 years ago

Ignore all E501, add the comment # noqa: E501 to this line, or break the line up - perhaps:

AUTH_PASSWORD_VALIDATORS = [
    {
        "NAME": "django.contrib.auth.password_validation"
        ".UserAttributeSimilarityValidator",
    },
    # ...
]

Or maybe black likes this indentation?

AUTH_PASSWORD_VALIDATORS = [
    {
        "NAME": "django.contrib.auth.password_validation"
                ".UserAttributeSimilarityValidator",
    },
    # ...
]

See also https://github.com/psf/black/issues/2063 https://github.com/psf/black/issues/1314 https://github.com/psf/black/issues/1327 https://github.com/psf/black/issues/2183 and --experimental-string-processing

serl commented 3 years ago

Wow, thanks for all the details! I should have found all of this by myself 😞

Using black --experimental-string-processing, that's the result:

AUTH_PASSWORD_VALIDATORS = [
    {
        "NAME": (
            "django.contrib.auth.password_validation.UserAttributeSimilarityValidator"
        ),
    },
    # ...
]

Which indeed satisfies both flake8 and black. Adding manually the parentheses (then running black without arguments) has the same result.

Now that I put E501 back, it complains about a very long comment I have somewhere else, which is of course out of the scope of black. So yes, this PR is rubbish, I'm closing it! 😆

peterjc commented 3 years ago

Glad to have helped, and thanks for sharing the black --experimental-string-processing output.