Skip to content

Commit eeb686a

Browse files
committed
Eliminate unwraps
1 parent 222689b commit eeb686a

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

src/singledispatch/core.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,10 @@ use pyo3::{
1414
use std::collections::HashMap;
1515
use std::sync::Mutex;
1616

17-
fn get_abc_cache_token(py: Python) -> Bound<'_, PyAny> {
18-
py.import(intern!(py, "abc"))
19-
.unwrap()
20-
.getattr(intern!(py, "get_cache_token"))
21-
.unwrap()
17+
fn get_abc_cache_token(py: Python) -> PyResult<Bound<'_, PyAny>> {
18+
py.import(intern!(py, "abc"))?
19+
.getattr(intern!(py, "get_cache_token"))?
2220
.call0()
23-
.unwrap()
2421
}
2522

2623
fn valid_dispatch_types(py: Python, cls: &Bound<'_, PyAny>) -> PyResult<Vec<Py<PyType>>> {
@@ -83,26 +80,29 @@ impl SingleDispatchState {
8380
mro_match = Some(typ.clone_ref(py));
8481
}
8582

86-
if mro_match.is_some() {
87-
let m = &mro_match.unwrap().clone_ref(py);
88-
if self.registry.contains_key(typ)
89-
&& !cls_mro.contains(typ)
90-
&& !cls_mro.contains(m)
91-
&& Builtins::cached(py)
83+
match mro_match {
84+
Some(m) => {
85+
let m = &m.clone_ref(py);
86+
if self.registry.contains_key(typ)
87+
&& !cls_mro.contains(typ)
88+
&& !cls_mro.contains(m)
89+
&& Builtins::cached(py)
9290
.issubclass(py, m.wrapped().bind(py), typ.wrapped().bind(py))
9391
.is_ok_and(|res| res)
94-
{
95-
return Err(PyRuntimeError::new_err(format!(
96-
"Ambiguous dispatch: {m} or {typ}"
97-
)));
92+
{
93+
return Err(PyRuntimeError::new_err(format!(
94+
"Ambiguous dispatch: {m} or {typ}"
95+
)));
96+
}
97+
mro_match = Some(m.clone_ref(py));
98+
eprintln!("MRO match: {m}");
99+
break;
98100
}
99-
mro_match = Some(m.clone_ref(py));
100-
eprintln!("MRO match: {m}");
101-
break;
101+
_ => {},
102102
}
103103
}
104104
let impl_fn = match mro_match {
105-
Some(_) => match self.registry.get(&mro_match.unwrap()) {
105+
Some(v) => match self.registry.get(&v) {
106106
Some(&ref it) => Some(it.clone_ref(py)),
107107
None => None,
108108
},
@@ -180,7 +180,7 @@ impl SingleDispatch {
180180
}
181181
if state.cache_token.is_none() {
182182
if let Ok(_) = unbound_func.getattr(py, intern!(py, "__abstractmethods__")) {
183-
state.cache_token = Some(get_abc_cache_token(py).unbind());
183+
state.cache_token = Some(get_abc_cache_token(py)?.unbind());
184184
}
185185
}
186186
state.cache.clear();
@@ -253,7 +253,7 @@ impl SingleDispatch {
253253
Ok(mut state) => {
254254
match &state.cache_token {
255255
Some(cache_token) => {
256-
let current_token = get_abc_cache_token(py);
256+
let current_token = get_abc_cache_token(py)?;
257257
match current_token.rich_compare(cache_token.bind(py), CompareOp::Eq) {
258258
Ok(_) => {
259259
state.cache.clear();

0 commit comments

Comments
 (0)