Is it possible to call Black as an API?
You could try using format_str: from black import format_str, FileMode res = format_str(“some python code”, mode=FileMode()) print(res)
You could try using format_str: from black import format_str, FileMode res = format_str(“some python code”, mode=FileMode()) print(res)
Currently we have to add –experimental-string-processing tag to it. I think in future versions it will be made default. black -l 80 –preview file.py
Update 2023-09-15: Now VSCode has a Microsoft oficial Black Formatter extension. It will probably solve your problems. Original answer: I use Black from inside VSCode and it rocks. It frees mental cycles that you would spend deciding how to format your code. It’s best to use it from your favorite editor. Just run from the … Read more
The issue is that you need =80 instead of 80 after –line-length for version 1.38.1 and above: –line-length=80
This has been fixed by Black 22.3.0. Versions before that won’t work with click 8.1.0. Incompatible with click 8.1.0 (ImportError: cannot import name ‘_unicodefun’ from ‘click’) #2964 E.g.: black.yml python-version: 3.8 – name: install black run: | – pip install black==20.8b1 + pip install black==22.3.0 – name: run black run: | black . –check –line-length … Read more
This issue on the black issue tracker outlines your particular problem pre-commit finds all the python files, then applies pre-commit‘s exclusion, and then passes that list of files to the underlying tools (in this case black) black currently (at the time of writing) will format all files listed on the command line — independent of … Read more
In my situation (select black as the Python Formatting Provider in VS Code Settings), I encountered this warning everytime when I pasting some text into the editor. And the official documentation of VS Code has a solution specifically for it: When using the black formatter, VS Code issues the following warning when pasting source code … Read more
(author of pre-commit here) the framework intentionally does not provide a way to auto-commit modifications. Here’s a few issues which have asked for such: pre-commit/pre-commit#806 pre-commit/pre-commit#747 A comment from one of those issues: pre-commit itself will never touch the staging area. These are good ways to silently break commits. In my mind this is one … Read more
I don’t think there’s an extension directly in Colab. What you could do, though, is to download your notebook, run pip install -U nbqa nbqa black notebook.ipynb and then reupload your (now formatted) notebook to Colab disclaimer: I’m the author of nbQA UPDATE: as of version 21.8b0, black runs directly on notebooks, no third-party tool … Read more
You can use #fmt: on/off as explained in the issue linked. In your case it would look like: # fmt: off np.array( [ [1, 0, 0, 0], [0, -1, 0, 0], [0, 0, 1, 0], [0, 0, 0, -1], ] ) # fmt: on # fmt: off disables formatting for all following lines until formatting … Read more