You can try splitting the complicated expression into simpler named parts. That should help keep the underlying logic apparent under formatting:
lacks_meta = all(type(base) != meta for base in bases)
found_spec = any(ccccccc.get_specifications(method) for method in dddd.values())
override = lacks_meta and not found_spec
This does however sacrifice some lazy evaluation (found_spec is now evaluated even if lacks_meta is False).
Another alternative would be to use some branching initialization:
override = True
if any(type(base) == meta for base in bases):
override = False
if any(ccccccc.get_specifications(method) for method in dddd.values()):
override = False
This stays just as clear under formatting, though whether this is more or less readable that the original is probably up to the reader. If you need lazy evaluation, you can prefix the if expressions with override and.
For what it’s worth, ruff and black both switch to something more like your preferred style once the number of and’d terms reaches 3 or more:
override = (
all(type(base) != meta for base in bases)
and not any(ccccccc.get_specifications(method) for method in dddd.values())
and foobar()
)
You can even trick black/ruff into using this style by adding a superfluous True and[1]:
override = (
True
and all(type(base) != meta for base in bases)
and not any(ccccccc.get_specifications(method) for method in dddd.values())
)
Though that will probably set off alarms in your linter. Just using # fmt: off would be clearer
At least in Python 3.10+, this has virtually no runtime overhead, since True and gets optimized into a NOP↩︎
foo = (type(base) != meta for base in bases)
bar = (ccccccc.get_specifications(method) for method in dddd.values())
override = all(foo) and not any(bar)
There are lots of ways of doing this that might make more or less sense in context. I expect that I would be more inclined to have something like:
if any(type(base) == meta for base in bases):
override = False
elif any(ccccccc.get_specifications(method) for method in dddd.values()):
override = False
else:
override = True
I expect that I would also have this in a function though so override = False becomes return False.
In my experience the black way of reformatting long lines is never really the right approach because the good way to make those long lines more readable is never as simple as just shifting whitespace around.
v2024.0.0 of the vscode autopep8 extension left it alone, that’s what I have installed. Odd that black crunches it down so much, it usually spreads things out more.