Open KOLANICH opened 4 years ago
How about this?
from astor import to_source
from black import format_str, FileMode
def astor_and_blacken(ast, filename, mode='a', skip_black=False):
src = to_source(ast)
if not skip_black:
src = format_str(src, mode=FileMode(
target_versions=set(),
line_length=119,
is_pyi=False,
string_normalization=False,
))
with open(filename, mode) as f:
f.write(src)
I'd be happy to improve/provide API to make integration with tools such as black is easier, but I have no plans to add support for black by default.
Samuel's snippet looks pretty good to me. Perhaps to_source()
could accept a post generation hook that looks similar to it.
I have no plans to add support for black by default.
I didn't mean actually using black
. I mean to generate code running on which black
will say that it has nothing to do. At least it needs some double quotes + whitespaces in strategic places. It is easier than to reimplement half of black
- black
has to keep user's stuff, we don't because we are generating code from scratch.
I wonder how far away is astor from producing such code. I've been using it in my structured editor prototype and the generated code can be pretty hard to read sometimes, specially within complex functions, primarily because of the lack of whitespace.
I usually postprocess the generated code with
black
. It seems to be inefficient and adds a delay. Should the lib generate code, mostly (except of indentation) identical to the one processed byblack
?