0

For a double nested x if condition else y it was legible before black got into the fray. It loses the nice indentations I had placed and now it's just a Wall of Code:

        clause = (
            (f"{self.colname} " if self.colname else "") + self.sql
            if self.sql
            else self.values_filter()
            if self.values is not None
            and len(self.values) > 0
            and (self.colname is not None)
            else self.range_filter()
            if self.range is not None and (self.colname is not None)
            else None
        )

I'm going to break this into separate pieces for expediency but for legacy purposes would like to know if there's some way to get a legible format for this language construct.

WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
  • 2
    Try adding parentheses, maybe it will indent them nicely. – Barmar May 02 '23 at 21:00
  • actually yea, I did add parens to another similar case and that did help. Looking more closely the parens in the above sql are for nested pieces not the main flow – WestCoastProjects May 02 '23 at 21:01
  • `black` is the wrong tool to use if you care about *what* formatting is used, rather than just having *consistent* formatting. – chepner May 02 '23 at 21:12
  • @chepner Maybe so but it's in use in the company projects and that's a fact. So I'm wondering whether I need to dump this language construct – WestCoastProjects May 02 '23 at 21:31
  • I would. Personally, I find this example too long to be particularly readable, at least split into so many short, equally indented lines. – chepner May 03 '23 at 11:55
  • @chepner You might have missed the point: _black_ removes the formatting / indentation that had already been in place. I come from _scala_ and fp background so this construct is used _all the time_ . It just does not work well in python it seems and even less so when _black_ gets into the fray. I went to a traditional assignment block instead though it is another pain point to be unnecessarily verbose that way – WestCoastProjects May 03 '23 at 13:22

1 Answers1

2

Just put a pair of # fmt directives around code you don't want Black to reformat:

# fmt: off
... your carefully formatted construct here ...
# fmt: on
pepoluan
  • 6,132
  • 4
  • 46
  • 76