3

This is a minimal example based on my code:

def fn(self):
    Foo() \
        .with_bar(
            Bar()
                .with_baz('Baz')
        )

I find this quite readable, but pycodestyle complains:

stdin:5:17: E131 continuation line unaligned for hanging indent

In almost every case pycodestyle reports issues which, when fixed, improve the readability of the code. However, this time the following code seems to be the only accepted solution:

def fn(self):
    Foo() \
        .with_bar(
            Bar()
            .with_baz('Baz')
        )

This seems much less readable: the indentation is inconsistent with the default continuation indentation used for Foo, and it makes .with_baz('Baz') look like a parameter to with_bar. Is this something I should simply get used to, is it a bug in pycodestyle, or is there an alternative formatting which conserves the general style of breaking up subsequent with_* calls that is PEP 8 compatible?

(Please imagine there are more with methods on both Foo and Bar - the whole thing is a builder pattern which needs to be split into multiple lines to be PEP 8 compatible and pleasant to read. I cannot simply join the lines to fix the issue. Pulling out the Bar as a variable is of course an option, but that's beside the point.)

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
l0b0
  • 55,365
  • 30
  • 138
  • 223
  • I can only agree. I have a similar case where aligning all to the left makes it much less readable. – rimono Apr 11 '22 at 05:11
  • I’m voting to close this question because it's no longer relevant for me. – l0b0 Apr 11 '22 at 07:02

0 Answers0