Skip to content

Commit

Permalink
Display more helpful error message when two fields strings are used i…
Browse files Browse the repository at this point in the history
…n condition (#2979)

* Raise helpful error if a field is used in both branches of a condition

* Add test

* Add changelog entry

* Blacken

* Blacken more

* Improve error message wording

* Apply suggestions from code review

Co-authored-by: Mattijn van Hoek <[email protected]>

---------

Co-authored-by: Mattijn van Hoek <[email protected]>
  • Loading branch information
joelostblom and mattijn authored Mar 26, 2023
1 parent 1317f6d commit b27b829
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
9 changes: 7 additions & 2 deletions altair/vegalite/v5/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,8 +773,13 @@ def condition(predicate, if_true, if_false, **kwargs):
# dict in the appropriate schema
if_true = if_true.to_dict()
elif isinstance(if_true, str):
if_true = utils.parse_shorthand(if_true)
if_true.update(kwargs)
if isinstance(if_false, str):
raise ValueError(
"A field cannot be used for both the `if_true` and `if_false` values of a condition. One of them has to specify a `value` or `datum` definition."
)
else:
if_true = utils.parse_shorthand(if_true)
if_true.update(kwargs)
condition.update(if_true)

if isinstance(if_false, core.SchemaBase):
Expand Down
1 change: 1 addition & 0 deletions doc/releases/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Enhancements
- Substantially improved error handling. Both in terms of finding the more relevant error (#2842), and in terms of improving the formatting and clarity of the error messages (#2824, #2568).
- Include experimental support for the DataFrame Interchange Protocol (through ``__dataframe__`` attribute). This requires ``pyarrow>=11.0.0`` (#2888).
- Support data type inference for columns with special characters (#2905).
- Changed error message to be more informative when two field strings are using in a condition (#2979).

Grammar Changes
~~~~~~~~~~~~~~~
Expand Down
14 changes: 14 additions & 0 deletions tests/utils/test_schemapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,20 @@ def test_chart_validation_errors(chart_func, expected_error_message):
chart.to_dict()


def test_multiple_field_strings_in_condition():
selection = alt.selection_point()
expected_error_message = "A field cannot be used for both the `if_true` and `if_false` values of a condition. One of them has to specify a `value` or `datum` definition."
with pytest.raises(ValueError, match=expected_error_message):
(
alt.Chart(data.cars())
.mark_circle()
.add_params(selection)
.encode(
color=alt.condition(selection, "Origin", "Origin"),
)
).to_dict()


def test_serialize_numpy_types():
m = MySchema(
a={"date": np.datetime64("2019-01-01")},
Expand Down

0 comments on commit b27b829

Please sign in to comment.