Skip to content

Make SchemaValidationError more helpful by printing expected parameters #2565

Closed

Description

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

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

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions