-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Unable to trace full error location when calling super().__init__() from custom model #7498
Comments
Hi @alice-luc, I'm guessing that the issue you're having is that the |
Thank you for your reply @sydney-runkle, |
HI @alice-luc, Sure thing. Hopefully this helps: I think the print of
Which indicates that the value of the innermost |
But what if i need it to point to root 'h' field in model G?
|
@alice-luc would you mind explaining what you are actually trying to do? We generally discourage trying to override |
Following up - if you don't override the
|
@sydney-runkle |
@alice-luc is there any particular reason you need to override the |
Let's say i have a house model that has the following structure: FloorModel has ApartmentModel has When im trying to validate large object with an error in KitchenModel.wall, pydantic does not specify which exact child model and field ruining validation, it only traces the path to 'apartments'. |
@alice-luc, could you please send a code sample so that we can reproduce your problem? As long as I don't override BaseModel's |
Just checking, have you tried using |
Sure @sydney-runkle
|
i sure have @adriangb |
Hi @alice-luc, Thanks for the example. The exhaustive errors that you're reporting aren't a unique function of the many levels of nested models you have - that happens with every See below: from pprint import pprint
from typing import List, Union
from pydantic import BaseModel
class ModelA(BaseModel):
a: str
class ModelB(BaseModel):
b: int
class MyModel(BaseModel):
value: List[Union[ModelA, ModelB]]
try:
my_model = MyModel(**{"value": [{'a': "a value"}, {"b": "not an int"}]})
except Exception as e:
pprint(e.errors())
"""
[{'input': {'b': 'not an int'},
'loc': ('value', 1, 'ModelA', 'a'),
'msg': 'Field required',
'type': 'missing',
'url': 'https://errors.pydantic.dev/2.3/v/missing'},
{'input': 'not an int',
'loc': ('value', 1, 'ModelB', 'b'),
'msg': 'Input should be a valid integer, unable to parse string as an integer',
'type': 'int_parsing',
'url': 'https://errors.pydantic.dev/2.3/v/int_parsing'}]
""" We currently have a PR that's a work in progress for supporting functional discriminators, which might help with your use case (see #6915). We also have an open issue (see #7462) calling for increased support for these. In the meantime, you could do something like this with a Let me know if that makes sense / if there's anything else we can do to help! 😄 |
Thank you for reply @sydney-runkle! |
@alice-luc sure thing! I'm going to close this issue in favor of continuing discussion on the PR + issue mentioned above, but we'll be sure to keep in mind the desire to simplify errors coming from validation against Thanks for your questions / prompt responses! 😄 |
Initial Checks
Description
Im trying to reassign annotation of the field "g" to locate the exact field that causes ValidationError. But when i spot the final field, im only able to trace error back up to last init call.
Errors i get:
{
'input': '654f',
'loc': ('g', 0, 'e', 'a'), < -------- no 'h' in location
'msg': 'Input should be a valid integer, unable to parse string as an integer',
'type': 'int_parsing',
'url': 'https://errors.pydantic.dev/2.3/v/int_parsing'
},
{
'input': 'tr',
'loc': ('g', 0, 'e', 'c'),
'msg': 'Extra inputs are not permitted',
'type': 'extra_forbidden',
'url': 'https://errors.pydantic.dev/2.3/v/extra_forbidden'
},
{
'input': {},
'loc': ('g', 0, 'e', 'd'),
'msg': 'Extra inputs are not permitted',
'type': 'extra_forbidden',
'url': 'https://errors.pydantic.dev/2.3/v/extra_forbidden'
}
Expected errors
{
'input': '654f',
'loc': ('h', 'g', 0, 'e', 'a'),
'msg': 'Input should be a valid integer, unable to parse string as an integer',
'type': 'int_parsing',
'url': 'https://errors.pydantic.dev/2.3/v/int_parsing'
},
{
'input': 'tr',
'loc': ('h', 'g', 0, 'e', 'c'),
'msg': 'Extra inputs are not permitted',
'type': 'extra_forbidden',
'url': 'https://errors.pydantic.dev/2.3/v/extra_forbidden'
},
{
'input': {},
'loc': ('h', 'g', 0, 'e', 'd'),
'msg': 'Extra inputs are not permitted',
'type': 'extra_forbidden',
'url': 'https://errors.pydantic.dev/2.3/v/extra_forbidden'
}
Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: