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

Comparison for dataclasses doesn't work correctly #2398

Closed
3 tasks done
barisione opened this issue Feb 24, 2021 · 4 comments · Fixed by #2557
Closed
3 tasks done

Comparison for dataclasses doesn't work correctly #2398

barisione opened this issue Feb 24, 2021 · 4 comments · Fixed by #2557
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@barisione
Copy link

barisione commented Feb 24, 2021

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.7.2
            pydantic compiled: True
                 install path: /Users/bari/.pyenv/versions/3.6.10/envs/undo-core-venv3.6/lib/python3.6/site-packages/pydantic
               python version: 3.6.10 (default, May 19 2020, 15:12:14)  [GCC 4.2.1 Compatible Apple LLVM 11.0.0 (clang-1100.0.33.8)]
                     platform: Darwin-20.3.0-x86_64-i386-64bit
     optional deps. installed: ['typing-extensions']

I also tried with 1.7.3 with Python 3.8 and the bug still happens.

After reading https://pydantic-docs.helpmanual.io/usage/dataclasses/ I understand why dataclasses in a model get replaced with a different type which behaves (almost) the same as the original dataclass.
Unfortunately comparison between instances of the original dataclass (defined with order=True) and the derived pydantic one doesn't work.

from dataclasses import dataclass

import pydantic


@dataclass(order=True)
class DC:
    num: int = 42


class Model(pydantic.BaseModel):
    dc: DC


real_dc = DC()
model = Model(dc=real_dc)

print(f"Real dataclass: {real_dc}; model's dataclass: {model.dc}")

# This works as expected.
assert real_dc <= real_dc
assert model.dc <= model.dc

try:
    # This raises a TypeError.
    assert real_dc <= model.dc
except TypeError as exc:
    print(f"Comparison failed: {exc}")

The output is:

Real dataclass: DC(num=42); model's dataclass: DC(num=42)
Comparison failed: '<=' not supported between instances of 'DC' and 'DC'

I thought this would help:

class Model(pydantic.BaseModel):
    dc: DC

    class Config:
        arbitrary_types_allowed = True

But the result is the same. I guess that arbitrary_types_allowed only applies to types not handled by pydantic at all, is that correct?

@barisione barisione added the bug V1 Bug related to Pydantic V1.X label Feb 24, 2021
@PrettyWood
Copy link
Member

Hi @barisione
It's the same problem as #2162

@barisione
Copy link
Author

Sorry, I searched existing issues but did not find that one. I guess I can close this then!

@PrettyWood PrettyWood reopened this Feb 24, 2021
@PrettyWood
Copy link
Member

Sorry I didn't mean it's the exact same issue but it's clearly related.
Now builtin dataclasses are converted into pydantic dataclasses when used in a model.
This is great and all but it breaks the __eq__, __gt__ and stuff that expect the exact same class.
I think it's worth keeping this issue open to think of all cases and not only __eq__

@PrettyWood
Copy link
Member

Hi @barisione
FYI I started to work again on the whole stdlib dataclass to validated dataclass #2557. Feedback more than welcome! 🙏

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.

2 participants