votes up 2

duplicate validator function "(ref)"; if this is intended, set `allow_reuse=True`

Package:
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
🙏 Scream for help to Ukraine
Today, 14th August 2022, Russia continues bombing and firing Ukraine. Don't trust Russia, they are bombing us and brazenly lying in same time they are not doing this 😠, civilians and children are dying too! We are screaming and asking exactly you to help us, we want to survive, our families, children, older ones.
Please spread the information, and ask your governemnt to stop Russia by any means. We promise to work extrahard after survival to make the world safer place for all.

Ways to fix

votes up 4 votes down

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()
Jul 19, 2022 rivashchenko answer

Add a possible fix

Please authorize to post fix