pythonのformatterのblackに標準入力でコードを渡す。

hmm

$ pip install black
$ cat 00hello.py | black
No paths given. Nothing to do 😴

なるほど。ファイルの指定を"-"にして渡す。

https://github.com/ambv/black/blob/250ba7f04b300df284ba80cd4bb4122b45b41efb/black.py#L465-L467

def reformat_one(
    src: Path, fast: bool, write_back: WriteBack, mode: FileMode, report: "Report"
) -> None:

# ...

        if not src.is_file() and str(src) == "-":
            if format_stdin_to_stdout(fast=fast, write_back=write_back, mode=mode):
                changed = Changed.YES

動いた。

$ cat 00hello.py | black -
def hello(x, y, z):
    pass


def bye(x, y, *, z=None):
    pass


def sleep(x, y, *, z=None):
    pass


def sleep2(x, y, *, z=None):
    pass


def ffffffffffffffffffffffffffffffffffffffffffffffffff(
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
    yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,
):
    pass
reformatted -
All done! ✨ 🍰 ✨
1 file reformatted.

ちなみに

ちなみにautopep8とyapfとblackの変換結果のgist