AI junk#3666
Closed
sean-kim05 wants to merge 2 commits into
Closed
Conversation
`style()` (and `secho()`) guarded foreground/background colors with a truthiness check (`if fg:` / `if bg:`), so the valid 256-color index 0 (black) was treated as unset and silently produced uncolored text. The RGB form `(0, 0, 0)` and every nonzero index worked, making the omission an inconsistency rather than intended behavior. Guard on `is not None` instead, matching how the other style attributes in the same function are handled.
style()
Booleans, malformed tuples and out-of-range values now raise the same `TypeError`
Collaborator
|
This is right on time as I am myself digging into the ANSI-side of Click (see: #3653 and #3420). I confirm the issue, which also made me aware of a new class of edge-cases the So I just updated your PR with an additional commit to illustrate all the edge-cases not covered in unittests, and also fix the behavior by validating all colors passed to Now I need a quick pass from other maintainers to check that I did not miss anything but to me this is good to merged in the upcoming 8.5.0 release. |
kdeldycke
added a commit
to kdeldycke/click-extra
that referenced
this pull request
Jul 7, 2026
Backport tests from pallets/click#3666
Collaborator
|
@davidism do you want me to redo it? |
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.
Summary
click.style()(and thereforesecho()) silently drops the 256-color index0(black) when passed asfgorbg, producing uncolored text.The color arguments are guarded with a truthiness check:
0is a valid 8-bit color index (documented range "[0, 255]") but is falsy, so the guard skips it and no escape is emitted. Every other input works — nonzero indices, named colors, and even RGB black(0, 0, 0)(a truthy tuple) — which makes index0an inconsistency, not a design choice.Reproduction
Fix
Guard on
is not Noneinstead of truthiness, matching how the other style attributes (bold,dim, …) in the same function are already handled.None(the default) stays suppressed, unknown color names still raiseTypeError, and index0is now emitted correctly.The truthiness guards date to the original 2014 implementation when only named colors (all truthy) existed; integer/RGB support was later added inside the same blocks without updating them.
Tests
Added
{"fg": 0}and{"bg": 0}cases to the parametrizedtest_stylingtable intests/test_utils.py. They fail onmainand pass with this change; the full suite is unaffected (1682 passed).