duplicate validator function "(ref)"; if this is intended, set `allow_reuse=True`
Package:
pydantic
7315
Exception Class:
ConfigError
Raise code
"""
Avoid validators with duplicated names since without this, validators can be overwritten silently
which generally isn't the intended behaviour, don't run in ipython (see #312) or if allow_reuse is False.
"""
f_cls = function if isinstance(function, classmethod) else classmethod(function)
if not in_ipython() and not allow_reuse:
ref = f_cls.__func__.__module__ + '.' + f_cls.__func__.__qualname__
if ref in _FUNCS:
raise ConfigError(f'duplicate validator function "{ref}"; if this is intended, set `allow_reuse=True`')
_FUNCS.add(ref)
return f_cls
class ValidatorGroup:
def __init__(self, validators: 'ValidatorListDict') -> None:
self.validators = validators
Links to the raise (1)
https://github.com/samuelcolvin/pydantic/blob/45db4ad3aa558879824a91dd3b011d0449eb2977/pydantic/class_validators.py#L144Ways to fix
This error happens if you have two validators with the same function name:
from pydantic import BaseModel, root_validator
class Test(BaseModel):
action: str
document: dict
@root_validator()
def action_check(cls, values):
if values["action"] != values["document"]["action"]:
raise Exception("actions are not same")
return values
@root_validator()
def action_check(cls, values):
if values["action"] == values["document"]["action"]:
raise Exception("actions are same")
return values
Test.parse_obj({
"action": "new",
"document": {
"action": "old"
}
})
Output:
...
pydantic.errors.ConfigError: duplicate validator function "__main__.Test.action_check"; if this is intended, set `allow_reuse=True`
Solution: rename one of the validation functions
Of course, you can also set @root_validator(allow_reuse=True)
but then the upper validator will be muted by validator below.
If you faced this problem in the Docker container
Due to caching in the container, this error after renaming or deleting one of the problematic validator may not disappear until you completely reload the container. You can fix this if you add these lines to the file where this error appears:
from pydantic.class_validators import _FUNCS
_FUNCS.clear()
Add a possible fix
Please authorize to post fix