diff --git a/src/validators/literal.rs b/src/validators/literal.rs index d71a0ac2a..9f8c25e0f 100644 --- a/src/validators/literal.rs +++ b/src/validators/literal.rs @@ -136,9 +136,9 @@ impl LiteralLookup { } // must be an enum or bytes if let Some(expected_py) = &self.expected_py { - // We don't use ? to unpack the result of get_item in the next line because unhashable - // inputs will produce a TypeError, which in this case we just want to treat as a - // validation failure + // We don't use ? to unpack the result of `get_item` in the next line because unhashable + // inputs will produce a TypeError, which in this case we just want to treat equivalently + // to a failed lookup if let Ok(Some(v)) = expected_py.as_ref(py).get_item(input) { let id: usize = v.extract().unwrap(); return Ok(Some((input, &self.values[id]))); diff --git a/tests/validators/test_union.py b/tests/validators/test_union.py index 3a1884f3f..42c542000 100644 --- a/tests/validators/test_union.py +++ b/tests/validators/test_union.py @@ -799,5 +799,7 @@ class ModelA: # validation against Literal[True] fails bc of the unhashable dict # A ValidationError is raised, not a ValueError, which allows the validation against the union to continue - assert validator.validate_python({'a': 42}) - assert validator.validate_python(True) + m = validator.validate_python({'a': 42}) + assert isinstance(m, ModelA) + assert m.a == 42 + assert validator.validate_python(True) is True