Skip to content

IsInstance does not work for Pydantic BaseModels #61

@StefanBRas

Description

@StefanBRas

It seems like IsInstance will never be equal to a pydantic model:

from dirty_equals import IsInstance
from pydantic import BaseModel

class A(BaseModel): pass

print(A() == IsInstance(A)) # False
print(A() == IsInstance[A]) # False

class B: pass

print(B() == IsInstance(B)) # True
print(B() == IsInstance[B]) # True

Interestingly enough it works perfectly with IsPartialDict:

from dirty_equals import IsPartialDict, IsNegativeInt
from pydantic import BaseModel

class C(BaseModel):
    a: int
    b: str

print(C(a=-1,b="a") ==  IsPartialDict(a=IsNegativeInt)) # True
print(C(a=-1,b="a") ==  IsPartialDict(a=IsNegativeInt, b=1)) # False

Maybe it's also nice to document that IsPartialDict works with pydantic models without having to call model_instance.dict()?

Tested with dirty-equals version 0.50 and pydantic versions 1.9.0 and 1.10.7.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions