grantjenks / blue

The slightly less uncompromising Python code formatter.
https://blue.readthedocs.io/
Other
393 stars 21 forks source link

leave function-scope imports alone #92

Open ghost opened 1 year ago

ghost commented 1 year ago

I know it's against every styleguide, but black insists on reformatting this function of mine and the result is much worse than if it would just not.

Before:

def run_json_cmd(cmd: Cmd, stderr=stderr) -> Jsonish:
    from shlex import quote
    stderr.write(" ".join(("+",) + tuple(quote(arg) for arg in cmd)))
    stderr.write("\n")

    from subprocess import check_output
    output = check_output(cmd)

    from json import loads
    return loads(output)

After:

def run_json_cmd(cmd: Cmd, stderr=stderr) -> Jsonish:
    from shlex import quote

    stderr.write(" ".join(("+",) + tuple(quote(arg) for arg in cmd)))
    stderr.write("\n")

    from subprocess import check_output

    output = check_output(cmd)

    from json import loads

    return loads(output)

P.S. This is the prelude in both cases, just in case anyone wants it:

from sys import stderr
from typing import Union, List, Dict, Tuple
Jsonish = Union[
    None, int, float, str, bool, List["Jsonish"], Dict[str, "Jsonish"]
]
Cmd = tuple[Arg, ...]