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

Possible to format a Python code contained in a string variable? #4147

Closed
misrasaurabh1 opened this issue Jan 10, 2024 · 1 comment
Closed
Labels
T: enhancement New feature or request

Comments

@misrasaurabh1
Copy link

I'm building a developer tool that generates code. The code we want to generate should be formatted according to black. The only way I've found to do that until now has been to write the new file to disk and then call black executable to format the file. But this seems pretty unnecessary and wasteful in time and performance.

It could be much better if I could just call black through some python library/API function and provide it a string variable with the unformatted python code. I would expect it to return a formatted code. Is this something black implements already somehow? If not, this could be pretty useful for developer tools to interface with black directly without involving a disk write.

@misrasaurabh1 misrasaurabh1 added the T: enhancement New feature or request label Jan 10, 2024
@hauntsaninja
Copy link
Collaborator

This is a duplicate of #779

Look at the functions in black/__init__.py, they can do what you want, e.g. see format_str. Just know that Black doesn't currently provide any backward compatibility guarantees for these APIs.

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Jan 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants