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
13
votes
5 answers

Python black formatter for vscode not formatting

I'm trying to use black as a formatter for Python on VS Code on Ubuntu 20.04 but it's not working on auto save. I've selected black in Python>Formatting:Provider. I'm using prettier as my default formatter for which I added a .prettierignore,…
VectorXY
  • 349
  • 1
  • 3
  • 12
13
votes
2 answers

Black (Python) Ignore Rule

I feel Black is doing something not compliant (with my Organisation), so I am trying to ignore certain rules. Example below and a related link PEP 8: whitespace before ':' My Organisation (Coding Standards) does not give priority to what Black…
Srinath Ganesh
  • 2,496
  • 2
  • 30
  • 60
12
votes
2 answers

Disable Black formatting of dict expression within mapping comprehensions

I'm currently researching Black as our default formatter, but, I'm having some edge cases that don't format well and I want to know if there's a way to get the result I want. Black's documentation partially explores my problem, I have a dictionary…
Nitzan
  • 1,669
  • 3
  • 19
  • 33
11
votes
1 answer

Python black formatter conflict with rule flake8 W503 in VSCode

Anytime there is an inline assertion rule to be verified against a bool statement, using the python black formatter in VSCode will break the line causing flake8 to warn about rule W503 line break before binary operatorflake8(W503) assert ( …
Antonio Santoro
  • 827
  • 1
  • 11
  • 29
11
votes
1 answer

Git pre-commit hooks keeps modifying files even after I have staged previously modified files

I'm running git pre-commit and running black as one of the hooks. Now when I run commit, black fails and says: All done! ✨ ✨ 15 files reformatted, 1 file left unchanged. I reviewed the reformatted files and I'm fine with them. So I stage those…
rrlamichhane
  • 1,435
  • 2
  • 18
  • 34
10
votes
3 answers

How can I apply black code formatting on save?

I would like to apply black whenever I save a Python file in Sublime Text 3. How can I do that? (Bonus points if there is a quick way to disable it)
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
9
votes
2 answers

GitLab CI Python black formatter says: would reformat, whereas running black does not reformat

When I run GitLab CI on this commit with this gitlab-ci.yml: stages: - format - test black_formatting: image: python:3.6 stage: format before_script: # Perform an update to make sure the system is up to date. - sudo apt-get…
a.t.
  • 2,002
  • 3
  • 26
  • 66
9
votes
2 answers

Can python black formatter use tabulators instead of spaces?

I want to use the python black code reformatter in my pre-commit hooks, but in my company they decided to use tabulators in python code instead of spaces. So I tried to change blacks configuration, but I haven't seen any option to use tabulators,…
Sven Rieke
  • 217
  • 1
  • 7
9
votes
2 answers

How to preserve git author line-by-line when using a code formatter?

We are sick of linting. So we want to use black in our project. Unfortunately, it changes almost every other line in our project which would make us loose most of our authorship information. We use annotate in pycharm or git blame a lot to figure…
pascalwhoop
  • 2,984
  • 3
  • 26
  • 40
8
votes
2 answers

How to automatically break long string constants in Python code using Black formatter?

Python formatting guidelines, the famous PEP8 recommends no line longer than 79 chars. I can easily auto-format my code to a max line length with the Black Formatter, but it does not break long strings. The linter will still complain about a long…
neves
  • 33,186
  • 27
  • 159
  • 192
7
votes
1 answer

What is the difference between `poetry run black myscript.py` and `black myscript.py`?

Based on the poetry docs: Likewise if you have command line tools such as pytest or black you can run them using poetry run pytest The suggested way to use black is: poetry run black myscript.py However, I do not notice any difference in…
zabop
  • 6,750
  • 3
  • 39
  • 84
7
votes
1 answer

Can Black be configured to ignore imports?

I am switching over to using Black for all my Python projects from now on. Till now I had been using autopep8 as my auto-formatter and isort to sort my imports. But while using Black I found out that Black alone does the job of isort too alongside…
Arafat Khan
  • 777
  • 1
  • 10
  • 24
7
votes
2 answers

PyCharm: Run `black -S` on region

We are not ready to automatically format the whole source code with black. But from time to time I would like to execute black -S on a region via PyCharm. There is a hint in the docs how to run black (or black -S (what I like)) on the whole file.…
guettli
  • 25,042
  • 81
  • 346
  • 663
7
votes
3 answers

The Black Formatter - Python

I just started using the Black Formatter module with VSCode everything was going well till I just noticed that it uses double quotes over single quotes which I already was using in my code.. and it overrode that.. So, is there an Black Argument that…
Muhammad Hawash
  • 126
  • 1
  • 1
  • 10
7
votes
0 answers

How to deal with ugly code formatter output? (Black)

I'm currently experimenting with the Black code formatter for Python. In >90% of the cases I am happy with the output (with default configs), but it regularly happens, that it formats some lines in a way that seems rather ugly to me. Here an…
nkaenzig
  • 684
  • 7
  • 14