diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 6cd2668..ecd2ab7 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -34,6 +34,14 @@ jobs: - name: Run tests run: uv run pytest + ci-checks: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Check Rust formatting + run: | + cargo fmt --check + linux: runs-on: ${{ matrix.platform.runner }} needs: [ test ] diff --git a/src/singledispatch/core.rs b/src/singledispatch/core.rs index 5c4d6b0..d393098 100644 --- a/src/singledispatch/core.rs +++ b/src/singledispatch/core.rs @@ -79,31 +79,25 @@ impl SingleDispatchState { mro_match = Some(typ.clone_ref(py)); } - match mro_match { - Some(m) => { - let m = &m.clone_ref(py); - if self.registry.contains_key(typ) - && !cls_mro.contains(typ) - && !cls_mro.contains(m) - && Builtins::cached(py) - .issubclass(py, m.wrapped().bind(py), typ.wrapped().bind(py)) - .is_ok_and(|res| res) - { - return Err(PyRuntimeError::new_err(format!( - "Ambiguous dispatch: {m} or {typ}" - ))); - } - mro_match = Some(m.clone_ref(py)); - break; + if let Some(m) = mro_match { + let m = &m.clone_ref(py); + if self.registry.contains_key(typ) + && !cls_mro.contains(typ) + && !cls_mro.contains(m) + && Builtins::cached(py) + .issubclass(py, m.wrapped().bind(py), typ.wrapped().bind(py)) + .is_ok_and(|res| res) + { + return Err(PyRuntimeError::new_err(format!( + "Ambiguous dispatch: {m} or {typ}" + ))); } - _ => {} + mro_match = Some(m.clone_ref(py)); + break; } } let impl_fn = match mro_match { - Some(v) => match self.registry.get(&v) { - Some(&ref it) => Some(it.clone_ref(py)), - None => None, - }, + Some(v) => self.registry.get(&v).map(|it| it.clone_ref(py)), None => None, }; match impl_fn { @@ -173,10 +167,12 @@ impl SingleDispatch { unbound_func.clone_ref(py), ); } - if state.cache_token.is_none() { - if let Ok(_) = unbound_func.getattr(py, intern!(py, "__abstractmethods__")) { - state.cache_token = Some(get_abc_cache_token(py)?.unbind()); - } + if state.cache_token.is_none() + && unbound_func + .getattr(py, intern!(py, "__abstractmethods__")) + .is_ok() + { + state.cache_token = Some(get_abc_cache_token(py)?.unbind()); } state.cache.clear(); Ok(unbound_func) @@ -246,18 +242,15 @@ impl SingleDispatch { fn dispatch(&self, py: Python<'_>, cls: Bound<'_, PyAny>) -> PyResult { match self.lock.lock() { Ok(mut state) => { - match &state.cache_token { - Some(cache_token) => { - let current_token = get_abc_cache_token(py)?; - match current_token.rich_compare(cache_token.bind(py), CompareOp::Eq) { - Ok(_) => { - state.cache.clear(); - state.cache_token = Some(current_token.unbind()); - } - _ => (), - } + if let Some(cache_token) = &state.cache_token { + let current_token = get_abc_cache_token(py)?; + if current_token + .rich_compare(cache_token.bind(py), CompareOp::Eq) + .is_ok() + { + state.cache.clear(); + state.cache_token = Some(current_token.unbind()); } - _ => (), } state.get_or_find_impl(py, cls) diff --git a/src/singledispatch/mro.rs b/src/singledispatch/mro.rs index 16ec947..6085fcb 100644 --- a/src/singledispatch/mro.rs +++ b/src/singledispatch/mro.rs @@ -28,7 +28,11 @@ fn get_obj_subclasses(cls: &Bound<'_, PyAny>) -> PyResult, abcs: Vec) -> PyResult> { +fn c3_mro( + py: Python, + cls: Bound<'_, PyAny>, + abcs: Vec, +) -> PyResult> { Ok(abcs) } @@ -62,7 +66,7 @@ pub(crate) fn compose_mro( *tref != other && other_mro.contains(tref) }) }) - .map(|tref| *tref) + .copied() .collect(); let mut mro: Vec = Vec::new(); eligible_types.iter().for_each(|&tref| { @@ -96,7 +100,7 @@ pub(crate) fn compose_mro( } else { found_subclasses.sort_by_key(|s| Reverse(s.len())); found_subclasses.iter().flatten().for_each(|tref| { - if !mro.contains(&tref) { + if !mro.contains(tref) { mro.push(tref.clone_ref(py)); } }); diff --git a/src/singledispatch/typeref.rs b/src/singledispatch/typeref.rs index 0ed49ee..dbae827 100644 --- a/src/singledispatch/typeref.rs +++ b/src/singledispatch/typeref.rs @@ -38,10 +38,6 @@ impl PartialEq for PyTypeReference { fn eq(&self, other: &Self) -> bool { self.wrapped.is(&other.wrapped) } - - fn ne(&self, other: &Self) -> bool { - !self.wrapped.is(&other.wrapped) - } } impl Eq for PyTypeReference {}