python-betterproto icon indicating copy to clipboard operation
python-betterproto copied to clipboard

Unconventional `__eq__` method behavior

Open atomicflag opened this issue 3 years ago • 1 comments

Possibly due to

https://github.com/danielgtaylor/python-betterproto/blob/6faac1d1ca4b979704c145d968dfed8e38fe159c/src/betterproto/init.py#L654

returning False instead of NotImplemented (see Python docs here), the following snippet doesn't work as expected:

from dataclasses import dataclass
from unittest.mock import ANY
import betterproto

@dataclass(eq=False, repr=False)
class Foo(betterproto.Message):
    pass

assert Foo() == ANY  # Expected to pass but raises AssertionError

This is a minimal reproducible example. The actual use case is related to how syrupy and pytest check assertions. The following test crashes with an enigmatic error KeyError: -1 which is indirectly caused by betterproto's __eq__ method, which doesn't delegate comparison to the right hand side argument in case of a type mismatch. (Which is what returning NotImplemented is for)

# pytest 7.4.0
# syrupy 4.0.8

from dataclasses import dataclass
import betterproto

@dataclass(eq=False, repr=False)
class Foo(betterproto.Message):
    pass

def test_foo(snapshot):
    foo = Foo()
    assert foo == snapshot

atomicflag avatar Jul 25 '23 11:07 atomicflag

Would you be willing to open a PR to fix this? I didn't even know that returning NotImplemented delegated for __eq__ because there's no __req__ method is there?

Gobot1234 avatar Jul 25 '23 12:07 Gobot1234