black icon indicating copy to clipboard operation
black copied to clipboard

Line too long: Dictionary key-value pair

Open ghost opened this issue 7 years ago • 7 comments

Hey,

with black, version 18.6b4 I am getting two lines within dict merged resulting in line exceeding provided limit.

black --py36 --line-length 80 file

gives:

@ settings.py:153 @ REST_FRAMEWORK = {
    "DEFAULT_PERMISSION_CLASSES": (
        'some stuff here'
    ),
-    "DEFAULT_PAGINATION_CLASS":
-        "rest_framework.pagination.LimitOffsetPagination",
+    "DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.LimitOffsetPagination",
    "PAGE_SIZE": some_value_here,
    "DEFAULT_FILTER_BACKENDS": (
        "django_filters.rest_framework.DjangoFilterBackend",

Line in question is 82 characters long.

Is that a bug or intended behaviour for dicts?

ghost avatar Jul 19 '18 22:07 ghost

Looks like black is failing to put parenthesis in there. Smells like a bug, thanks for reporting! May be related to #275 and #330

zsol avatar Jul 22 '18 14:07 zsol

In the mean time, if you put the value in parentheses manually, Black will keep them. We need to figure out a way to do it nicely automatically without over-extending.

ambv avatar Aug 17 '18 14:08 ambv

Can confirm this still happens even with experimental string processing. Here's a playground for convenience.

felix-hilden avatar Jun 16 '21 13:06 felix-hilden

Any update on a solution for this bug?

berzi avatar May 10 '22 07:05 berzi

Any updates will be posted here!

felix-hilden avatar May 10 '22 08:05 felix-hilden

@felix-hilden I am running into this as well. I haven't contributed to black before, but I am thinking to contribute. I have two questions: has this been prioritized? and is this a good candidate for a first contribution? It hasn't been resolved since 2018, could be due to complexity?

govindrai avatar May 18 '22 02:05 govindrai

@govindrai You're welcome to give it a go! It hasn't been prioritised, and could be a hard one (based on no real info though), but we'll try to help if you create a PR draft 👍

felix-hilden avatar May 18 '22 07:05 felix-hilden

This is fixed on main in preview mode:

% black -c '''REST_FRAMEWORK = {
    "DEFAULT_PERMISSION_CLASSES": (
        "some stuff here"
    ),
    "DEFAULT_PAGINATION_CLASS":
        "rest_framework.pagination.LimitOffsetPagination",
    "PAGE_SIZE": some_value_here,
}''' --preview -l 80
REST_FRAMEWORK = {
    "DEFAULT_PERMISSION_CLASSES": "some stuff here",
    "DEFAULT_PAGINATION_CLASS": (
        "rest_framework.pagination.LimitOffsetPagination"
    ),
    "PAGE_SIZE": some_value_here,
}

JelleZijlstra avatar Dec 19 '22 04:12 JelleZijlstra