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 SchemaValidationError more helpful by printing expected parameters #2565

Closed
joelostblom opened this issue Mar 20, 2022 · 1 comment · Fixed by #2568
Closed

Make SchemaValidationError more helpful by printing expected parameters #2565

joelostblom opened this issue Mar 20, 2022 · 1 comment · Fixed by #2568

Comments

@joelostblom
Copy link
Contributor

When a non-existing parameter name is used, I think it would be helpful to include the existing parameter names in the error message. For example, when misspelling a parameter name like in the example below it is not immediately clear whether I made a typo or something else went wrong, so the immediate feedback of seeing the correct parameter names would be helpful.

import altair as alt
import pandas as pd

source = pd.DataFrame({
    'a': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'],
    'b': [28, 55, 43, 91, 81, 53, 19, 87, 52]
})

alt.Chart(source).mark_bar().encode(
    x=alt.X("a", scale=alt.Scale(padingOuter=0.5)),
    y=alt.Y("b")
)
SchemaValidationError: Invalid specification

        altair.vegalite.v4.schema.core.Scale, validating 'additionalProperties'

        Additional properties are not allowed ('padingOuter' was unexpected)

I also belive the error message text could be clarified a little. Especially for novices, I don't think it is immediately clear what it means that "Additional properties are not allowed" and it could helpful to update this error message to something like:

SchemaValidationError: Invalid specification

        altair.vegalite.v4.schema.core.Scale, validating 'additionalProperties'

        No such parameter name exists: 'padingOuter'
        
        Existing parameter names are: 'align', 'base', 'bins', 'clamp', 'constant', 'domain',
            'domainMid', 'exponent', 'interpolate', 'nice', 'padding', 'paddingInner', 'paddingOuter',
            'range', 'reverse', 'round', 'scheme', 'type', 'zero', 'kwds'
        
        See the help for altair.Scale` for the full description of these parameters.

We can use the inspect module to retrieve the list of expected parameter names via list(inspect.signature(altair.vegalite.v4.schema.core.Scale).parameters.keys()). I can try to PR this if it sounds like a good idea (it looks like modifying the returned value of altair/utils/schemapi.py is the right place?).

@jakevdp
Copy link
Collaborator

jakevdp commented Mar 21, 2022

Strongly agree - I just don’t know how to do it, because schema validation is done by the jsonschema package and it doesn’t really return anything useful. If you can figure out how to do it robustly I’ll happily review the PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants