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
Currently the
eq_intparameter of#[pyclass]is described as the following:pyo3/guide/pyclass-parameters.md
Line 9 in 771558e
This description is confusing to me. From my original interpretation, I was under the impression
eq_intemitted the following pseudo-code:More specifically, I thought the
eq_intattribute 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_intlets simple enums be compared with integers in addition to itself:I think
eq_int's description should be re-worded so the following are clear:eq_intlets simple enums be compared for equality with integers in addition to itself#[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 runningMyEnum::Variant as u64in Rust