You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd like to be able to give a better error message with code snippet for ValidationErrors.
Using the 'Class as union of subclasses' example:
from dataclasses import dataclass
from typing import Any, Optional, Union
from apischema import deserialize, deserializer, identity, serializer
from apischema.conversions import Conversion
class Base:
_union: Any = None
# You can use __init_subclass__ to register new subclass automatically
def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
# Deserializers stack directly as a Union
deserializer(Conversion(identity, source=cls, target=Base))
# Only Base serializer must be registered (and updated for each subclass) as
# a Union, and not be inherited
Base._union = cls if Base._union is None else Union[Base._union, cls]
serializer(
Conversion(identity, source=Base, target=Base._union, inherited=False)
)
@dataclass
class Foo(Base):
foo: Optional["Foo"]
assert deserialize(Base, {"foo": {"foo": {"foo": 0}}}) == Foo(Foo(Foo(0)))
In VSCode editor this gives a red squiggly line under the 0 in the assertion Foo(Foo(Foo(0))).
The ValidationError in the terminal is:
Traceback (most recent call last):
File "/dls/science/users/sfx44126/devel/ibek/src/ibek/apischemaerrorexample.py", line 29, in <module>
assert deserialize(Base, {"foo": {"foo": {"foo": 0}}}) == Foo(Foo(Foo(0)))
File "/venv/lib/python3.10/site-packages/apischema/utils.py", line 400, in wrapper
return wrapped(*args, **kwargs)
File "/venv/lib/python3.10/site-packages/apischema/deserialization/__init__.py", line 891, in deserialize
return deserialization_method(
File "apischema/deserialization/methods.pyx", line 655, in apischema.deserialization.methods.ConversionMethod.deserialize
File "apischema/deserialization/methods.pyx", line 656, in apischema.deserialization.methods.ConversionMethod.deserialize
File "apischema/deserialization/methods.pyx", line 1176, in apischema.deserialization.methods.ConversionMethod_deserialize
File "apischema/deserialization/methods.pyx", line 1305, in apischema.deserialization.methods.DeserializationMethod_deserialize
File "apischema/deserialization/methods.pyx", line 1078, in apischema.deserialization.methods.ObjectMethod_deserialize
apischema.validation.errors.ValidationError: ValidationError: [{'loc': ['foo'], 'err': 'expected type null, found object'}, {'loc': ['foo', 'foo'], 'err': 'expected type null, found object'}, {'loc': ['foo', 'foo', 'foo'], 'err': 'expected type object, found integer'}, {'loc': ['foo', 'foo', 'foo'], 'err': 'expected type null, found integer'}]
Is there a way of extracting the culpable code snippet and validation error from this to give a clearer error message?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'd like to be able to give a better error message with code snippet for ValidationErrors.
Using the 'Class as union of subclasses' example:
In VSCode editor this gives a red squiggly line under the
0
in the assertionFoo(Foo(Foo(0)))
.The ValidationError in the terminal is:
Is there a way of extracting the culpable code snippet and validation error from this to give a clearer error message?
Beta Was this translation helpful? Give feedback.
All reactions