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

Raise the most internal exception when parsing nested dataclasses #83

Closed
wants to merge 1 commit into from

Conversation

hellais
Copy link

@hellais hellais commented Sep 14, 2022

Currently when an exception happens in a nested dataclass object, the caller will only get context about the most high level parent dataclass which triggered the exception. This makes it tricky to understand the root cause of the validation issues.

When a field is invalid, we re-raise the child exception so it's easier to pin-point exactly where it occurred.

Currently when an exception happens in a nested dataclass object, the
caller will only get context about the most high level parent dataclass
which triggered the exception. This makes it tricky to understand the
root cause of the validation issues.

When a field is invalid, we re-raise the child exception so it's easier
to pin-point exactly where it occurred.
@Fatal1ty
Copy link
Owner

Fatal1ty commented Sep 24, 2022

Hey @hellais

I don't like this idea because it will make the context of the outer scope missing. It'll be hard to guess which of the two fields "a" and "b" has got an invalid value:

@dataclass
class Inner(DataClassDictMixin):
    x: int

@dataclass
class Outer(DataClassJSONMixin):
    a: Inner
    b: Inner

obj = Outer.from_dict(
    {
        "a": {"x": 1},
        "b": {"x": "invalid"},
    }
)

This makes it tricky to understand the root cause of the validation issues.

You can read the traceback to find the root cause actually. Given your test case that will be the following:

mashumaro.exceptions.InvalidFieldValue: Field "child" of type MyDataClassWithChild in DataClass has invalid value {'child': {'a': 'invalid', 'b': 1}}
mashumaro.exceptions.InvalidFieldValue: Field "child" of type MyDataClass in MyDataClassWithChild has invalid value {'a': 'invalid', 'b': 1}
mashumaro.exceptions.InvalidFieldValue: Field "a" of type int in MyDataClass has invalid value 'invalid'
ValueError: invalid literal for int() with base 10: 'invalid'

@Fatal1ty
Copy link
Owner

I think it could be better save a reference to the original exception that caused InvalidFieldValue.

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

Successfully merging this pull request may close these issues.

2 participants