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

Check on duplicate validators breaks validators in jupyter notebooks #312

Closed
amogorkon opened this Issue Nov 24, 2018 · 4 comments

Comments

Projects
None yet
3 participants
@amogorkon
Copy link

amogorkon commented Nov 24, 2018

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.

@samuelcolvin

This comment has been minimized.

Copy link
Owner

samuelcolvin commented Nov 26, 2018

Would be great if you could format code or output like this to make it more readable.

I see what you mean. What about an environment variable and/or some check if we're in the jupyter before doing the duplicate check?

@samuelcolvin samuelcolvin added the bug label Nov 26, 2018

@samuelcolvin

This comment has been minimized.

Copy link
Owner

samuelcolvin commented Nov 26, 2018

I understand what you mean about warnings, but this catches a lot of potential problems for me alone, and probably for others too.

I'd rather keep it as an error but allow a workaround.

@Abraxos

This comment has been minimized.

Copy link

Abraxos commented Dec 18, 2018

I am having this problem as well, and I do not really understand what kind of behavior this is supposed to prevent. Could you please explain and perhaps I can re-write my code better?

@samuelcolvin

This comment has been minimized.

Copy link
Owner

samuelcolvin commented Dec 25, 2018

The test for this behavior demonstrates what it's there to avoid:

class Model(BaseModel):
a: str
b: str
@validator('a')
def duplicate_name(cls, v):
return v
@validator('b') # noqa
def duplicate_name(cls, v):
return v

If you setup multiple validators with the same name on a class you may get unexpected behaviour when the second validator overwrites the first. The error is there to avoid this behavour.

I often get this myself when I have two similar validators, and create the second by copy and pasting the first and forget to change the function name.

I'll try to fix for jupyter now.

samuelcolvin added a commit that referenced this issue Dec 25, 2018

samuelcolvin added a commit that referenced this issue Dec 25, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.