Questions tagged [python-black]

Black is the uncompromising Python code formatter.

Black is the uncompromising Python code formatter.

Blackened code looks the same regardless of the project you're reading. Formatting becomes transparent after a while and you can focus on the content instead.

91 questions
200
votes
3 answers

Black formatter - Ignore specific multi-line code

I would like to ignore a specific multi-line code by black python formatter. Particularly, this is used for np.array or matrix construction which turned ugly when formatted. Below is the example. np.array( [ [1, 0, 0, 0], [0, -1,…
Darren Christopher
  • 3,893
  • 4
  • 20
  • 37
170
votes
6 answers

ImportError: cannot import name '_unicodefun' from 'click'

When running our lint checks with the Python Black package, an error comes up: ImportError: cannot import name '_unicodefun' from 'click' (/Users/robot/.cache/pre-commit/repo3u71ccm2/py_env-python3.9/lib/python3.9/site-packages/click/init.py)` In…
zerocewl
  • 11,401
  • 6
  • 27
  • 53
58
votes
3 answers

Code formatter like nb_black for google colab

I know that for jupyter notebooks and jupyter lab, there are available code formatter extensions such as nb_blackor blackcellmagic. However when I installed them, it doesn't seem to work on google colab. Do you know if there are any native option in…
Syndo rik
  • 755
  • 1
  • 7
  • 15
51
votes
20 answers

Formatter black is not working on my VSCode...but why?

I have started using Python and Django and I am very new in this field. And, this is my first time to ask a question here...I do apologise in advance if there is a known solution to this issue... When I installed and set VSCode formatter 'black'…
51
votes
5 answers

black as pre-commit hook always fails my commits

I'm trying to use pre-commit to manage Black as a Git pre-commit hook, but I must be doing it wrong. In my pre-commit config file I have: - repo: https://github.com/psf/black rev: 19.3b0 hooks: - id: black What I'm expecting to…
Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
49
votes
5 answers

Black does not support "Format Selection" command

I need to indent my python file in VS Code. I followed the normal procedure: On Windows Shift + Alt + F On Mac Shift + Option + F On Linux Ctrl + Shift + I But my question is every time when I try to format python file it it says Black does not…
user13700226
49
votes
2 answers

Exclude some files on running black using pre-commit

I want to configure black in pre-commit and exclude precommit from checking any migrations folder. My pyproject.toml looks like this [tool.black] line-length = 79 target-version = ['py37'] include = '\.pyi?$' exclude = ''' ( /( \.eggs |…
Suyash Salampuria
  • 765
  • 2
  • 6
  • 9
44
votes
4 answers

VS Code Python + Black formatter arguments - python.formatting.blackArgs

I'm using the May 2018 Python extension (released June 2018) for VS Code 1.23.1 on Windows, python 3.6 via Anaconda, conda installing black from conda-forge into my conda environment. In my user settings.json I have the…
Rafael Zayas
  • 2,061
  • 1
  • 18
  • 20
32
votes
3 answers

Python: Black doesn't wrap long lines

I am using black==20.8b1. I have a long string like: return f"{self.name}, a {adjective.to_name()} {kin_string}{self._type.to_name()} who works for the {target.get_relationship_target_string()}." I run: $ black -l 80 . -t py38 All done! ✨ ✨ 2…
gruszczy
  • 40,948
  • 31
  • 128
  • 181
29
votes
4 answers

Is it possible to call Black as an API?

Say I want to use black as an API, and do something like: import black black.format("some python code") Formatting code by calling the black binary with Popen is an alternative, but that's not what I'm asking.
laike9m
  • 18,344
  • 20
  • 107
  • 140
23
votes
1 answer

Python Black code formatter doesn't format docstring line length?

I am running the Black code formatter against a Python script however it doesn't reformat the line length for docstrings. For example, given the following code: def my_func(): """ This is a really long docstring. This is a really long…
Daniel O.
  • 583
  • 5
  • 15
17
votes
2 answers

Flake8 disable all formatting rules

I use flake8 for linting and black for formatting. flake8 produces warnings on execution rules and formatting rules: I only want to see execution rules. I don't care about formatting rules because they'll automatically get fixed by black…
Brian Malehorn
  • 2,627
  • 14
  • 15
16
votes
2 answers

Black --exclude argument not excluding desired file(s)

Let's say I have the following python file exclude_from_black.py in the root of my project: print('I want single quotes') I'm trying to exclude this file from black reformatting but none of the following commands seem to work: black…
Johnny Metz
  • 5,977
  • 18
  • 82
  • 146
15
votes
4 answers

Can I undo the formatting style changes that Black makes to my Python code?

Does the Python formatting tool Black have an option for undoing the formatting changes it made after it's been run? Or does it assume I'm using source control and making my own backups? This is as of December 2019 and Black version 19.3b0.
Al Sweigart
  • 11,566
  • 10
  • 64
  • 92
14
votes
2 answers

Why are migrations files often excluded from code formatting?

We're applying Black code style to a django project. In all the tutorials / examples I find (such as in django cookiecutter and this blog), I keep seeing django's migrations files excluded from the linter. But to my mind, these are still python…
thclark
  • 4,784
  • 3
  • 39
  • 65
1
2 3 4 5 6 7