altair: "'ValidationError' object has no attribute 'json_path'" on interactive legend example in RC

I have trouble passing the condition passing a point selection into the encode method. The interactive legend example does not work on my end. I tried both RC1 and RC2, and in both cases, I get this:

'ValidationError' object has no attribute 'json_path'

… which seems to have been connected with issue #2715.

Is this a known issue? I do not get the error on version 4 with the old

Thank you, Marian

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 15 (10 by maintainers)

Most upvoted comments

Thank you @nrchtct! That thread you linked does not seem very reassuring to me in terms of whether this update will happen on the Deepnote side anytime soon.

So if there were a straightforward way to solve this on the Altair side, that would also be helpful for my use of Altair in Deepnote (for teaching).

I learned in a thread on their forum that “jsonschema is one of the core packages of Deepnote that make the product work”. So this might take some time. In case, there are other Altair users on Deepnote they may run into similar issues when filtering along interactive legends.

My guess is that Deepnote imports jsonschema for its own use before executing user code. It would be worth asking their support if there’s a way around that, or if they could update the version.

Yes, per #3039.

Thanks I can reproduce this error with

jsonschema version 3.2.0 altair version 5.0.0rc2.

reproducible spec:

import altair as alt
from vega_datasets import data

source = data.unemployment_across_industries.url

selection = alt.selection_point(fields=['series'], bind='legend')

alt.Chart(source).mark_area().encode(
    alt.X('yearmonth(date):T').axis(domain=False, format='%Y', tickSize=0),
    alt.Y('sum(count):Q').stack('center').axis(None),
    alt.Color('series:N').scale(scheme='category20b'),
    opacity=alt.condition(selection, alt.value(1), alt.value(0.2))
).add_params(
    selection
)

The full error message is

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[2], line 8
      4 source = data.unemployment_across_industries.url
      6 selection = alt.selection_point(fields=['series'], bind='legend')
----> 8 alt.Chart(source).mark_area().encode(
      9     alt.X('yearmonth(date):T').axis(domain=False, format='%Y', tickSize=0),
     10     alt.Y('sum(count):Q').stack('center').axis(None),
     11     alt.Color('series:N').scale(scheme='category20b'),
     12     opacity=alt.condition(selection, alt.value(1), alt.value(0.2))
     13 ).add_params(
     14     selection
     15 )

File D:\Software\Miniconda3\envs\stable\lib\site-packages\altair\vegalite\v5\api.py:2306, in _EncodingMixin.encode(self, *args, **kwargs)
   2303 @utils.use_signature(core.FacetedEncoding)
   2304 def encode(self, *args, **kwargs) -> Self:
   2305     # Convert args to kwargs based on their types.
-> 2306     kwargs = utils.infer_encoding_types(args, kwargs, channels)
   2308     # get a copy of the dict representation of the previous encoding
   2309     # ignore type as copy method comes from SchemaBase
   2310     copy = self.copy(deep=["encoding"])  # type: ignore[attr-defined]

File D:\Software\Miniconda3\envs\stable\lib\site-packages\altair\utils\core.py:730, in infer_encoding_types(args, kwargs, channels)
    726     except jsonschema.ValidationError:
    727         # our attempts at finding the correct class have failed
    728         return obj
--> 730 return {
    731     encoding: _wrap_in_channel_class(obj, encoding)
    732     for encoding, obj in kwargs.items()
    733 }

File D:\Software\Miniconda3\envs\stable\lib\site-packages\altair\utils\core.py:731, in <dictcomp>(.0)
    726     except jsonschema.ValidationError:
    727         # our attempts at finding the correct class have failed
    728         return obj
    730 return {
--> 731     encoding: _wrap_in_channel_class(obj, encoding)
    732     for encoding, obj in kwargs.items()
    733 }

File D:\Software\Miniconda3\envs\stable\lib\site-packages\altair\utils\core.py:725, in infer_encoding_types.<locals>._wrap_in_channel_class(obj, encoding)
    720 cls = classes["value"] if "value" in obj else classes["field"]
    722 try:
    723     # Don't force validation here; some objects won't be valid until
    724     # they're created in the context of a chart.
--> 725     return cls.from_dict(obj, validate=False)
    726 except jsonschema.ValidationError:
    727     # our attempts at finding the correct class have failed
    728     return obj

File D:\Software\Miniconda3\envs\stable\lib\site-packages\altair\utils\schemapi.py:877, in SchemaBase.from_dict(cls, dct, validate, _wrapper_classes)
    875     _wrapper_classes = cls._default_wrapper_classes()
    876 converter = _FromDict(_wrapper_classes)
--> 877 return converter.from_dict(dct, cls)

File D:\Software\Miniconda3\envs\stable\lib\site-packages\altair\utils\schemapi.py:1040, in _FromDict.from_dict(self, dct, cls, schema, rootschema, default_class)
   1038 for key, val in dct.items():
   1039     if key in props:
-> 1040         val = self.from_dict(val, schema=props[key], rootschema=rootschema)
   1041     kwds[key] = val
   1042 return cls(**kwds)

File D:\Software\Miniconda3\envs\stable\lib\site-packages\altair\utils\schemapi.py:1023, in _FromDict.from_dict(self, dct, cls, schema, rootschema, default_class)
   1021 for possible_schema in schemas:
   1022     try:
-> 1023         validate_jsonschema(dct, possible_schema, rootschema=rootschema)
   1024     except jsonschema.ValidationError:
   1025         continue

File D:\Software\Miniconda3\envs\stable\lib\site-packages\altair\utils\schemapi.py:76, in validate_jsonschema(spec, schema, rootschema, raise_error)
     74 if errors:
     75     leaf_errors = _get_leaves_of_error_tree(errors)
---> 76     grouped_errors = _group_errors_by_json_path(leaf_errors)
     77     grouped_errors = _subset_to_most_specific_json_paths(grouped_errors)
     78     grouped_errors = _deduplicate_errors(grouped_errors)

File D:\Software\Miniconda3\envs\stable\lib\site-packages\altair\utils\schemapi.py:142, in _group_errors_by_json_path(errors)
    140 errors_by_json_path = collections.defaultdict(list)
    141 for err in errors:
--> 142     errors_by_json_path[err.json_path].append(err)
    143 return dict(errors_by_json_path)

AttributeError: 'ValidationError' object has no attribute 'json_path'

Is this something that you recognize @binste?