Skip to content

Commit 6922f18

Browse files
authored
Apply fixes from Clippy; check cargo fmt in CI (#3)
* Apply fixes suggested by `cargo clippy` to clean up code * Check `cargo fmt` in CI
1 parent 9820fc6 commit 6922f18

File tree

4 files changed

+44
-43
lines changed

4 files changed

+44
-43
lines changed

.github/workflows/CI.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ jobs:
3434
- name: Run tests
3535
run: uv run pytest
3636

37+
ci-checks:
38+
runs-on: ubuntu-latest
39+
steps:
40+
- uses: actions/checkout@v4
41+
- name: Check Rust formatting
42+
run: |
43+
cargo fmt --check
44+
3745
linux:
3846
runs-on: ${{ matrix.platform.runner }}
3947
needs: [ test ]

src/singledispatch/core.rs

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -79,31 +79,25 @@ impl SingleDispatchState {
7979
mro_match = Some(typ.clone_ref(py));
8080
}
8181

82-
match mro_match {
83-
Some(m) => {
84-
let m = &m.clone_ref(py);
85-
if self.registry.contains_key(typ)
86-
&& !cls_mro.contains(typ)
87-
&& !cls_mro.contains(m)
88-
&& Builtins::cached(py)
89-
.issubclass(py, m.wrapped().bind(py), typ.wrapped().bind(py))
90-
.is_ok_and(|res| res)
91-
{
92-
return Err(PyRuntimeError::new_err(format!(
93-
"Ambiguous dispatch: {m} or {typ}"
94-
)));
95-
}
96-
mro_match = Some(m.clone_ref(py));
97-
break;
82+
if let Some(m) = mro_match {
83+
let m = &m.clone_ref(py);
84+
if self.registry.contains_key(typ)
85+
&& !cls_mro.contains(typ)
86+
&& !cls_mro.contains(m)
87+
&& Builtins::cached(py)
88+
.issubclass(py, m.wrapped().bind(py), typ.wrapped().bind(py))
89+
.is_ok_and(|res| res)
90+
{
91+
return Err(PyRuntimeError::new_err(format!(
92+
"Ambiguous dispatch: {m} or {typ}"
93+
)));
9894
}
99-
_ => {}
95+
mro_match = Some(m.clone_ref(py));
96+
break;
10097
}
10198
}
10299
let impl_fn = match mro_match {
103-
Some(v) => match self.registry.get(&v) {
104-
Some(&ref it) => Some(it.clone_ref(py)),
105-
None => None,
106-
},
100+
Some(v) => self.registry.get(&v).map(|it| it.clone_ref(py)),
107101
None => None,
108102
};
109103
match impl_fn {
@@ -173,10 +167,12 @@ impl SingleDispatch {
173167
unbound_func.clone_ref(py),
174168
);
175169
}
176-
if state.cache_token.is_none() {
177-
if let Ok(_) = unbound_func.getattr(py, intern!(py, "__abstractmethods__")) {
178-
state.cache_token = Some(get_abc_cache_token(py)?.unbind());
179-
}
170+
if state.cache_token.is_none()
171+
&& unbound_func
172+
.getattr(py, intern!(py, "__abstractmethods__"))
173+
.is_ok()
174+
{
175+
state.cache_token = Some(get_abc_cache_token(py)?.unbind());
180176
}
181177
state.cache.clear();
182178
Ok(unbound_func)
@@ -246,18 +242,15 @@ impl SingleDispatch {
246242
fn dispatch(&self, py: Python<'_>, cls: Bound<'_, PyAny>) -> PyResult<PyObject> {
247243
match self.lock.lock() {
248244
Ok(mut state) => {
249-
match &state.cache_token {
250-
Some(cache_token) => {
251-
let current_token = get_abc_cache_token(py)?;
252-
match current_token.rich_compare(cache_token.bind(py), CompareOp::Eq) {
253-
Ok(_) => {
254-
state.cache.clear();
255-
state.cache_token = Some(current_token.unbind());
256-
}
257-
_ => (),
258-
}
245+
if let Some(cache_token) = &state.cache_token {
246+
let current_token = get_abc_cache_token(py)?;
247+
if current_token
248+
.rich_compare(cache_token.bind(py), CompareOp::Eq)
249+
.is_ok()
250+
{
251+
state.cache.clear();
252+
state.cache_token = Some(current_token.unbind());
259253
}
260-
_ => (),
261254
}
262255

263256
state.get_or_find_impl(py, cls)

src/singledispatch/mro.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ fn get_obj_subclasses(cls: &Bound<'_, PyAny>) -> PyResult<HashSet<PyTypeReferenc
2828
Ok(subclasses)
2929
}
3030

31-
fn c3_mro(py: Python, cls: Bound<'_, PyAny>, abcs: Vec<PyTypeReference>) -> PyResult<Vec<PyTypeReference>> {
31+
fn c3_mro(
32+
py: Python,
33+
cls: Bound<'_, PyAny>,
34+
abcs: Vec<PyTypeReference>,
35+
) -> PyResult<Vec<PyTypeReference>> {
3236
Ok(abcs)
3337
}
3438

@@ -62,7 +66,7 @@ pub(crate) fn compose_mro(
6266
*tref != other && other_mro.contains(tref)
6367
})
6468
})
65-
.map(|tref| *tref)
69+
.copied()
6670
.collect();
6771
let mut mro: Vec<PyTypeReference> = Vec::new();
6872
eligible_types.iter().for_each(|&tref| {
@@ -96,7 +100,7 @@ pub(crate) fn compose_mro(
96100
} else {
97101
found_subclasses.sort_by_key(|s| Reverse(s.len()));
98102
found_subclasses.iter().flatten().for_each(|tref| {
99-
if !mro.contains(&tref) {
103+
if !mro.contains(tref) {
100104
mro.push(tref.clone_ref(py));
101105
}
102106
});

src/singledispatch/typeref.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ impl PartialEq for PyTypeReference {
3838
fn eq(&self, other: &Self) -> bool {
3939
self.wrapped.is(&other.wrapped)
4040
}
41-
42-
fn ne(&self, other: &Self) -> bool {
43-
!self.wrapped.is(&other.wrapped)
44-
}
4541
}
4642

4743
impl Eq for PyTypeReference {}

0 commit comments

Comments
 (0)