altair icon indicating copy to clipboard operation
altair copied to clipboard

Make `SchemaValidationError` more helpful by printing expected parameters

Open joelostblom opened this issue 3 years ago • 1 comments

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?).

joelostblom avatar Mar 20 '22 19:03 joelostblom

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!

jakevdp avatar Mar 21 '22 02:03 jakevdp