-
Notifications
You must be signed in to change notification settings - Fork 763
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
3203: support ordering magic methods for `#[pyclass]` r=adamreichold a=davidhewitt Closes #2089 This adds `__lt__`, `__le__`, `__eq__`, `__ne__`, `__gt__`, and `__ge__` as per the Python implementations of what we call `__richcmp__`. There's a UI test confirming that the user cannot implement split forms and `__richcmp__` simultaneously. There's also a benchmark comparing implementing these split methods against using `__richcmp__`. I couldn't see a meaningful performance difference, so I'm tempted to deprecate `__richcmp__`, given that's not a magic method which exists in Python. Potentially we can provide options such as the opt-in `#[pyclass(eq, ord)]` to avoid boilerplate for people who don't want to implement six different methods. Co-authored-by: David Hewitt <1939362+davidhewitt@users.noreply.github.com>
- Loading branch information
Showing
15 changed files
with
610 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use criterion::{criterion_group, criterion_main, Bencher, Criterion}; | ||
|
||
use pyo3::{prelude::*, pyclass::CompareOp, Python}; | ||
|
||
#[pyclass] | ||
struct OrderedDunderMethods(i64); | ||
|
||
#[pymethods] | ||
impl OrderedDunderMethods { | ||
fn __lt__(&self, other: &Self) -> bool { | ||
self.0 < other.0 | ||
} | ||
|
||
fn __le__(&self, other: &Self) -> bool { | ||
self.0 <= other.0 | ||
} | ||
|
||
fn __eq__(&self, other: &Self) -> bool { | ||
self.0 == other.0 | ||
} | ||
|
||
fn __ne__(&self, other: &Self) -> bool { | ||
self.0 != other.0 | ||
} | ||
|
||
fn __gt__(&self, other: &Self) -> bool { | ||
self.0 > other.0 | ||
} | ||
|
||
fn __ge__(&self, other: &Self) -> bool { | ||
self.0 >= other.0 | ||
} | ||
} | ||
|
||
#[pyclass] | ||
#[derive(PartialEq, Eq, PartialOrd, Ord)] | ||
struct OrderedRichcmp(i64); | ||
|
||
#[pymethods] | ||
impl OrderedRichcmp { | ||
fn __richcmp__(&self, other: &Self, op: CompareOp) -> bool { | ||
op.matches(self.cmp(other)) | ||
} | ||
} | ||
|
||
fn bench_ordered_dunder_methods(b: &mut Bencher<'_>) { | ||
Python::with_gil(|py| { | ||
let obj1 = Py::new(py, OrderedDunderMethods(0)).unwrap().into_ref(py); | ||
let obj2 = Py::new(py, OrderedDunderMethods(1)).unwrap().into_ref(py); | ||
|
||
b.iter(|| obj2.gt(obj1).unwrap()); | ||
}); | ||
} | ||
|
||
fn bench_ordered_richcmp(b: &mut Bencher<'_>) { | ||
Python::with_gil(|py| { | ||
let obj1 = Py::new(py, OrderedRichcmp(0)).unwrap().into_ref(py); | ||
let obj2 = Py::new(py, OrderedRichcmp(1)).unwrap().into_ref(py); | ||
|
||
b.iter(|| obj2.gt(obj1).unwrap()); | ||
}); | ||
} | ||
|
||
fn criterion_benchmark(c: &mut Criterion) { | ||
c.bench_function("ordered_dunder_methods", bench_ordered_dunder_methods); | ||
c.bench_function("ordered_richcmp", bench_ordered_richcmp); | ||
} | ||
|
||
criterion_group!(benches, criterion_benchmark); | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Support `__lt__`, `__le__`, `__eq__`, `__ne__`, `__gt__` and `__ge__` in `#[pymethods]` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ hypothesis>=3.55 | |
pytest>=6.0 | ||
pytest-benchmark>=3.4 | ||
psutil>=5.6 | ||
typing_extensions>=4.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
use pyo3::prelude::*; | ||
use pyo3::{types::PyModule, Python}; | ||
|
||
#[pyclass] | ||
struct Eq(i64); | ||
|
||
#[pymethods] | ||
impl Eq { | ||
#[new] | ||
fn new(value: i64) -> Self { | ||
Self(value) | ||
} | ||
|
||
fn __eq__(&self, other: &Self) -> bool { | ||
self.0 == other.0 | ||
} | ||
|
||
fn __ne__(&self, other: &Self) -> bool { | ||
self.0 != other.0 | ||
} | ||
} | ||
|
||
#[pyclass] | ||
struct EqDefaultNe(i64); | ||
|
||
#[pymethods] | ||
impl EqDefaultNe { | ||
#[new] | ||
fn new(value: i64) -> Self { | ||
Self(value) | ||
} | ||
|
||
fn __eq__(&self, other: &Self) -> bool { | ||
self.0 == other.0 | ||
} | ||
} | ||
|
||
#[pyclass] | ||
struct Ordered(i64); | ||
|
||
#[pymethods] | ||
impl Ordered { | ||
#[new] | ||
fn new(value: i64) -> Self { | ||
Self(value) | ||
} | ||
|
||
fn __lt__(&self, other: &Self) -> bool { | ||
self.0 < other.0 | ||
} | ||
|
||
fn __le__(&self, other: &Self) -> bool { | ||
self.0 <= other.0 | ||
} | ||
|
||
fn __eq__(&self, other: &Self) -> bool { | ||
self.0 == other.0 | ||
} | ||
|
||
fn __ne__(&self, other: &Self) -> bool { | ||
self.0 != other.0 | ||
} | ||
|
||
fn __gt__(&self, other: &Self) -> bool { | ||
self.0 > other.0 | ||
} | ||
|
||
fn __ge__(&self, other: &Self) -> bool { | ||
self.0 >= other.0 | ||
} | ||
} | ||
|
||
#[pyclass] | ||
struct OrderedDefaultNe(i64); | ||
|
||
#[pymethods] | ||
impl OrderedDefaultNe { | ||
#[new] | ||
fn new(value: i64) -> Self { | ||
Self(value) | ||
} | ||
|
||
fn __lt__(&self, other: &Self) -> bool { | ||
self.0 < other.0 | ||
} | ||
|
||
fn __le__(&self, other: &Self) -> bool { | ||
self.0 <= other.0 | ||
} | ||
|
||
fn __eq__(&self, other: &Self) -> bool { | ||
self.0 == other.0 | ||
} | ||
|
||
fn __gt__(&self, other: &Self) -> bool { | ||
self.0 > other.0 | ||
} | ||
|
||
fn __ge__(&self, other: &Self) -> bool { | ||
self.0 >= other.0 | ||
} | ||
} | ||
|
||
#[pymodule] | ||
pub fn comparisons(_py: Python<'_>, m: &PyModule) -> PyResult<()> { | ||
m.add_class::<Eq>()?; | ||
m.add_class::<EqDefaultNe>()?; | ||
m.add_class::<Ordered>()?; | ||
m.add_class::<OrderedDefaultNe>()?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.