Repository navigation
fix: decide color support per output stream, not solely from stdout - #1123
Merged
Merged
Conversation
`_init_colors` only checked `sys.stdout.isatty()`, but almost all colored output (log lines, warnings, TIP hints, tracebacks, errors) is written to stderr; only the final "Successfully built" line goes to stdout. This meant `python -m build > log` needlessly disabled color on a tty stderr, while `python -m build 2> log` wrote raw ANSI escapes into the redirected log even though stdout was a tty. `_styles` now holds a stdout/stderr style mapping, and `_cprint` picks the style set for its target stream. `NO_COLOR`/`FORCE_COLOR` still apply uniformly to both streams. Assisted-by: ClaudeCode:claude-sonnet-5
Assisted-by: ClaudeCode:claude-sonnet-5
Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
henryiii
marked this pull request as ready for review
July 2, 2026 14:18
gaborbernat
approved these changes
Jul 2, 2026
layday
approved these changes
Jul 2, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I did a bit of cleanup (see last commit), I think this is ready.
🤖 AI text below 🤖
Summary
Part of the code review in #1116.
_init_colorsdecided color support solely fromsys.stdout.isatty(), but nearly all colored output (log lines, warnings, TIP hints, tracebacks,_error) is written to stderr; only the final "Successfully built ..." message goes to stdout. This meantpython -m build > logneedlessly disabled colors on a tty stderr, andpython -m build 2> logwrote raw ANSI escapes into the log while stdout was a tty._stylesnow holds a{'stdout': ..., 'stderr': ...}mapping instead of a single style set, and_cprint(fmt, msg, file)picks the style set for its target stream (file or sys.stdout).NO_COLORstill disables color on both streams (keeping the existing warning when bothNO_COLORandFORCE_COLORare set), andFORCE_COLORstill enables color on both streams. Otherwise each stream gets color independently based on its own.isatty()._styles.get()use inmain()'s success message to use the stdout styles.Test plan
test_colors, extended;test_cprint_selects_style_per_stream, new) covering stdout-tty/stderr-non-tty and the inverse, confirmed it fails on the old single-stream logic and passes after the fix.uv run --group test pytest(285 passed)uv run --group mypy mypy(clean)prek -a --quiet(clean)