Skip to content
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

Merged
merged 6 commits into from
Nov 21, 2023

Conversation

aktech
Copy link
Collaborator

@aktech aktech commented Sep 25, 2023

Fixes #264

  • This allows to tidy-import to use black with available configuration, like say pyproject.toml
  • Don't ignore width when using black with tidy-import

Comment on lines 495 to 505
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,
)
Copy link
Collaborator

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 :

  1. Safer security wise (no risk to have shell escape, or alike
  2. 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)
Copy link
Collaborator

@Carreau Carreau Nov 6, 2023

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.

@Carreau Carreau force-pushed the black-custom-formatting branch from a3f1fcb to bdd71ee Compare November 13, 2023 18:10
@Carreau Carreau force-pushed the black-custom-formatting branch from a7af375 to 805b584 Compare November 13, 2023 18:25
@Carreau
Copy link
Collaborator

Carreau commented Nov 21, 2023

Merging as failures seem to be issues upstream.

@Carreau Carreau merged commit a60a494 into deshaw:master Nov 21, 2023
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

tidy-imports --black with custom line length (PyInf#11045)
2 participants