Skip to content
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

Make error messages on typos and missing/incorrect data types more informative #2824

Merged
merged 10 commits into from
Feb 21, 2023

Conversation

joelostblom
Copy link
Contributor

When trying to reference a column from a dataframe and a typo is made, the error message suggests that there is an issue with the type specification rather than the column name itself:

import altair as alt
from vega_datasets import data

alt.Chart(data.cars()).mark_point().encode(x='Horsepowers')

Old error

ValueError: Horsepowers encoding field is specified without a type; the type cannot be inferred because it does not match any column in the data.

I think that message is incorrect and unhelpful, and I often see learners struggle with understanding that they have made a typo. This PR suggests replacing the error message with this more informative alternative:

New error

ValueError: "Horsepowers" cannot be found.

  • If you are trying to reference a column in the dataframe, there is likely a typo in the column name.
  • If you are trying to reference a field from a transform, there is either a typo in the field name or the data type specification is missing (Altair can only infer data types from dataframe columns).

Similarly, when making a typo in the encoding data type, the error message could also be made more informative:

alt.Chart(data.cars()).mark_point().encode(x='Horsepower:Qi')

Old error
ValueError: Horsepower:Qi encoding field is specified without a type; the type cannot be inferred because it does not match any column in the data.

This PR changes that to the following:

New error

ValueError: "Qi" is not one of the valid encoding data types: O, N, Q, T, G.
For more details, see https://altair-viz.github.io/altair-docs/user_guide/encodings/index.html#encoding-data-types. (If you are trying to use a column name that contains a colon, prefix it with a backslash, \:)

Note that this second change means that it is now required to escape colons in column names and although this is is a breaking change I think it makes it more consistent instead of having colon in column names sometimes work depending on whether the part after the colon happens to equal an Altair data type or not.

altair/utils/core.py Outdated Show resolved Hide resolved
altair/vegalite/v5/schema/channels.py Show resolved Hide resolved
@joelostblom
Copy link
Contributor Author

@mattijn How do you think we should proceed here? I think the improved error messages would be nice to have in the release candidate.

@joelostblom
Copy link
Contributor Author

Merging in this too, yay better error messages everywhere!

@joelostblom joelostblom merged commit 389b87b into master Feb 21, 2023
binste added a commit to binste/altair that referenced this pull request Feb 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants