Skip to content

ambiguous hash result for pydantic objects with default attributes #1534

@SiLiKhon

Description

@SiLiKhon

I've been trying to define a cached pipeline parameterized by a pydantic object as the only input to my function. I noticed however that joblib.hash() result depends on whether or not you pass the value for the default-valued attribute at construction (even if your passed value is same as default). See MRE:

import pydantic
import joblib

class Config(pydantic.BaseModel):
    field1: int
    field2: int = 5

c1 = Config(field1=0)
c2 = Config(**c1.dict())
c3 = Config(**c1.dict())

print(f"{(c1 == c2)=}")
print(f"{(c2 == c3)=}")
print(f"{(joblib.hash(c1) == joblib.hash(c2))=}")
print(f"{(joblib.hash(c2) == joblib.hash(c3))=}")
print(f"{joblib.__version__=}")
print(f"{pydantic.__version__=}")

Output:

(c1 == c2)=True
(c2 == c3)=True
(joblib.hash(c1) == joblib.hash(c2))=False
(joblib.hash(c2) == joblib.hash(c3))=True
joblib.__version__='1.3.2'
pydantic.__version__='1.10.13'

Expected True in all comparisons.
If I remove the default field (field2: int = 5) everything works as expected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions