Can python black formatter use tabulators instead of spaces?
Asked Answered
A

3

12

I want to use the python black code reformatter in my pre-commit hooks, but in my company they decided to use tabulators in python code instead of spaces. So I tried to change blacks configuration, but I haven't seen any option to use tabulators, only thing you can influence in is the line length.

Is black so tightly bound to the PEP8 standard that it will never allow tabulators?

Adoree answered 2/12, 2019 at 8:35 Comment(0)
J
10

It is not possible. The Python Black supports only the spaces.

Here is a ticket for it: https://github.com/psf/black/issues/47 (It contains a conversation why Python Black won't support the tabs).

As they said when they closed the ticket:

No, tabs for indentation are the devil. Making this configurable would go against Black's philosophy.

Jaunitajaunt answered 4/5, 2020 at 13:41 Comment(0)
F
2

If Black won't give you an option just post-process the result... 😀

import sys, pathlib, re

def spaces2tabs(t):
    while True:
        t, n = re.subn('^(\t*) {4}', '\\1\t', t, flags=re.MULTILINE)
        if not n: return t

p = pathlib.Path(sys.argv[1])
p.write_text(spaces2tabs(p.read_text()))
Forficate answered 13/7, 2023 at 13:9 Comment(0)
T
1

As explained by @milanbalazs, Black does not support this. You could, however, use Black (with tabs), a fork of Black that uses tabs instead of spaces:

https://pypi.org/project/black-with-tabs/

Warning: Black (with tabs) does NOT provide a separate command line interface from the original Black. It's still just black. So, they cannot coexist (without virtual environments, anyways).

Triennial answered 16/5 at 9:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.