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

Equatable base dataclasses become inequatable when used as BaseModel fields #3709

Closed
silby opened this issue Jan 20, 2022 · 0 comments · Fixed by #2557
Closed

Equatable base dataclasses become inequatable when used as BaseModel fields #3709

silby opened this issue Jan 20, 2022 · 0 comments · Fixed by #2557
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@silby
Copy link

silby commented Jan 20, 2022

Checks

  • [ * ] I added a descriptive title to this issue
  • [ * ] I have searched (google, github) for similar issues and couldn't find anything
  • [ * ] I have read and followed the docs and still think this is a bug

Bug

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

             pydantic version: 1.9.0
            pydantic compiled: True
                 install path: /Users/silby/Documents/code/pydantic-wtf/venv/lib/python3.10/site-packages/pydantic
               python version: 3.10.0 (v3.10.0:b494f5935c, Oct  4 2021, 14:59:19) [Clang 12.0.5 (clang-1205.0.22.11)]
                     platform: macOS-11.5.2-arm64-arm-64bit
     optional deps. installed: ['typing-extensions']

When using a base dataclass created with eq=True, values of that class used as fields in a BaseModel inheritor lose their equatable property when wrapped by Pydantic. This seems to contradict the docs, which state "the generated pydantic dataclass will have the exact same configuration (order, frozen, ...) as the original one."

from dataclasses import dataclass
from pydantic import BaseModel

@dataclass(frozen=True, eq=True)
class A:
    id: int


class PairOfA(BaseModel):
    name: str
    left_a: A
    right_a: A


aa = A(1)
ab = A(1)

print(aa)
print(ab)

assert aa == ab

pair = PairOfA(name="same", left_a=aa, right_a=ab)

print(pair)

assert pair.left_a == pair.right_a

The second assertion above fails as written. When replacing dataclasses.dataclass with pydantic.dataclasses.dataclass it succeeds, which is our workaround for now, but it seems like this shouldn't be necessary.

@silby silby added the bug V1 Bug related to Pydantic V1.X label Jan 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant