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

Catch pydantic validation errors with one validation type #2

Open
ketozhang opened this issue Mar 30, 2023 · 4 comments
Open

Catch pydantic validation errors with one validation type #2

ketozhang opened this issue Mar 30, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@ketozhang
Copy link
Owner

I wonder if it is possible to modify the AsdfPydanticModel to raise the same ValidationError as ASDF does? That way libraries reading asdf files will on need to worry about catching one error type.
@WilliamJamieson

@ketozhang ketozhang added the enhancement New feature or request label Mar 30, 2023
@ketozhang
Copy link
Owner Author

ASDF's error is a direct use of jsonschema's which is not a subclass of a standard type

from jsonschema import ValidationError

Pydantic's error is a subclass of TypeError

from pydantic.error_wrappers import ValidationError

I really would love to use ExceptionGroup (new in Python 3.11) for this because there are situations where I want to catch only pydantic validation error.

@WilliamJamieson
Copy link

WilliamJamieson commented Mar 30, 2023

Maybe asdf should define a AsdfValidationError which inherits from both TypeError and jsonschema.ValidationError? I think that would be a reasonable improvement on asdf's part.

Conversely, since this is such a new library you could just require Python 3.11 to use it.

@ketozhang
Copy link
Owner Author

Desired result:

from asdf import ValidationError

try:
    af.open(...)
except ValidationError:
    ...

After Python 3.11,

from asdf import ValidationError
# Very verbose, could forward this asdf_pydantic
from pydantic.error_wrappers import ValidationError as PydanticValidationError  

try:
    af.open(...)
except* ValidationError:
    ...
except* PydanticValidationError:
   ...

@ketozhang
Copy link
Owner Author

Good idea on inheriting TypeError. I'd keep the same name ValidationError for compatibilty.

Conversely, since this is such a new library you could just require Python 3.11 to use it.

I will consider it; I have things in Roman that are 3.10.

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

No branches or pull requests

2 participants