# Nicer formatting to spike Black

**URL:** <https://discuss.python.org/t/nicer-formatting-to-spike-black/62650>\
**Category:** Python Help\
**Created:** [September 2, 2024, 1:25pm UTC](https://discuss.python.org/t/nicer-formatting-to-spike-black/62650 "2024-09-02T13:25:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![petercordia](https://avatars.discourse-cdn.com/v4/letter/p/bb73d2/32.png) [@petercordia](https://discuss.python.org/u/petercordia)\
**Post date:** [September 2, 2024, 1:25pm UTC](https://discuss.python.org/t/nicer-formatting-to-spike-black/62650/1 "2024-09-02T13:25:42Z")

</div>

I’ve got an assignment I could represent as

```python
override = (all(type(base) != meta for base in bases)
            and not any(ccccccc.get_specifications(method) for method in dddd.values()))

```

which would look acceptable to me, but not quite ideal.

But I’m supposed to use black- or ruff-style formatting which turns this into

```python
override = all(type(base) != meta for base in bases) and not any(
    cccccccc.get_specifications(method) for method in dddd.values()
)

```

which I feel hides the logic that’s implemented.

I’d really quite like to have the line break before or after `and`.

I know I shouldn’t make to much of this. But is there perhaps a good solution or improvement I’m missing?

---

<div class="post-metadata">

**Author:** ![bschubert](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bschubert/32/26920_2.png) [@bschubert](https://discuss.python.org/u/bschubert)\
**Post date:** [September 2, 2024, 1:50pm UTC](https://discuss.python.org/t/nicer-formatting-to-spike-black/62650/2 "2024-09-02T13:50:47Z")

</div>

You can try splitting the complicated expression into simpler named parts. That should help keep the underlying logic apparent under formatting:

```python
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:

```python
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:

```python
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\]:

```python
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 😉

* * *

1. At least in Python 3.10+, this has virtually no runtime overhead, since `True and` gets optimized into a `NOP`

---

<div class="post-metadata">

**Author:** ![chepner](https://avatars.discourse-cdn.com/v4/letter/c/22d042/32.png) [@chepner](https://discuss.python.org/u/chepner)\
**Post date:** [September 2, 2024, 2:42pm UTC](https://discuss.python.org/t/nicer-formatting-to-spike-black/62650/3 "2024-09-02T14:42:19Z")

</div>

> [@bschubert](#):
>
> You can try splitting the complicated expression into simpler named parts. That should help keep the underlying logic apparent under formatting:
> 
> ```python
> 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).

You don’t have to call `all` and `any` right away.

```python
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)

```

---

<div class="post-metadata">

**Author:** ![oscarbenjamin](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/oscarbenjamin/32/1209_2.png) [@oscarbenjamin](https://discuss.python.org/u/oscarbenjamin)\
**Post date:** [September 2, 2024, 5:15pm UTC](https://discuss.python.org/t/nicer-formatting-to-spike-black/62650/4 "2024-09-02T17:15:16Z")

</div>

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:

```python
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.

---

<div class="post-metadata">

**Author:** ![Wombat](https://avatars.discourse-cdn.com/v4/letter/w/e480ec/32.png) [@Wombat](https://discuss.python.org/u/Wombat)\
**Post date:** [September 4, 2024, 11:44am UTC](https://discuss.python.org/t/nicer-formatting-to-spike-black/62650/5 "2024-09-04T11:44:27Z")

</div>

Automatic formatters process low level patterns and cannot think at same level as you. Suggest using `fmt: off` and `fmt: on`.

---

<div class="post-metadata">

**Author:** ![moosemaimer](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/moosemaimer/32/15091_2.png) [@moosemaimer](https://discuss.python.org/u/moosemaimer)\
**Post date:** [September 4, 2024, 2:24pm UTC](https://discuss.python.org/t/nicer-formatting-to-spike-black/62650/6 "2024-09-04T14:24:32Z")

</div>

If you’re getting paid by line count, maybe do:

```python
override = (
    all(
        type(base) != meta for base in bases
    )
    and not any(
        ccccccc.get_specifications(method) for method in dddd.values()
    )
)

```

---

<div class="post-metadata">

**Author:** ![bschubert](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bschubert/32/26920_2.png) [@bschubert](https://discuss.python.org/u/bschubert)\
**Post date:** [September 4, 2024, 2:50pm UTC](https://discuss.python.org/t/nicer-formatting-to-spike-black/62650/7 "2024-09-04T14:50:10Z")

</div>

> [@moosemaimer](#):
>
> If you’re getting paid by line count, maybe do:
> 
> ```python
> override = (
> all(
> type(base) != meta for base in bases
> )
> and not any(
> ccccccc.get_specifications(method) for method in dddd.values()
> )
> )
> 
> ```

Hmm, it looks like ruff and black both format that the same as the original code:

```python
override = all(type(base) != meta for base in bases) and not any(
    cccccccc.get_specifications(method) for method in dddd.values()
)

```

Is the version you’re using preserving that layout under formatting? If so, which version(s)?

---

<div class="post-metadata">

**Author:** ![sinoroc](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/sinoroc/32/1607_2.png) [@sinoroc](https://discuss.python.org/u/sinoroc)\
**Post date:** [September 4, 2024, 4:42pm UTC](https://discuss.python.org/t/nicer-formatting-to-spike-black/62650/8 "2024-09-04T16:42:02Z")

</div>

I think maybe you could use comments (`#`) to force black’s hand. Maybe something like this, but I am too lazy to try it:

```python
override = (all(type(base) != meta for base in bases) #
            and not any(ccccccc.get_specifications(method) for method in dddd.values()))

```

---

<div class="post-metadata">

**Author:** ![sinoroc](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/sinoroc/32/1607_2.png) [@sinoroc](https://discuss.python.org/u/sinoroc)\
**Post date:** [September 4, 2024, 4:46pm UTC](https://discuss.python.org/t/nicer-formatting-to-spike-black/62650/9 "2024-09-04T16:46:00Z")

</div>

Ah well, [black does move comments around](https://black.readthedocs.io/en/latest/the_black_code_style/current_style.html#comments). That seems overreaching to me.

> **[Black Playground](https://black.vercel.app/?version=stable&state=_Td6WFoAAATm1rRGAgAhARYAAAB0L-Wj4AEWAKxdAD2IimZxl1N_WmKABr60PyDOTK-tFvkaB566Y-jsL8LROhGatn0-bVUtcHhbRqhFL4BKdHMgpeQOAnRpISYsWiItRS8zFu86i86pvF-b5lbDGhpP7NvrHdOXBZUq7fy-uEnMJ3D4O7SQEz41vcFzbb6ZKwhptw48v3XbVouAG7Ca9YaGciiYNfwxycCiXhRA67sMxAEKzHmA8aqhPivl-CZrVbMAm3yo489qtQAANUGeOd-PLkgAAcgBlwIAAMdsnSCxxGf7AgAAAAAEWVo=)**
>
> Playground for Black, the uncompromising Python code formatter.

---

<div class="post-metadata">

**Author:** ![moosemaimer](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/moosemaimer/32/15091_2.png) [@moosemaimer](https://discuss.python.org/u/moosemaimer)\
**Post date:** [September 4, 2024, 7:23pm UTC](https://discuss.python.org/t/nicer-formatting-to-spike-black/62650/10 "2024-09-04T19:23:22Z")

</div>

> [@bschubert](#):
>
> Is the version you’re using preserving that layout under formatting? If so, which version(s)?

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.
