Skip to content

Commit 9e6a9a4

Browse files
committed
allow non-string extras in flaglist attributes
1 parent 0279560 commit 9e6a9a4

File tree

2 files changed

+23
-33
lines changed

2 files changed

+23
-33
lines changed

Diff for: packages/python/plotly/_plotly_utils/basevalidators.py

+8-13
Original file line numberDiff line numberDiff line change
@@ -1795,8 +1795,6 @@ def __init__(
17951795
self.extras = extras if extras is not None else []
17961796
self.array_ok = array_ok
17971797

1798-
self.all_flags = self.flags + self.extras
1799-
18001798
def description(self):
18011799

18021800
desc = (
@@ -1835,24 +1833,21 @@ def description(self):
18351833
return desc
18361834

18371835
def vc_scalar(self, v):
1836+
if isinstance(v, str):
1837+
v = v.strip()
1838+
1839+
if v in self.extras:
1840+
return v
1841+
18381842
if not isinstance(v, str):
18391843
return None
18401844

18411845
# To be generous we accept flags separated on plus ('+'),
1842-
# or comma (',')
1846+
# or comma (',') and we accept whitespace around the flags
18431847
split_vals = [e.strip() for e in re.split("[,+]", v)]
18441848

18451849
# Are all flags valid names?
1846-
all_flags_valid = all([f in self.all_flags for f in split_vals])
1847-
1848-
# Are any 'extras' flags present?
1849-
has_extras = any([f in self.extras for f in split_vals])
1850-
1851-
# For flaglist to be valid all flags must be valid, and if we have
1852-
# any extras present, there must be only one flag (the single extras
1853-
# flag)
1854-
is_valid = all_flags_valid and (not has_extras or len(split_vals) == 1)
1855-
if is_valid:
1850+
if all(f in self.flags for f in split_vals):
18561851
return "+".join(split_vals)
18571852
else:
18581853
return None

Diff for: packages/python/plotly/_plotly_utils/tests/validators/test_flaglist_validator.py

+15-20
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,41 @@
44
import numpy as np
55

66

7+
EXTRAS = ["none", "all", True, False, 3]
8+
FLAGS = ["lines", "markers", "text"]
9+
710
# Fixtures
811
# --------
9-
@pytest.fixture(params=[None, ["none", "all"]])
12+
@pytest.fixture(params=[None, EXTRAS])
1013
def validator(request):
1114
# Validator with or without extras
12-
return FlaglistValidator(
13-
"prop", "parent", flags=["lines", "markers", "text"], extras=request.param
14-
)
15+
return FlaglistValidator("prop", "parent", flags=FLAGS, extras=request.param)
1516

1617

1718
@pytest.fixture()
1819
def validator_extra():
19-
return FlaglistValidator(
20-
"prop", "parent", flags=["lines", "markers", "text"], extras=["none", "all"]
21-
)
20+
return FlaglistValidator("prop", "parent", flags=FLAGS, extras=EXTRAS)
2221

2322

2423
@pytest.fixture()
2524
def validator_extra_aok():
2625
return FlaglistValidator(
27-
"prop",
28-
"parent",
29-
flags=["lines", "markers", "text"],
30-
extras=["none", "all"],
31-
array_ok=True,
26+
"prop", "parent", flags=FLAGS, extras=EXTRAS, array_ok=True,
3227
)
3328

3429

3530
@pytest.fixture(
3631
params=[
3732
"+".join(p)
38-
for i in range(1, 4)
39-
for p in itertools.permutations(["lines", "markers", "text"], i)
33+
for i in range(1, len(FLAGS) + 1)
34+
for p in itertools.permutations(FLAGS, i)
4035
]
4136
)
4237
def flaglist(request):
4338
return request.param
4439

4540

46-
@pytest.fixture(params=["none", "all"])
41+
@pytest.fixture(params=EXTRAS)
4742
def extra(request):
4843
return request.param
4944

@@ -69,7 +64,7 @@ def test_coercion(in_val, coerce_val, validator):
6964

7065

7166
# ### Rejection by type ###
72-
@pytest.mark.parametrize("val", [21, (), ["lines"], set(), {}])
67+
@pytest.mark.parametrize("val", [(), ["lines"], set(), {}])
7368
def test_rejection_type(val, validator):
7469
with pytest.raises(ValueError) as validation_failure:
7570
validator.validate_coerce(val)
@@ -79,7 +74,7 @@ def test_rejection_type(val, validator):
7974

8075
# ### Rejection by value ###
8176
@pytest.mark.parametrize(
82-
"val", ["", "line", "markers+line", "lin es", "lin es+markers"]
77+
"val", ["", "line", "markers+line", "lin es", "lin es+markers", 21]
8378
)
8479
def test_rejection_val(val, validator):
8580
with pytest.raises(ValueError) as validation_failure:
@@ -144,7 +139,7 @@ def test_acceptance_aok_scalarlist_flaglist(flaglist, validator_extra_aok):
144139
[
145140
["all", "markers", "text+markers"],
146141
["lines", "lines+markers", "markers+lines+text"],
147-
["all", "all", "lines+text", "none"],
142+
["all", "all", "lines+text"] + EXTRAS,
148143
],
149144
)
150145
def test_acceptance_aok_list_flaglist(val, validator_extra_aok):
@@ -158,8 +153,8 @@ def test_acceptance_aok_list_flaglist(val, validator_extra_aok):
158153
"in_val,expected",
159154
[
160155
(
161-
[" lines ", " lines + markers ", "lines ,markers"],
162-
["lines", "lines+markers", "lines+markers"],
156+
[" lines ", " lines + markers ", "lines ,markers", " all "],
157+
["lines", "lines+markers", "lines+markers", "all"],
163158
),
164159
(np.array(["text +lines"]), np.array(["text+lines"], dtype="unicode")),
165160
],

0 commit comments

Comments
 (0)