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

Can not use ValidataionError __init__ method #6734

Closed
1 task done
so1n opened this issue Jul 18, 2023 · 8 comments
Closed
1 task done

Can not use ValidataionError __init__ method #6734

so1n opened this issue Jul 18, 2023 · 8 comments
Assignees
Labels
bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable

Comments

@so1n
Copy link

so1n commented Jul 18, 2023

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Can not use ValidataionError init method

Example Code

>>> from pydantic import ValidationError
>>> ValidationError([])
Traceback (most recent call last):
  File "/usr/lib/python3.7/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
TypeError: No constructor defined
>>> from pydantic import ValidationError
>>>ValidationError(errors=[])
Traceback (most recent call last):
  File "/usr/lib/python3.7/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
TypeError: No constructor defined
>>> from pydantic import ValidationError
>>> dir(ValidationError)
['__cause__', '__class__', '__context__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__suppress_context__', '__traceback__', 'args', 'error_count', 'errors', 'from_exception_data', 'json', 'title', 'with_traceback']

Python, Pydantic & OS Version

pydantic version: 2.0.3
        pydantic-core version: 2.3.0 release build profile
                 install path: .venv/lib/python3.7/site-packages/pydantic
               python version: 3.7.3 (default, Nov  1 2022, 10:27:39)  [GCC 8.3.0]
                     platform: Linux-5.15.77-amd64-desktop-x86_64-with-Deepin-20.9-apricot
     optional deps. installed: ['typing-extensions']

Selected Assignee: @hramezani

@so1n so1n added bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable labels Jul 18, 2023
@adriangb
Copy link
Member

There is no __init__. You’ll have to use from_err_details

@so1n
Copy link
Author

so1n commented Jul 18, 2023

Thanks for your reply, but I can't find the from_err_details method. Is it the from_exception_data method?

>>> from pydantic import ValidationError
>>> dir(ValidationError)
['__cause__', '__class__', '__context__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__suppress_context__', '__traceback__', 'args', 'error_count', 'errors', 'from_exception_data', 'json', 'title', 'with_traceback']

@adriangb
Copy link
Member

Yes sorry, apologies for the misnomer.

@dineshbvadhia
Copy link

dineshbvadhia commented Aug 10, 2023

@adriangb @so1n Hello. I get the same error (with V2 but not with V1) and don't understand what the fix is. Here is a sample code snippet:

from fastapi.exceptions import HTTPException, RequestValidationError
from pydantic import ValidationError

try:
    this = something('odd')
except:
    raise RequestValidationError(errors=[ValidationError(ValueError('Invalid oddness'), 'odd')])

TypeError: No constructor defined

What is the solution?

@adriangb
Copy link
Member

Use ValidationError.from_err_details.

@dineshbvadhia
Copy link

@adriangb I think you meant to say use ValidationError.from_exception_data which gives a TypeError :(

from fastapi.exceptions import HTTPException, RequestValidationError
from pydantic import ValidationError

try:
    this = something('odd')
except:
    raise RequestValidationError(errors=[ValidationError.from_exception_data(ValueError('Invalid oddness'), 'odd')])

TypeError: argument 'line_errors': 'str' object cannot be converted to 'PyList'

@Kludex
Copy link
Member

Kludex commented Aug 11, 2023

@dineshbvadhia Eh... I recommend setting up any kind of type checker on your setup.

The API documentation may help you: https://docs.pydantic.dev/latest/api/pydantic_core/#pydantic_core._pydantic_core.ValidationError.from_exception_data

@somnam
Copy link

somnam commented Oct 4, 2023

I'm facing this error (TypeError: No constructor defined) when retrieving exception from a Celery task. The error is raised when a pickled ValidataionError is being unpickled. I've found this issue in pydantic-core repo:
pydantic/pydantic-core#959
Is it possible to have __init__ method on ValidataionError?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable
Projects
None yet
Development

No branches or pull requests

6 participants