-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow custom formatting options with black #266
Conversation
lib/python/pyflyby/_importstmt.py
Outdated
black_cmd = [ | ||
f'black --line-length {str(params.max_line_length)} -c "{str_to_format.strip()}"', | ||
] | ||
try: | ||
completed_process = subprocess.run( | ||
black_cmd, | ||
shell=True, | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
universal_newlines=True, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You likely should not do that, in general every-time you see shell=True
you should step back and think security.
The first argument to run
is a list of your program arguments, unless you really want them parsed by the shell. And Here you are forming a string to get it parsed, while you could have ['black', '--line-length', 'str(params.max_line_length)', ...]
.
This is :
- Safer security wise (no risk to have shell escape, or alike
- More correct (no risk of bas escape in
str_to_format
).
import black | ||
mode = black.FileMode() | ||
return black.format_str(res, mode=mode) | ||
return self.run_black(res, params) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't something along the following work w/o a subprocess ?
from black import read_pyproject_toml, find_pyproject_toml
from click import Context, Command
c = Context(Command("black"))
print(read_pyproject_toml(c, None, None))
c.default_map
mode = FileMode(...)
I'm pretty sure you can find some inspiration in https://github.com/akaihola/darker/blob/e96018f086383a2dcfaab6825945fbee08daca2a/src/darker/black_diff.py#L97 that use black by importing it.
That would avoid an expensive subprocess and the subprocess parsing issues.
a3f1fcb
to
bdd71ee
Compare
a7af375
to
805b584
Compare
Merging as failures seem to be issues upstream. |
Fixes #264
tidy-import
to use black with available configuration, like saypyproject.toml
black
withtidy-import