Skip to content

Clarify #[pyclass(eq_int)] docs #6142

Description

@BD103

Currently the eq_int parameter of #[pyclass] is described as the following:

| `eq_int` | Implements `__eq__` using `__int__` for simple enums. |

This description is confusing to me. From my original interpretation, I was under the impression eq_int emitted the following pseudo-code:

# Without `eq_int`
def __eq__(self, other: MyEnum) -> bool:
    return self == other

# With `eq_int`
def __eq__(self, other: MyEnum) -> bool:
    return int(self) == int(other)

More specifically, I thought the eq_int attribute was purely an internal change that modifies how simple enums are compared, but did not have any user-facing effects. This was incorrect.

In reality, eq_int lets simple enums be compared with integers in addition to itself:

# What `eq_int` actually generates.
def __eq__(self, other: MyEnum | int) -> bool:
    if isinstance(other, MyEnum):
        return self == other
    elif isinstance(other, int):
        return int(self) == other

I think eq_int's description should be re-worded so the following are clear:

  • eq_int lets simple enums be compared for equality with integers in addition to itself
  • If the enum is also annotated with #[pyclass(ord)], it also lets the type by compared as greater or lesser than integers
  • __int__() returns the enum's discriminant, which is the same as running MyEnum::Variant as u64 in Rust

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions