Skip to content

Commit 84faba3

Browse files
committed
simplify as discussed in PR
1 parent 6cef2dc commit 84faba3

File tree

1 file changed

+4
-35
lines changed

1 file changed

+4
-35
lines changed

src/input/input_python.rs

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -48,35 +48,6 @@ use super::{
4848
Input,
4949
};
5050

51-
static ORDERED_DICT_TYPE: PyOnceLock<Py<PyType>> = PyOnceLock::new();
52-
53-
fn get_ordered_dict_type(py: Python<'_>) -> &Bound<'_, PyType> {
54-
ORDERED_DICT_TYPE
55-
.get_or_init(py, || {
56-
py.import("collections")
57-
.and_then(|collections_module| collections_module.getattr("OrderedDict"))
58-
.unwrap()
59-
.extract()
60-
.unwrap()
61-
})
62-
.bind(py)
63-
}
64-
65-
fn check_if_ordered_dict(obj: &Bound<'_, PyAny>) -> bool {
66-
if obj.is_exact_instance_of::<PyDict>() {
67-
return false;
68-
}
69-
70-
if let Ok(type_name) = obj.get_type().name() {
71-
if type_name != "OrderedDict" {
72-
return false;
73-
}
74-
}
75-
76-
let ordered_dict_type = get_ordered_dict_type(obj.py());
77-
obj.is_instance(ordered_dict_type).unwrap_or(false)
78-
}
79-
8051
static FRACTION_TYPE: PyOnceLock<Py<PyType>> = PyOnceLock::new();
8152

8253
pub fn get_fraction_type(py: Python<'_>) -> &Bound<'_, PyType> {
@@ -424,19 +395,17 @@ impl<'py> Input<'py> for Bound<'py, PyAny> {
424395
Self: 'a;
425396

426397
fn strict_dict<'a>(&'a self) -> ValResult<GenericPyMapping<'a, 'py>> {
427-
if self.is_exact_instance_of::<PyDict>() {
428-
Ok(GenericPyMapping::Dict(self.downcast::<PyDict>()?))
429-
} else if check_if_ordered_dict(self) {
398+
if let Ok(dict) = self.downcast_exact::<PyDict>() {
399+
Ok(GenericPyMapping::Dict(dict))
400+
} else if self.is_instance_of::<PyDict>() {
430401
Ok(GenericPyMapping::Mapping(self.downcast::<PyMapping>()?))
431402
} else {
432403
Err(ValError::new(ErrorTypeDefaults::DictType, self))
433404
}
434405
}
435406

436407
fn lax_dict<'a>(&'a self) -> ValResult<GenericPyMapping<'a, 'py>> {
437-
if check_if_ordered_dict(self) {
438-
Ok(GenericPyMapping::Mapping(self.downcast::<PyMapping>()?))
439-
} else if let Ok(dict) = self.downcast::<PyDict>() {
408+
if let Ok(dict) = self.downcast_exact::<PyDict>() {
440409
Ok(GenericPyMapping::Dict(dict))
441410
} else if let Ok(mapping) = self.downcast::<PyMapping>() {
442411
Ok(GenericPyMapping::Mapping(mapping))

0 commit comments

Comments
 (0)