dataclasses_order error

Invalid ordering comparison of dataclass instances

When @dataclass(order=True), Python synthesizes __lt__, __le__, __gt__, and __ge__ methods. These methods raise TypeError at runtime if the other operand is not an instance of the **same** class. Comparing two order=True dataclass instances of different types with <, <=, >, or >= is therefore a type error.

Additionally, when a class does NOT have order=True (including dataclass_transform classes with order=False), ordering comparisons are not supported at all because __lt__ etc. are never synthesized.

from dataclasses import dataclass

@dataclass(order=True)
class DC1:
    a: str

@dataclass(order=True)
class DC2:
    a: str

dc1 = DC1("x")
dc2 = DC2("y")

if dc1 < dc2:   # E: incompatible types
    pass

How to handle it

Every rule is on by default — strict is the default, not a cage. You can dial dataclasses_order down per-file or per-path from your editor or pyproject.toml, or fix the code so it type-checks. See the Type System rules and the complete diagnostic reference.

Canonical URL: https://www.basilisk-python.dev/errors/dataclasses_order