Last active
July 28, 2025 12:48
-
-
Save cgoldberg/47807509c12474ae543c838325c1e09a to your computer and use it in GitHub Desktop.
Python - workaround for calling black formatter from tox (so success messages don't display in red)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Problem: | |
# When you call the black formatter, it prints all output to standand | |
# error (even when no formatting is required). However, tox colors all | |
# output to standard error in bold red. This results in black's | |
# "All done! ... files left unchanged" message being colored red in | |
# console output (which is very annoying). | |
# | |
# Workaround: | |
# This is a `tox.ini` configuration file that invokes black through a | |
# Python subprocess and redirects output to standard output when | |
# successful so the output isn't displayed in red. | |
# | |
# see: | |
# - black: https://black.readthedocs.io | |
# - tox: https://tox.wiki | |
[testenv] | |
skip_install = true | |
deps = black | |
commands = | |
{envpython} -c "import subprocess as s; import sys; "\ | |
"r = s.run(['black', '--check', '.'], capture_output=True, text=True, encoding='utf-8');"\ | |
"sys.stdout.write(r.stderr) if not r.returncode else sys.stderr.write(r.stderr)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment