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 3 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
2 changes: 1 addition & 1 deletion packages/python/plotly/_plotly_utils/basevalidators.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ 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 @@ -32,6 +32,13 @@ def validator_max():
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', 'italics'])

# ### Acceptance ###
@pytest.mark.parametrize("val", [1, -19, 0, -1234])
Expand All @@ -56,6 +63,23 @@ def test_rejection_by_value(val, validator):
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'], ['italics'], [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", ['italic', 'bolditalic', -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