Skip to content

Commit 5e2d3d8

Browse files
authored
Merge pull request #4693 from marthacryan/extras-tests
Fix bug in IntegerValidator when an array contains both strings and integers, and add tests for IntegerValidator extras
2 parents bd1dd66 + 75ac930 commit 5e2d3d8

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## UNRELEASED
6+
7+
### Updated
8+
- Fixed a bug in integer validation of arrays that threw an error when an array contained a mix of strings and integers.
9+
510
## [5.23.0] - 2024-07-23
611

712
### Updated

packages/python/plotly/_plotly_utils/basevalidators.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,8 @@ def validate_coerce(self, v):
950950
invalid_els = [
951951
e
952952
for e in v
953-
if not (self.min_val <= e <= self.max_val) and e not in self.extras
953+
if not (isinstance(e, int) and self.min_val <= e <= self.max_val)
954+
and e not in self.extras
954955
]
955956

956957
if invalid_els:

packages/python/plotly/_plotly_utils/tests/validators/test_integer_validator.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ def validator_aok(request):
3333
return IntegerValidator("prop", "parent", min=-2, max=10, array_ok=True)
3434

3535

36+
@pytest.fixture
37+
def validator_extras():
38+
return IntegerValidator("prop", "parent", min=-2, max=10, extras=["normal", "bold"])
39+
40+
41+
@pytest.fixture
42+
def validator_extras_aok():
43+
return IntegerValidator(
44+
"prop", "parent", min=-2, max=10, array_ok=True, extras=["normal", "bold"]
45+
)
46+
47+
3648
# ### Acceptance ###
3749
@pytest.mark.parametrize("val", [1, -19, 0, -1234])
3850
def test_acceptance(val, validator):
@@ -57,6 +69,27 @@ def test_acceptance_min_max(val, validator_min_max):
5769
assert validator_min_max.validate_coerce(val) == approx(val)
5870

5971

72+
# With extras
73+
@pytest.mark.parametrize("val", ["normal", "bold", 10, -2])
74+
def test_acceptance_extras(val, validator_extras):
75+
assert validator_extras.validate_coerce(val) == val
76+
77+
78+
# Test extras for array_ok
79+
@pytest.mark.parametrize("val", [[10, "normal", "bold"], ["normal"], [10, -2], [5]])
80+
def test_acceptance_extras_array(val, validator_extras_aok):
81+
assert validator_extras_aok.validate_coerce(val) == val
82+
83+
84+
# Test rejection by extras
85+
@pytest.mark.parametrize("val", ["invalid value", "different invalid value", -3, 11])
86+
def test_rejection_extras(val, validator_extras):
87+
with pytest.raises(ValueError) as validation_failure:
88+
validator_extras.validate_coerce(val)
89+
90+
assert "Invalid value" in str(validation_failure.value)
91+
92+
6093
@pytest.mark.parametrize(
6194
"val", [-1.01, -10, 2.1, 3, np.iinfo(int).max, np.iinfo(int).min]
6295
)

0 commit comments

Comments
 (0)