From 3f23ce7f161845848c2d532a885b6cfc820f2b94 Mon Sep 17 00:00:00 2001 From: Martha Cryan Date: Thu, 25 Jul 2024 17:26:21 -0700 Subject: [PATCH 1/6] Add tests for IntegerValidator extras --- .../tests/validators/test_integer_validator.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/python/plotly/_plotly_utils/tests/validators/test_integer_validator.py b/packages/python/plotly/_plotly_utils/tests/validators/test_integer_validator.py index 8b7cb1dbf48..442d8e0eded 100644 --- a/packages/python/plotly/_plotly_utils/tests/validators/test_integer_validator.py +++ b/packages/python/plotly/_plotly_utils/tests/validators/test_integer_validator.py @@ -32,6 +32,9 @@ 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", extras=['normal', 'bold']) # ### Acceptance ### @pytest.mark.parametrize("val", [1, -19, 0, -1234]) @@ -56,6 +59,18 @@ 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']) +def test_acceptance_extras(val, validator_extras): + assert validator_extras.validate_coerce(val) == val + +# Test rejection by extras +@pytest.mark.parametrize("val", ['italic', 'bolditalic']) +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] From ff56b21197479352cc63513f0cc87c2566932a9c Mon Sep 17 00:00:00 2001 From: Martha Cryan Date: Fri, 26 Jul 2024 09:44:36 -0700 Subject: [PATCH 2/6] Add further tests --- .../tests/validators/test_integer_validator.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/python/plotly/_plotly_utils/tests/validators/test_integer_validator.py b/packages/python/plotly/_plotly_utils/tests/validators/test_integer_validator.py index 442d8e0eded..ca7fb5fc75e 100644 --- a/packages/python/plotly/_plotly_utils/tests/validators/test_integer_validator.py +++ b/packages/python/plotly/_plotly_utils/tests/validators/test_integer_validator.py @@ -34,7 +34,11 @@ def validator_aok(request): @pytest.fixture def validator_extras(): - return IntegerValidator("prop", "parent", extras=['normal', 'bold']) + 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]) @@ -60,12 +64,17 @@ 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']) +@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", [['normal', 'bold'], ['italics']]) +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']) +@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) From 3f13e9a5f40c6c867581ff678bdd1ad750a771a6 Mon Sep 17 00:00:00 2001 From: Martha Cryan Date: Fri, 26 Jul 2024 10:50:05 -0700 Subject: [PATCH 3/6] Fix bug and update test --- packages/python/plotly/_plotly_utils/basevalidators.py | 2 +- .../_plotly_utils/tests/validators/test_integer_validator.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/python/plotly/_plotly_utils/basevalidators.py b/packages/python/plotly/_plotly_utils/basevalidators.py index e0ce3e4f2de..2e081fdbdd8 100644 --- a/packages/python/plotly/_plotly_utils/basevalidators.py +++ b/packages/python/plotly/_plotly_utils/basevalidators.py @@ -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: diff --git a/packages/python/plotly/_plotly_utils/tests/validators/test_integer_validator.py b/packages/python/plotly/_plotly_utils/tests/validators/test_integer_validator.py index ca7fb5fc75e..566df0fd441 100644 --- a/packages/python/plotly/_plotly_utils/tests/validators/test_integer_validator.py +++ b/packages/python/plotly/_plotly_utils/tests/validators/test_integer_validator.py @@ -38,7 +38,7 @@ def validator_extras(): @pytest.fixture def validator_extras_aok(): - return IntegerValidator("prop", "parent", min=-2, max=10, array_ok=True, extras=[['normal', 'bold'], ['italics']]) + return IntegerValidator("prop", "parent", min=-2, max=10, array_ok=True, extras=['normal', 'bold', 'italics']) # ### Acceptance ### @pytest.mark.parametrize("val", [1, -19, 0, -1234]) @@ -69,7 +69,7 @@ def test_acceptance_extras(val, validator_extras): assert validator_extras.validate_coerce(val) == val # Test extras for array_ok -@pytest.mark.parametrize("val", [['normal', 'bold'], ['italics']]) +@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 From 9986b2d6b6a5586b4ae4b15eea8216a2521b8cd6 Mon Sep 17 00:00:00 2001 From: Martha Cryan Date: Fri, 26 Jul 2024 15:46:48 -0700 Subject: [PATCH 4/6] Remove mention of italics --- .../tests/validators/test_integer_validator.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/python/plotly/_plotly_utils/tests/validators/test_integer_validator.py b/packages/python/plotly/_plotly_utils/tests/validators/test_integer_validator.py index 566df0fd441..545a356eda0 100644 --- a/packages/python/plotly/_plotly_utils/tests/validators/test_integer_validator.py +++ b/packages/python/plotly/_plotly_utils/tests/validators/test_integer_validator.py @@ -38,7 +38,7 @@ def validator_extras(): @pytest.fixture def validator_extras_aok(): - return IntegerValidator("prop", "parent", min=-2, max=10, array_ok=True, extras=['normal', 'bold', 'italics']) + return IntegerValidator("prop", "parent", min=-2, max=10, array_ok=True, extras=['normal', 'bold']) # ### Acceptance ### @pytest.mark.parametrize("val", [1, -19, 0, -1234]) @@ -69,12 +69,12 @@ 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]]) +@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", ['italic', 'bolditalic', -3, 11]) +@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) From 223c5cc88bccfdb22504b1c430ede303000e4779 Mon Sep 17 00:00:00 2001 From: Martha Cryan Date: Fri, 26 Jul 2024 15:49:31 -0700 Subject: [PATCH 5/6] Add CHANGELOG --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 20c880922af..b43a738776c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From 75ac9307bb9eaf8382083135f7d616f8879c23d9 Mon Sep 17 00:00:00 2001 From: Martha Cryan Date: Fri, 26 Jul 2024 16:13:59 -0700 Subject: [PATCH 6/6] Black formatting --- .../plotly/_plotly_utils/basevalidators.py | 3 ++- .../validators/test_integer_validator.py | 19 ++++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/packages/python/plotly/_plotly_utils/basevalidators.py b/packages/python/plotly/_plotly_utils/basevalidators.py index 2e081fdbdd8..21731afad43 100644 --- a/packages/python/plotly/_plotly_utils/basevalidators.py +++ b/packages/python/plotly/_plotly_utils/basevalidators.py @@ -950,7 +950,8 @@ def validate_coerce(self, v): invalid_els = [ e for e in v - if not (isinstance(e, int) and 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: diff --git a/packages/python/plotly/_plotly_utils/tests/validators/test_integer_validator.py b/packages/python/plotly/_plotly_utils/tests/validators/test_integer_validator.py index 545a356eda0..9a01fde7e41 100644 --- a/packages/python/plotly/_plotly_utils/tests/validators/test_integer_validator.py +++ b/packages/python/plotly/_plotly_utils/tests/validators/test_integer_validator.py @@ -32,13 +32,18 @@ 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']) + 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']) + return IntegerValidator( + "prop", "parent", min=-2, max=10, array_ok=True, extras=["normal", "bold"] + ) + # ### Acceptance ### @pytest.mark.parametrize("val", [1, -19, 0, -1234]) @@ -63,24 +68,28 @@ 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]) +@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]]) +@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]) +@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] )