PyO3 / PyO3/pyo3

Clarify `#[pyclass(eq_int)]` docs

Open Beginner friendly
#6,142 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
16.2k
Forks
1k
Avg merge
2d 6h
Merged PRs (30d)
66

Description

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

https://github.com/PyO3/pyo3/blob/771558e3e0ea2e8e681bb5692a0f5afe8d5569ad/guide/pyclass-parameters.md?plain=1#L9

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with guide/pyclass-parameters.md at the linked #[pyclass] parameter description. Clarify that eq_int adds equality comparisons with integers, that #[pyclass(ord)] also enables ordering against integers, and that int returns the enum discriminant. Done when these user-facing effects are unambiguous in the documentation.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
documentation
Issue type
Documentation
Difficulty
1/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.