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

Throw descriptive exception for incorrectly defined validator fields. #3215

Closed
SunsetOrange opened this issue Sep 14, 2021 Discussed in #3129 · 0 comments · Fixed by #3216
Closed

Throw descriptive exception for incorrectly defined validator fields. #3215

SunsetOrange opened this issue Sep 14, 2021 Discussed in #3129 · 0 comments · Fixed by #3216

Comments

@SunsetOrange
Copy link
Contributor

SunsetOrange commented Sep 14, 2021

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.

    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>', ...], ...)`"
        )
samuelcolvin added a commit that referenced this issue Aug 8, 2022
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant