Skip to content

Commit d52d85b

Browse files
committed
fix strict and updates as request in PR
1 parent bef11c6 commit d52d85b

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

src/input/input_python.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,21 @@ fn get_ordered_dict_type(py: Python<'_>) -> &Bound<'_, PyType> {
6363
}
6464

6565
fn check_if_ordered_dict(obj: &Bound<'_, PyAny>) -> bool {
66+
println!("check_if_ordered_dict: {:?}", obj);
6667
if obj.is_exact_instance_of::<PyDict>() {
68+
println!("is exact dict");
6769
return false;
6870
}
6971

7072
if let Ok(type_name) = obj.get_type().name() {
73+
println!("this is the type name: {}", type_name);
7174
if type_name.to_string() != "OrderedDict" {
7275
return false;
7376
}
7477
}
7578

7679
let ordered_dict_type = get_ordered_dict_type(obj.py());
80+
println!("is ordered dict type: {}", ordered_dict_type);
7781
obj.is_instance(ordered_dict_type).unwrap_or(false)
7882
}
7983

@@ -424,26 +428,19 @@ impl<'py> Input<'py> for Bound<'py, PyAny> {
424428
Self: 'a;
425429

426430
fn strict_dict<'a>(&'a self) -> ValResult<GenericPyMapping<'a, 'py>> {
427-
if let Ok(dict) = self.downcast::<PyDict>() {
428-
Ok(GenericPyMapping::Dict(dict))
429-
} else {
431+
if self.is_exact_instance_of::<PyDict>() {
432+
Ok(GenericPyMapping::Dict(self.downcast::<PyDict>()?))
433+
} else if check_if_ordered_dict(self) {
434+
Ok(GenericPyMapping::Mapping(self.downcast::<PyMapping>()?))
435+
}
436+
else {
430437
Err(ValError::new(ErrorTypeDefaults::DictType, self))
431438
}
432439
}
433440

434441
fn lax_dict<'a>(&'a self) -> ValResult<GenericPyMapping<'a, 'py>> {
435-
436-
if check_if_ordered_dict(self) {
437-
// OrderedDict is a subclass of dict, but we want to treat it as a mapping to preserve order
438-
if let Ok(mapping) = self.downcast::<PyMapping>() {
439-
return Ok(GenericPyMapping::Mapping(mapping));
440-
}
441-
}
442-
443-
if let Ok(dict) = self.downcast::<PyDict>() {
444-
Ok(GenericPyMapping::Dict(dict))
445-
} else if let Ok(mapping) = self.downcast::<PyMapping>() {
446-
Ok(GenericPyMapping::Mapping(mapping))
442+
if (self.is_instance_of::<PyDict>() || self.is_instance_of::<PyMapping>()) {
443+
Ok(GenericPyMapping::Mapping(self.downcast::<PyMapping>()?))
447444
} else {
448445
Err(ValError::new(ErrorTypeDefaults::DictType, self))
449446
}

tests/validators/test_dict.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,22 +314,23 @@ def test_dict_fail_fast(fail_fast, expected):
314314

315315
assert exc_info.value.errors(include_url=False) == expected
316316

317-
def test_ordered_dict_key_order_preservation():
317+
@pytest.mark.parametrize('strict', [True, False])
318+
def test_ordered_dict_key_order_preservation(strict):
318319
# GH 12273
319320
v = SchemaValidator(cs.dict_schema(keys_schema=cs.str_schema(), values_schema=cs.int_schema()))
320321

321322
# Original issue
322323
foo = OrderedDict({'a': 1, 'b': 2})
323324
foo.move_to_end('a')
324325

325-
result = v.validate_python(foo)
326+
result = v.validate_python(foo, strict=strict)
326327
assert list(result.keys()) == list(foo.keys()) == ['b', 'a']
327328
assert result == {'b': 2, 'a': 1}
328329

329330
# More complex case
330331
foo2 = OrderedDict({'x': 1, 'y': 2, 'z': 3})
331332
foo2.move_to_end('x')
332333

333-
result2 = v.validate_python(foo2)
334+
result2 = v.validate_python(foo2, strict=strict)
334335
assert list(result2.keys()) == list(foo2.keys()) == ['y', 'z', 'x']
335336
assert result2 == {'y': 2, 'z': 3, 'x': 1}

0 commit comments

Comments
 (0)