kurka
kurka

Reputation: 83

How to compare instances subclasses of @dataclass with same inheritance

I am trying to compare two dataclasses that inherit from a common base class (also a @dataclass).

The fields of the inherited classes are specific to them and are not considered in the comparison; I want to compare only the base class attributes.

Here is my attempt:

from dataclasses import dataclass, field

@dataclass(order=True)
class Base:
    a: float

@dataclass(order=True)
class ChildA(Base):
    attribute_a: str = field(compare=False)

@dataclass(order=True)
class ChildB(Base):
    attribute_b: str = field(compare=False)


ca = ChildA(1, 'a')
cb = ChildB(2, 'b')
ca < cb

However, I get:

TypeError: '<' not supported between instances of 'ChildA' and 'ChildB'

How can I solve this?

Upvotes: 6

Views: 20297

Answers (2)

chepner
chepner

Reputation: 531275

You should define the comparison methods for Base yourself; the methods created by dataclass require the arguments to have the exact same type.

from functools import total_ordering

@total_ordering
@dataclass(eq=False)
class Base:
    a: float


    # Both of these are oversimplified. At the very
    # least, handle the error resulting from `other`
    # not having an `a` attribute.

    def __eq__(self, other):
        return self.a == other.a

    def __lt__(self, other):
        return self.a < other.a

Upvotes: 6

JLeno46
JLeno46

Reputation: 1269

ca.a < cb.a

Both have the attribute 'a' from the parent class, access the attribute using '.a'

Upvotes: 0

Related Questions