Skip to content

Commit f370e55

Browse files
committed
More stuff
1 parent 5cd47e2 commit f370e55

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

src/singledispatch/mro.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,20 +151,20 @@ fn c3_mro(
151151
}
152152
Err(e) => return Err(e),
153153
};
154+
eprintln!("bases = {bases:#?}");
154155
let boundary = c3_boundary(py, &bases)?;
155156
eprintln!("boundary = {boundary}");
156-
let base = &bases[boundary];
157157

158158
let (explicit_bases, other_bases) = bases.split_at(boundary);
159159
let abstract_bases: Vec<_> = abcs
160160
.iter()
161161
.flat_map(|abc| {
162162
if Builtins::cached(py)
163-
.issubclass(py, cls, base.wrapped().bind(py))
163+
.issubclass(py, cls, abc.wrapped().bind(py))
164164
.unwrap()
165165
&& !bases.iter().any(|b| {
166166
Builtins::cached(py)
167-
.issubclass(py, b.wrapped().bind(py), base.wrapped().bind(py))
167+
.issubclass(py, b.wrapped().bind(py), abc.wrapped().bind(py))
168168
.unwrap()
169169
})
170170
{
@@ -174,6 +174,9 @@ fn c3_mro(
174174
}
175175
})
176176
.collect();
177+
eprintln!("explict_bases = {explicit_bases:#?}");
178+
eprintln!("other_bases = {other_bases:#?}");
179+
eprintln!("abstract_bases = {abstract_bases:#?}");
177180

178181
let new_abcs: Vec<_> = abcs.iter().filter(|c| abstract_bases.contains(c)).collect();
179182

@@ -186,6 +189,7 @@ fn c3_mro(
186189
mros.extend(&mut explicit_bases_mro);
187190

188191
let mut abstract_bases_mro = sub_c3_mro(py, abstract_bases.iter().map(|v| *v), &new_abcs)?;
192+
eprintln!("abstract_bases_mro = {abstract_bases_mro:#?}");
189193
mros.extend(&mut abstract_bases_mro);
190194

191195
let mut other_bases_mro = sub_c3_mro(py, other_bases.iter(), &new_abcs)?;
@@ -212,7 +216,9 @@ pub(crate) fn compose_mro(
212216
let typing = TypingModule::cached(py);
213217

214218
let bases: HashSet<_> = get_obj_mro(&cls)?;
219+
eprintln!("bases = {bases:#?}");
215220
let registered_types: HashSet<_> = types.collect();
221+
eprintln!("registered_types = {registered_types:#?}");
216222
let eligible_types: HashSet<_> = registered_types
217223
.iter()
218224
.filter(|&tref| {
@@ -235,6 +241,7 @@ pub(crate) fn compose_mro(
235241
})
236242
.copied()
237243
.collect();
244+
eprintln!("eligible_types = {eligible_types:#?}");
238245
let mut mro: Vec<PyTypeReference> = Vec::new();
239246
eligible_types.iter().for_each(|&tref| {
240247
// Subclasses of the ABCs in *types* which are also implemented by
@@ -244,6 +251,7 @@ pub(crate) fn compose_mro(
244251
.unwrap()
245252
.iter()
246253
.filter(|subclass| {
254+
eprintln!("subclass = {subclass:#?}");
247255
let typ = subclass.wrapped();
248256
let tref = PyTypeReference::new(typ.clone_ref(py));
249257
!bases.contains(&tref)
@@ -273,6 +281,9 @@ pub(crate) fn compose_mro(
273281
});
274282
}
275283
});
284+
eprintln!("Pre-mro candidates {mro:#?}");
276285

277-
c3_mro(py, &cls, mro)
286+
let final_rmo = c3_mro(py, &cls, mro);
287+
eprintln!("MRO for {cls}: {final_rmo:#?}");
288+
final_rmo
278289
}

src/singledispatch/typeref.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use pyo3::{PyObject, Python};
2-
use std::fmt::{Display, Formatter};
2+
use std::fmt::{Debug, Display, Formatter};
33
use std::hash::{Hash, Hasher};
44

55
pub struct PyTypeReference {
@@ -22,6 +22,12 @@ impl PyTypeReference {
2222
}
2323
}
2424

25+
impl Debug for PyTypeReference {
26+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
27+
std::fmt::Display::fmt(&self.wrapped, f)
28+
}
29+
}
30+
2531
impl Display for PyTypeReference {
2632
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
2733
std::fmt::Display::fmt(&self.wrapped, f)

tests/test_singledispatch_native.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from collections.abc import Sequence
22

33
import pytest
4+
#from functools import singledispatch
45
from singledispatch_native import singledispatch
56

67
from typing import Any
@@ -16,7 +17,7 @@ def _some_fun_str(o: str) -> str:
1617

1718

1819
@some_fun.register(int)
19-
def _some_fun_str(o: int) -> str:
20+
def _some_fun_int(o: int) -> str:
2021
return "It's an int!"
2122

2223

@@ -26,7 +27,7 @@ def _some_fun_sequence(l: Sequence) -> str:
2627

2728

2829
@some_fun.register(tuple)
29-
def _some_fun_sequence(l: tuple) -> str:
30+
def _some_fun_tuple(l: tuple) -> str:
3031
return "tuple: " + ", ".join(l)
3132

3233

0 commit comments

Comments
 (0)