Black: Black should always explode multiline collection literals

Created on 20 Apr 2018  路  7Comments  路  Source: psf/black

Operating system: macOS
Python version: 3.6.1
Black version: 18.4a2
Does also happen on master: Yes

After running black, I've gotten code formatted like:

class Classifier(db.ModelBase):

    __tablename__ = "trove_classifiers"
    __table_args__ = (
        Index("trove_class_class_idx", "classifier"), Index("trove_class_id_idx", "id")
    )

    __repr__ = make_repr("classifier")

    id = Column(Integer, primary_key=True, nullable=False)
    classifier = Column(Text, unique=True)
    deprecated = Column(Boolean, nullable=False, server_default=sql.false())
    l2 = Column(Integer)
    l3 = Column(Integer)
    l4 = Column(Integer)
    l5 = Column(Integer)

While I understand the intention is for a sequence that can fit on one line to be on one line, this list here obviously can't fit on one line (it's been broken over 3 lines!) but it can only fit onto one line when you remove part of the syntax that makes it a list/tuple/whatever.

I think that it would be easier to read if the detection of a multi-line sequence was more aggressive, either it should all fit onto one line, or it should be formatted like:

class Classifier(db.ModelBase):

    __tablename__ = "trove_classifiers"
    __table_args__ = (
        Index("trove_class_class_idx", "classifier"),
        Index("trove_class_id_idx", "id"),
    )

    __repr__ = make_repr("classifier")

    id = Column(Integer, primary_key=True, nullable=False)
    classifier = Column(Text, unique=True)
    deprecated = Column(Boolean, nullable=False, server_default=sql.false())
    l2 = Column(Integer)
    l3 = Column(Integer)
    l4 = Column(Integer)
    l5 = Column(Integer)
design

All 7 comments

TBH I am highly sympathetic to this argument.

This turns out to be a common complaint.

The current behavior is pretty core to how Black formats all bracket content. This includes collection literals (tuples, lists, sets, dicts), function calls and function signatures.

Since we probably already have to bail on this behavior because of isort, it makes sense to evolve Black such that:

  • function calls and function signatures have the immediate step;
  • collection literals and from_imports don't.

What do you think? (cc @hynek)

I lament that it will complicate my code but that's my problem.

I lament that it will complicate my code but that's my problem.

I'm just here to ruin your day with complexity ;)

Well, as you know, I鈥檓 a big fan of exploding everything. :) I鈥檓 very bad at reading long lines (especially with dicts).

The import part of this is now done. For collection literals I have to figure out a consistent way of doing it.

Just to verify, this covers:

env = Environment(
    loader=FileSystemLoader(dir_name),
    extensions=[
-            "jinja2.ext.i18n",
-            "warehouse.utils.html.ClientSideIncludeExtension",
+            "jinja2.ext.i18n", "warehouse.utils.html.ClientSideIncludeExtension"
    ],
    cache_size=0,
)

Right?

Yup.

Was this page helpful?
0 / 5 - 0 ratings