Reject hex colors that are not 3 or 6 digits - #5729
Open
dylanpulver wants to merge 1 commit into
Open
Conversation
hex_to_rgb computed the section width as len(value) // 3 and sliced in steps of that width, so "#12345" came back as the 5-tuple (1, 2, 3, 4, 5) rather than being rejected. An empty string raised "range() arg 3 must not be zero", which says nothing about colors. The docstring already gives the contract: 3 or 6 digits. This enforces it and keeps the two valid shapes working, with or without the leading '#'. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011M5uTyCU4WcNTsPvGrErDo
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.
Link to issue
Closes #5728
Description of change
hex_to_rgbcomputed its section width aslen(value) // 3and sliced in steps of that width, so any length other than 3 or 6 produced a tuple of the wrong size instead of an error:hex_to_rgb("#12345")returned(1, 2, 3, 4, 5). An empty string raisedValueError: range() arg 3 must not be zero, which says nothing about colors. This enforces the contract the docstring already states.Demo
Before:
After:
Both valid shapes are unchanged, with or without the leading
#:"#ffffff","#fff","aabbcc"and"abc"all behave as before.Testing strategy
Added a parametrized rejection test over
"#12345","#1","#1234567",""and"#", plus a case pinning that the leading#stays optional. Reverting the change fails five of them, so they are not passing vacuously.tests/test_plotly_utils/: 1417 passed, 2 failed. Those 2 aretest_fig_deepcopypyarrow cases that fail identically onmasterin my environment; nothing fails only on this branch.Not addressed here, noted in the issue:
n_colors(low, high, 1)raisesZeroDivisionErrorbecause the increment isdiff / (n_colors - 1). The right behaviour forn=1is your call, so I left it alone rather than guess.This PR was written with AI assistance (Claude Code).