Skip to content

Commit 28c0b26

Browse files
authored
Fix incorrect validation error for animate args info array (#1267)
* Added failing test cases * Only perform 2D validation if 2D array is allowed
1 parent bed52f7 commit 28c0b26

File tree

2 files changed

+52
-26
lines changed

2 files changed

+52
-26
lines changed

Diff for: _plotly_utils/basevalidators.py

+23-26
Original file line numberDiff line numberDiff line change
@@ -1808,32 +1808,29 @@ def validate_coerce(self, v):
18081808

18091809
is_v_2d = v and is_array(v[0])
18101810

1811-
if is_v_2d:
1812-
if self.dimensions == 1:
1813-
self.raise_invalid_val(orig_v)
1814-
else: # self.dimensions is '1-2' or 2
1815-
if is_array(self.items):
1816-
# e.g. 2D list as parcoords.dimensions.constraintrange
1817-
# check that all items are there for each nested element
1818-
for i, row in enumerate(v):
1819-
# Check row length
1820-
if not is_array(row) or len(row) != len(self.items):
1821-
self.raise_invalid_val(orig_v[i], [i])
1822-
1823-
for j, validator in enumerate(self.item_validators):
1824-
row[j] = self.validate_element_with_indexed_name(
1825-
v[i][j], validator, [i, j])
1826-
else:
1827-
# e.g. 2D list as layout.grid.subplots
1828-
# check that all elements match individual validator
1829-
validator = self.item_validators[0]
1830-
for i, row in enumerate(v):
1831-
if not is_array(row):
1832-
self.raise_invalid_val(orig_v[i], [i])
1833-
1834-
for j, el in enumerate(row):
1835-
row[j] = self.validate_element_with_indexed_name(
1836-
el, validator, [i, j])
1811+
if is_v_2d and self.dimensions in ('1-2', 2):
1812+
if is_array(self.items):
1813+
# e.g. 2D list as parcoords.dimensions.constraintrange
1814+
# check that all items are there for each nested element
1815+
for i, row in enumerate(v):
1816+
# Check row length
1817+
if not is_array(row) or len(row) != len(self.items):
1818+
self.raise_invalid_val(orig_v[i], [i])
1819+
1820+
for j, validator in enumerate(self.item_validators):
1821+
row[j] = self.validate_element_with_indexed_name(
1822+
v[i][j], validator, [i, j])
1823+
else:
1824+
# e.g. 2D list as layout.grid.subplots
1825+
# check that all elements match individual validator
1826+
validator = self.item_validators[0]
1827+
for i, row in enumerate(v):
1828+
if not is_array(row):
1829+
self.raise_invalid_val(orig_v[i], [i])
1830+
1831+
for j, el in enumerate(row):
1832+
row[j] = self.validate_element_with_indexed_name(
1833+
el, validator, [i, j])
18371834
elif v and self.dimensions == 2:
18381835
# e.g. 1D list passed as layout.grid.subplots
18391836
self.raise_invalid_val(orig_v[0], [0])

Diff for: _plotly_utils/tests/validators/test_infoarray_validator.py

+29
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ def validator_number3_free():
2626
{'valType': 'number', 'min': 0, 'max': 1}], free_length=True)
2727

2828

29+
@pytest.fixture()
30+
def validator_any3_free():
31+
return InfoArrayValidator('prop', 'parent', items=[
32+
{'valType': 'any'},
33+
{'valType': 'any'},
34+
{'valType': 'any'}], free_length=True)
35+
36+
2937
@pytest.fixture()
3038
def validator_number2_2d():
3139
return InfoArrayValidator('prop', 'parent', items=[
@@ -219,6 +227,27 @@ def test_validator_rejection_number3_free_element_value(val, first_invalid_ind,
219227
first_invalid_ind=first_invalid_ind)) in str(validation_failure.value)
220228

221229

230+
# Any3 Tests (free_length=True)
231+
# --------------------------------
232+
# ### Acceptance ###
233+
@pytest.mark.parametrize('val', [
234+
[1, 0, 'Hello'],
235+
(False, 0.99),
236+
np.array([0.1, 0.99]),
237+
[0], [],
238+
[['a', 'list']],
239+
[['a', 'list'], 0],
240+
[0, ['a', 'list'], 1],
241+
])
242+
def test_validator_acceptance_any3_free(val, validator_any3_free):
243+
coerce_val = validator_any3_free.validate_coerce(val)
244+
assert coerce_val == list(val)
245+
246+
# Compute expected
247+
expected = tuple(tuple(el) if isinstance(el, list) else el for el in val)
248+
assert validator_any3_free.present(coerce_val) == expected
249+
250+
222251
# Number2 2D
223252
# ----------
224253
# ### Acceptance ###

0 commit comments

Comments
 (0)