8000 Fix reversal of loc when building errors from Python by adriangb · Pull Request #609 · pydantic/pydantic-core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix reversal of loc when building errors from Python #609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 18, 2023
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
7 changes: 6 additions & 1 deletion
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,11 @@ impl Serialize for Location {
impl TryFrom<Option<&PyAny>> for Location {
type Error = PyErr;

/// Only ever called by ValidationError -> PyLineError to convert user input to our internal Location
/// Thus this expects the location to *not* be reversed and reverses it before storing it.
fn try_from(location: Option<&PyAny>) -> PyResult<Self> {
Comment on lines +208 to 210
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if we have things like this that are only ever called from one place it might be best to just implement them in that one place instead of a reusable trait to avoid confusion around this sort of implementation details stemming from how the trait ends up getting used.

if let Some(location) = location {
let loc_vec: Vec<LocItem> = if let Ok(tuple) = location.downcast::<PyTuple>() {
let mut loc_vec: Vec<LocItem> = if let Ok(tuple) = location.downcast::<PyTuple>() {
tuple.iter().map(LocItem::try_from).collect::<PyResult<_>>()?
} else if let Ok(list) = location.downcast::<PyList>() {
list.iter().map(LocItem::try_from).collect::<PyResult<_>>()?
Expand All @@ -219,6 +221,9 @@ i 8000 mpl TryFrom<Option<&PyAny>> for Location {
if loc_vec.is_empty() {
Ok(Self::Empty)
} else {
// Don't force Python users to give use the location reversed
// just be we internally store it like that
loc_vec.reverse();
Ok(Self::List(loc_vec))
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ def test_raise_validation_error():

# insert_assert(exc_info.value.errors(include_url=False))
assert exc_info.value.errors(include_url=False) == [
{'type': 'greater_than', 'loc': (2, 'a'), 'msg': 'Input should be greater than 5', 'input': 4, 'ctx': {'gt': 5}}
{'type': 'greater_than', 'loc': ('a', 2), 'msg': 'Input should be greater than 5', 'input': 4, 'ctx': {'gt': 5}}
]
with pytest.raises(TypeError, match='GreaterThan requires context: {gt: Number}'):
raise ValidationError.from_exception_data('Foobar', [{'type': 'greater_than', 'loc': ('a', 2), 'input': 4}])
Expand Down
0