Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand Down
65 changes: 29 additions & 36 deletions src/singledispatch/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -246,18 +242,15 @@ impl SingleDispatch {
fn dispatch(&self, py: Python<'_>, cls: Bound<'_, PyAny>) -> PyResult<PyObject> {
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)
Expand Down
10 changes: 7 additions & 3 deletions src/singledispatch/mro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ fn get_obj_subclasses(cls: &Bound<'_, PyAny>) -> PyResult<HashSet<PyTypeReferenc
Ok(subclasses)
}

fn c3_mro(py: Python, cls: Bound<'_, PyAny>, abcs: Vec<PyTypeReference>) -> PyResult<Vec<PyTypeReference>> {
fn c3_mro(
py: Python,
cls: Bound<'_, PyAny>,
abcs: Vec<PyTypeReference>,
) -> PyResult<Vec<PyTypeReference>> {
Ok(abcs)
}

Expand Down Expand Up @@ -62,7 +66,7 @@ pub(crate) fn compose_mro(
*tref != other && other_mro.contains(tref)
})
})
.map(|tref| *tref)
.copied()
.collect();
let mut mro: Vec<PyTypeReference> = Vec::new();
eligible_types.iter().for_each(|&tref| {
Expand Down Expand Up @@ -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));
}
});
Expand Down
4 changes: 0 additions & 4 deletions src/singledispatch/typeref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}