Black: formatting of fluent method calls

31 Views Asked by At

I'm using the black formatter, and I'm having trouble getting it to respect my wishes for fluent method chains like this:

            result = (
                data
                .some_method()
                .some_other_method(arg1, arg2)
                .yet_another_method_with_longer_name(arg3)
            )

, which gets reformatted by black as

            result = data.some_method().some_other_method(arg1, arg2).yet_another_method_with_longer_name(arg3)

.

The docs (at https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#call-chains) are pretty terse on the subject, saying

Black formats those by treating dots that follow a call or an indexing operation like a very low priority delimiter. It’s easier to show the behavior than to explain it.

It may be easier for the writer, but not for the reader - I don't get it. =)

I'm looking for something like the "magic trailing comma" to tell the formatter that I desire one method call per line, so please format it that way. But of course, an actual comma is not valid in this situation, it would turn the right-hand-side into a tuple.

I tend to think of #fmt: on/off as a very last resort, so I would prefer to avoid using them if at all possible. Because of the libraries I'm using (and writing), there would be a lot of them in the code.

0

There are 0 best solutions below