Skip to content

Don't convert None to 'None' string in a non-strict string array #1249

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 1 commit into from
Oct 30, 2018
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: 4 additions & 1 deletion _plotly_utils/basevalidators.py
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,10 @@ def validate_coerce(self, v):

elif is_simple_array(v):
if not self.strict:
v = [str(e) for e in v]
# Convert all elements other than None to strings
# Leave None as is, Plotly.js will decide how to handle
# these null values.
v = [str(e) if e is not None else None for e in v]

# Check no_blank
if self.no_blank:
Expand Down
18 changes: 13 additions & 5 deletions _plotly_utils/tests/validators/test_string_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ def validator_strict():

@pytest.fixture
def validator_aok():
return StringValidator('prop', 'parent', array_ok=True, strict=True)
return StringValidator('prop', 'parent', array_ok=True, strict=False)


@pytest.fixture
def validator_aok_strict():
return StringValidator('prop', 'parent', array_ok=True, strict=True)

@pytest.fixture
def validator_aok_values():
Expand Down Expand Up @@ -127,7 +131,8 @@ def test_acceptance_aok_scalars(val, validator_aok):
['foo',
['foo'],
np.array(['BAR', ''], dtype='object'),
['baz', 'baz', 'baz']])
['baz', 'baz', 'baz'],
['foo', None, 'bar']])
def test_acceptance_aok_list(val, validator_aok):
coerce_val = validator_aok.validate_coerce(val)
if isinstance(val, np.ndarray):
Expand All @@ -143,16 +148,19 @@ def test_acceptance_aok_list(val, validator_aok):
# ### Rejection by type ###
@pytest.mark.parametrize('val',
[['foo', ()], ['foo', 3, 4], [3, 2, 1]])
def test_rejection_aok(val, validator_aok):
def test_rejection_aok(val, validator_aok_strict):
with pytest.raises(ValueError) as validation_failure:
validator_aok.validate_coerce(val)
validator_aok_strict.validate_coerce(val)

assert 'Invalid element(s)' in str(validation_failure.value)


# ### Rejection by value ###
@pytest.mark.parametrize('val',
[['foo', 'bar'], ['3', '4'], ['BAR', 'BAR', 'hello!']])
[['foo', 'bar'],
['3', '4'],
['BAR', 'BAR', 'hello!'],
['foo', None]])
def test_rejection_aok_values(val, validator_aok_values):
with pytest.raises(ValueError) as validation_failure:
validator_aok_values.validate_coerce(val)
Expand Down