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

Why set splitted as all elements on new line? #650

Closed
zaz600 opened this issue Dec 27, 2018 · 7 comments
Closed

Why set splitted as all elements on new line? #650

zaz600 opened this issue Dec 27, 2018 · 7 comments

Comments

@zaz600
Copy link

zaz600 commented Dec 27, 2018

Black v18.9b0

Playground link

Options

--line-length=60
--pyi

Input

KEYS1 = {"aaaaa", "aaaaa", "aaaaa", "aaaaa", "aaaaa"}

KEYS2 = {"aaaaa", "aaaaa", "aaaaa", "aaaaa", "aaaaa", "aaaaa"}

Output

KEYS1 = {"aaaaa", "aaaaa", "aaaaa", "aaaaa", "aaaaa"}

KEYS2 = {
    "aaaaa",
    "aaaaa",
    "aaaaa",
    "aaaaa",
    "aaaaa",
    "aaaaa",
}

Expected

Something like this

KEYS1 = {"aaaaa", "aaaaa", "aaaaa", "aaaaa", "aaaaa"}

KEYS2 = {
    "aaaaa", "aaaaa", "aaaaa", "aaaaa", "aaaaa", 
    "aaaaa"
}
@zaz600
Copy link
Author

zaz600 commented Dec 27, 2018

Problem

Base code

KEYS1 = {"aaaaa", "aaaaa", "aaaaa", "aaaaa", "aaaaa"}

Alice make changes

KEYS1 = {"aaaaa", "aaaaa", "aaaaa", "aaaaa", "bbbbb"}

Alice format code with black:

KEYS1 = {"aaaaa", "aaaaa", "aaaaa", "aaaaa", "bbbbb"}

Bob make changes

KEYS1 = {"aaaaa", "aaaaa", "aaaaa", "aaaaa", "aaaaa", "ccccc"}

Bob format code with black:

KEYS1 = {
    "aaaaa",
    "aaaaa",
    "aaaaa",
    "aaaaa",
    "aaaaa",
    "ccccc",
}

We have ugly merge conflict of two changes

@peterjc
Copy link

peterjc commented Jan 9, 2019

Seems related to #601 ?

@nicktimko
Copy link

In your example, the list was made longer, so you could extend that to making the list arbitrarily long. In that case, the solutions would seem to be one of

  • never break lists onto multiple lines
  • always break lists (basically ignoring the line-length)
  • look at the repository history and/or remember the past state of each variable (possibly also be sensitive to the number of committers?)

@reagle
Copy link

reagle commented Jan 23, 2019

Sometimes I have similar structures, such as a list of a couple dozen words, or a hundred integers, and black forces them each to their own line, which is ghastly, as is the case in the third line:

ARTICLES = {'a', 'an', 'the'}
CONJUNCTIONS = {'and', 'but', 'nor', 'or'}
SHORT_PREPOSITIONS = {'among', 'as', 'at', 'by', 'for', 'from', 'in',
                      'of', 'on', 'out', 'per', 'to', 'upon', 'with', }

becomes:

ARTICLES = {"a", "an", "the"}
CONJUNCTIONS = {"and", "but", "nor", "or"}
SHORT_PREPOSITIONS = {
    "among",
    "as",
    "at",
    "by",
    "for",
    "from",
    "in",
    "of",
    "on",
    "out",
    "per",
    "to",
    "upon",
    "with",
}

Is this what it is supposed to do? Is there something I can do to prevent this?

@nicktimko
Copy link

@reagle I'd do one of the following:

  1. If it's small-ish and you want to keep it with other code
# fmt: off
LONG_LIST_OF_STUFF = { ... }
# fmt: on
  1. If it's large-ish, put those vars in their own source file and either:

    • prefix the file with # fmt: off
    • add the file to tool.black.exclude in pyproject.toml.
  2. If it's gigantic, I'd store it in another format (text, JSON, etc.) and load it.

@reagle
Copy link

reagle commented Jan 23, 2019

@nicktimko, thanks. I had searched the documentation for "disable" and "suspend" but hadn't come across # fmt: off.

@zsol
Copy link
Collaborator

zsol commented Feb 14, 2019

As explained in the comments above, this is the intended behavior, and you can opt code out using # fmt: off and # fmt: on pairs.

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

No branches or pull requests

5 participants