Skip to content

Commit 295745e

Browse files
committed
Use py.NotImplemented() for unsupported cmp ops
1 parent d273516 commit 295745e

File tree

4 files changed

+48
-41
lines changed

4 files changed

+48
-41
lines changed

src/document.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
use itertools::Itertools;
55
use pyo3::{
66
basic::CompareOp,
7-
exceptions::PyNotImplementedError,
87
prelude::*,
98
types::{
109
PyAny, PyDateAccess, PyDateTime, PyDict, PyList, PyTimeAccess, PyTuple,
@@ -563,13 +562,16 @@ impl Document {
563562
self.clone()
564563
}
565564

566-
fn __richcmp__(&self, other: &Self, op: CompareOp) -> PyResult<bool> {
565+
fn __richcmp__(
566+
&self,
567+
other: &Self,
568+
op: CompareOp,
569+
py: Python<'_>,
570+
) -> PyObject {
567571
match op {
568-
CompareOp::Eq => Ok(self == other),
569-
CompareOp::Ne => Ok(self != other),
570-
_ => Err(PyNotImplementedError::new_err(format!(
571-
"{op:?} op is not supported"
572-
))),
572+
CompareOp::Eq => (self == other).into_py(py),
573+
CompareOp::Ne => (self != other).into_py(py),
574+
_ => py.NotImplemented(),
573575
}
574576
}
575577
}

src/facet.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use pyo3::{
2-
basic::CompareOp, exceptions::PyNotImplementedError, prelude::*,
3-
types::PyType,
4-
};
1+
use pyo3::{basic::CompareOp, prelude::*, types::PyType};
52
use tantivy::schema;
63

74
/// A Facet represent a point in a given hierarchy.
@@ -71,13 +68,16 @@ impl Facet {
7168
Ok(format!("Facet({})", self.to_path_str()))
7269
}
7370

74-
fn __richcmp__(&self, other: &Self, op: CompareOp) -> PyResult<bool> {
71+
fn __richcmp__(
72+
&self,
73+
other: &Self,
74+
op: CompareOp,
75+
py: Python<'_>,
76+
) -> PyObject {
7577
match op {
76-
CompareOp::Eq => Ok(self == other),
77-
CompareOp::Ne => Ok(self != other),
78-
_ => Err(PyNotImplementedError::new_err(format!(
79-
"{op:?} op is not supported"
80-
))),
78+
CompareOp::Eq => (self == other).into_py(py),
79+
CompareOp::Ne => (self != other).into_py(py),
80+
_ => py.NotImplemented(),
8181
}
8282
}
8383
}

src/schema.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use pyo3::{basic::CompareOp, exceptions::PyNotImplementedError, prelude::*};
1+
use pyo3::{basic::CompareOp, prelude::*};
22
use tantivy as tv;
33

44
/// Tantivy schema.
@@ -13,13 +13,16 @@ pub(crate) struct Schema {
1313

1414
#[pymethods]
1515
impl Schema {
16-
fn __richcmp__(&self, other: &Self, op: CompareOp) -> PyResult<bool> {
16+
fn __richcmp__(
17+
&self,
18+
other: &Self,
19+
op: CompareOp,
20+
py: Python<'_>,
21+
) -> PyObject {
1722
match op {
18-
CompareOp::Eq => Ok(self == other),
19-
CompareOp::Ne => Ok(self != other),
20-
_ => Err(PyNotImplementedError::new_err(format!(
21-
"{op:?} op is not supported"
22-
))),
23+
CompareOp::Eq => (self == other).into_py(py),
24+
CompareOp::Ne => (self != other).into_py(py),
25+
_ => py.NotImplemented(),
2326
}
2427
}
2528
}

src/searcher.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
#![allow(clippy::new_ret_no_self)]
22

33
use crate::{document::Document, query::Query, to_pyerr};
4-
use pyo3::{
5-
basic::CompareOp,
6-
exceptions::{PyNotImplementedError, PyValueError},
7-
prelude::*,
8-
};
4+
use pyo3::{basic::CompareOp, exceptions::PyValueError, prelude::*};
95
use tantivy as tv;
106
use tantivy::collector::{Count, MultiCollector, TopDocs};
117

@@ -65,13 +61,16 @@ impl SearchResult {
6561
}
6662
}
6763

68-
fn __richcmp__(&self, other: &Self, op: CompareOp) -> PyResult<bool> {
64+
fn __richcmp__(
65+
&self,
66+
other: &Self,
67+
op: CompareOp,
68+
py: Python<'_>,
69+
) -> PyObject {
6970
match op {
70-
CompareOp::Eq => Ok(self == other),
71-
CompareOp::Ne => Ok(self != other),
72-
_ => Err(PyNotImplementedError::new_err(format!(
73-
"{op:?} op is not supported"
74-
))),
71+
CompareOp::Eq => (self == other).into_py(py),
72+
CompareOp::Ne => (self != other).into_py(py),
73+
_ => py.NotImplemented(),
7574
}
7675
}
7776

@@ -231,13 +230,16 @@ impl DocAddress {
231230
self.doc
232231
}
233232

234-
fn __richcmp__(&self, other: &Self, op: CompareOp) -> PyResult<bool> {
233+
fn __richcmp__(
234+
&self,
235+
other: &Self,
236+
op: CompareOp,
237+
py: Python<'_>,
238+
) -> PyObject {
235239
match op {
236-
CompareOp::Eq => Ok(self == other),
237-
CompareOp::Ne => Ok(self != other),
238-
_ => Err(PyNotImplementedError::new_err(format!(
239-
"{op:?} op is not supported"
240-
))),
240+
CompareOp::Eq => (self == other).into_py(py),
241+
CompareOp::Ne => (self != other).into_py(py),
242+
_ => py.NotImplemented(),
241243
}
242244
}
243245
}

0 commit comments

Comments
 (0)