Skip to content

Fix bug in IntegerValidator when an array contains both strings and integers, and add tests for IntegerValidator extras #4693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## UNRELEASED

### Updated
- Fixed a bug in integer validation of arrays that threw an error when an array contained a mix of strings and integers.

## [5.23.0] - 2024-07-23

### Updated
Expand Down
3 changes: 2 additions & 1 deletion packages/python/plotly/_plotly_utils/basevalidators.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,8 @@ def validate_coerce(self, v):
invalid_els = [
e
for e in v
if not (self.min_val <= e <= self.max_val) and e not in self.extras
if not (isinstance(e, int) and self.min_val <= e <= self.max_val)
and e not in self.extras
]

if invalid_els:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ def validator_aok(request):
return IntegerValidator("prop", "parent", min=-2, max=10, array_ok=True)


@pytest.fixture
def validator_extras():
return IntegerValidator("prop", "parent", min=-2, max=10, extras=["normal", "bold"])


@pytest.fixture
def validator_extras_aok():
return IntegerValidator(
"prop", "parent", min=-2, max=10, array_ok=True, extras=["normal", "bold"]
)


# ### Acceptance ###
@pytest.mark.parametrize("val", [1, -19, 0, -1234])
def test_acceptance(val, validator):
Expand All @@ -57,6 +69,27 @@ def test_acceptance_min_max(val, validator_min_max):
assert validator_min_max.validate_coerce(val) == approx(val)


# With extras
@pytest.mark.parametrize("val", ["normal", "bold", 10, -2])
def test_acceptance_extras(val, validator_extras):
assert validator_extras.validate_coerce(val) == val


# Test extras for array_ok
@pytest.mark.parametrize("val", [[10, "normal", "bold"], ["normal"], [10, -2], [5]])
def test_acceptance_extras_array(val, validator_extras_aok):
assert validator_extras_aok.validate_coerce(val) == val


# Test rejection by extras
@pytest.mark.parametrize("val", ["invalid value", "different invalid value", -3, 11])
def test_rejection_extras(val, validator_extras):
with pytest.raises(ValueError) as validation_failure:
validator_extras.validate_coerce(val)

assert "Invalid value" in str(validation_failure.value)


@pytest.mark.parametrize(
"val", [-1.01, -10, 2.1, 3, np.iinfo(int).max, np.iinfo(int).min]
)
Expand Down