You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Originally posted by SunsetOrange August 24, 2021
I had been trying to locate a bug in my code for an hour or so, which I eventually narrowed down to an incorrectly defined custom validator decorator. I had placed multiple field strings into the decorator call, inside a list rather than as multiple args.
My own mistake, but Pydantic had been throwing a very unhelpful exception stating that my model was not hashable.
The exception was pointing at the class definition line, not the validator decorator.
Exception has occurred: TypeError unhashable type: 'list'
This confused me greatly for a good while as I tried to find a possible location in the model and its super classes where a list was being used as a dictionary key. As this is a mistake that others could easily make (and I think I have made it in the past as well), it would be nice and helpful to output an exception that identifies the mistake and provides helpful feedback. Else wise, the source of the exception is difficult to identify.
I proposing that the following code be added to class_validators.py at line 75.
elif not all([isinstance(field, str) for field in fields]):
raise ConfigError(
"validator fields should be passed as separate string args. Do not pass multiple fields as a list, etc. "
"E.g. usage should be `@validator('<field_name_1>', '<field_name_2>', ...)` "
"NOT `@validator(['<field_name_1>', '<field_name_2>', ...], ...)`"
)
The text was updated successfully, but these errors were encountered:
* Descriptive exception for incorrectly defined validator fields.
Throws a ConfigError wth helpful feedback if a validator is passed incorrectly defined fields.
* Minor grammar correction.
* Expanded example.
* Added test for bad validator decorator fields.
* Added changes .md file.
* Improved wording in error message.
* Test fix.
* Update pydantic/class_validators.py
Encloses expression into a list comprehension.
Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
* Update pydantic/class_validators.py
Removed some exception verbosity.
Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
* Update changes/3215-SunsetOrange.md
Removed some changes verbosity.
Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
* use pytest.raises(.., match=...)
Co-authored-by: Michael <michael.bedford@superloop.com>
Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
Discussed in #3129
Originally posted by SunsetOrange August 24, 2021
I had been trying to locate a bug in my code for an hour or so, which I eventually narrowed down to an incorrectly defined custom validator decorator. I had placed multiple field strings into the decorator call, inside a list rather than as multiple args.
My own mistake, but Pydantic had been throwing a very unhelpful exception stating that my model was not hashable.
The exception was pointing at the class definition line, not the validator decorator.
Exception has occurred: TypeError unhashable type: 'list'
This confused me greatly for a good while as I tried to find a possible location in the model and its super classes where a list was being used as a dictionary key. As this is a mistake that others could easily make (and I think I have made it in the past as well), it would be nice and helpful to output an exception that identifies the mistake and provides helpful feedback. Else wise, the source of the exception is difficult to identify.
I proposing that the following code be added to class_validators.py at line 75.
The text was updated successfully, but these errors were encountered: