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

Improve ValidationError messaging for default values (validate_default=True). #9408

Open
3 of 13 tasks
JohnHind opened this issue May 7, 2024 · 1 comment
Open
3 of 13 tasks

Comments

@JohnHind
Copy link

JohnHind commented May 7, 2024

Initial Checks

  • I have searched Google & GitHub for similar requests and couldn't find anything
  • I have read and followed the docs and still think this feature is missing

Description

This code sample, which contains a minor typo converting an intended default for an integer field into a tuple:

from pydantic import BaseModel, ConfigDict

class MyBaseModel(BaseModel):
    model_config = ConfigDict(validate_default=True)

class Command(MyBaseModel):
    chan: int = 0,
    val: int = 0

validated = Command(val = 'f')

Produces this output:

Traceback (most recent call last):
  File "/home/pi/blinka/BugTest.py", line 11, in <module>
    validated = Command(val = 'f')
                ^^^^^^^^^^^^^^^^^^
  File "/home/pi/blinka/lib/python3.11/site-packages/pydantic/main.py", line 176, in __init__
    self.__pydantic_validator__.validate_python(data, self_instance=self)
pydantic_core._pydantic_core.ValidationError: 2 validation errors for Command
chan
  Input should be a valid integer [type=int_type, input_value=(0,), input_type=tuple]
    For further information visit https://errors.pydantic.dev/2.7/v/int_type
val
  Input should be a valid integer, unable to parse string as an integer [type=int_parsing, input_value='f', input_type=str]
    For further information visit https://errors.pydantic.dev/2.7/v/int_parsing

This is hard to understand because the identifier 'chan' does not appear anywhere in the highlighted traceback (line 11). I suggest that the field identifier should be presented more verbosely, both in the case where it is taken from the constructor argument list and in the case where it is defaulted. Say, something like:

Field: chan (default value from Command schema)
  Input should be a valid integer [type=int_type, input_value=(0,), input_type=tuple]
    For further information visit https://errors.pydantic.dev/2.7/v/int_type
Field: val
  Input should be a valid integer, unable to parse string as an integer [type=int_parsing, input_value='f', input_type=str]
    For further information visit https://errors.pydantic.dev/2.7/v/int_parsing

Suggestion comes out of this thread:
#9389

Affected Components

@sydney-runkle
Copy link
Member

Hi @JohnHind,

Thanks for the feature request. I think this is a reasonble feature request - effectively to be more clear about where the input value is coming from in the error message. That being said, I do find the error message to be quite clear in general, so I wouldn't say this is high on our list of priorities.

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

No branches or pull requests

2 participants