Skip to content

Check on duplicate validators breaks validators in jupyter notebooks #312

@amogorkon

Description

@amogorkon

Check on duplicate validators breaks validators in jupyter notebooks if a cell is executed more than once.

from pydantic import BaseModel, validator
import yaml
from ipaddress import ip_address

class Config(BaseModel):
    ip: str
    name: str
    port: int
        
    @validator("ip")
    def check_ip(cls, value):
        return ip_address(value)            
            

with open("config") as f:
    d = yaml.safe_load(f)
    c = Config(**d)
    print(c)

Returns

Config ip=IPv4Address('123.0.0.200') name='asdf' port=2392
when the cell is called first and raises

ConfigError Traceback (most recent call last)
in ()
3 from ipaddress import ip_address
4
----> 5 class Config(BaseModel):
6 ip: str
7 name: str

in Config()
8 port: int
9
---> 10 @validator("ip")
11 def check_ip(cls, value):
12 return ip_address(value)

~/anaconda3/lib/python3.7/site-packages/pydantic/main.py in dec(f)
449 ref = f.module + '.' + f.qualname
450 if ref in _FUNCS:
--> 451 raise ConfigError(f'duplicate validator function "{ref}"')
452 _FUNCS.add(ref)
453 f_cls = classmethod(f)

ConfigError: duplicate validator function "main.Config.check_ip"

The second time the same cell is called.

I was somewhat surprised by this behaviour and when I checked the code, I didn't really get why this exception is raised since we're dealing with a set here, so behaviour shouldn't change.
I would suggest to replace this exception with a logger message, warning level, which won't mess with people's code and is well supported by jupyter.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V1Bug related to Pydantic V1.X

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions