Skip to content

Commit 4e57805

Browse files
author
Jon M. Mease
committed
Don't normalize color string during coercion
We still make strings lowercase and remove spaces during validation, but now we store the original string internally
1 parent 6f6672e commit 4e57805

File tree

3 files changed

+12
-26
lines changed

3 files changed

+12
-26
lines changed

_plotly_utils/basevalidators.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -924,17 +924,16 @@ def perform_validate_coerce(v, allow_number=None):
924924
return None
925925
else:
926926
# Remove spaces so regexes don't need to bother with them.
927-
v = v.replace(' ', '')
928-
v = v.lower()
927+
v_normalized = v.replace(' ', '').lower()
929928

930-
if ColorValidator.re_hex.fullmatch(v):
929+
if ColorValidator.re_hex.fullmatch(v_normalized):
931930
# valid hex color (e.g. #f34ab3)
932931
return v
933-
elif ColorValidator.re_rgb_etc.fullmatch(v):
932+
elif ColorValidator.re_rgb_etc.fullmatch(v_normalized):
934933
# Valid rgb(a), hsl(a), hsv(a) color
935934
# (e.g. rgba(10, 234, 200, 50%)
936935
return v
937-
elif v in ColorValidator.named_colors:
936+
elif v_normalized in ColorValidator.named_colors:
938937
# Valid named color (e.g. 'coral')
939938
return v
940939
else:

_plotly_utils/tests/validators/test_color_validator.py

+7-20
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ def validator_aok_colorscale():
3131
['red', 'BLUE', 'rgb(255, 0, 0)', 'hsl(0, 100%, 50%)', 'hsla(0, 100%, 50%, 100%)',
3232
'hsv(0, 100%, 100%)', 'hsva(0, 100%, 100%, 50%)'])
3333
def test_acceptance(val, validator: ColorValidator):
34-
if isinstance(val, str):
35-
assert validator.validate_coerce(val) == str.replace(val.lower(), ' ', '')
36-
else:
37-
assert validator.validate_coerce(val) == val
34+
assert validator.validate_coerce(val) == val
3835

3936

4037
# ### Rejection by type ###
@@ -64,10 +61,7 @@ def test_rejection(val, validator: ColorValidator):
6461
['red', 'BLUE', 23, 15, 'rgb(255, 0, 0)', 'hsl(0, 100%, 50%)', 'hsla(0, 100%, 50%, 100%)',
6562
'hsv(0, 100%, 100%)', 'hsva(0, 100%, 100%, 50%)'])
6663
def test_acceptance_colorscale(val, validator_colorscale: ColorValidator):
67-
if isinstance(val, str):
68-
assert validator_colorscale.validate_coerce(val) == str.replace(val.lower(), ' ', '')
69-
else:
70-
assert validator_colorscale.validate_coerce(val) == val
64+
assert validator_colorscale.validate_coerce(val) == val
7165

7266

7367
# ### Rejection by type ###
@@ -100,14 +94,11 @@ def test_rejection_colorscale(val, validator_colorscale: ColorValidator):
10094
['hsva(0, 100%, 100%, 50%)']])
10195
def test_acceptance_aok(val, validator_aok: ColorValidator):
10296
coerce_val = validator_aok.validate_coerce(val)
97+
10398
if isinstance(val, (list, np.ndarray)):
104-
expected = np.array(
105-
[str.replace(v.lower(), ' ', '') if isinstance(v, str) else v for v in val],
106-
dtype=coerce_val.dtype)
107-
assert np.array_equal(coerce_val, expected)
99+
assert np.array_equal(coerce_val, val)
108100
else:
109-
expected = str.replace(val.lower(), ' ', '') if isinstance(val, str) else val
110-
assert coerce_val == expected
101+
assert coerce_val == val
111102

112103

113104
# ### Rejection ###
@@ -134,13 +125,9 @@ def test_rejection_aok(val, validator_aok: ColorValidator):
134125
def test_acceptance_aok_colorscale(val, validator_aok_colorscale: ColorValidator):
135126
coerce_val = validator_aok_colorscale.validate_coerce(val)
136127
if isinstance(val, (list, np.ndarray)):
137-
expected = np.array(
138-
[str.replace(v.lower(), ' ', '') if isinstance(v, str) else v for v in val],
139-
dtype=coerce_val.dtype)
140-
assert np.array_equal(coerce_val, expected)
128+
assert np.array_equal(list(coerce_val), val)
141129
else:
142-
expected = str.replace(val.lower(), ' ', '') if isinstance(val, str) else val
143-
assert coerce_val == expected
130+
assert coerce_val == val
144131

145132

146133
# ### Rejection ###

_plotly_utils/tests/validators/test_colorscale_validator.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def test_coercion_named(named_colorscale, validator: ColorscaleValidator):
5555
])
5656
def test_acceptance_array(val, validator: ColorscaleValidator):
5757
# Compute expected (tuple of tuples where color is lowercase with no spaces)
58-
expected = tuple([tuple([e[0], str.replace(e[1].lower(), ' ', '')]) for e in val])
58+
expected = tuple([tuple([e[0], e[1]]) for e in val])
5959
assert validator.validate_coerce(val) == expected
6060

6161

0 commit comments

Comments
 (0)