Skip to content

Commit

Permalink
Use a TypeError, rather than a ValueError, when refusing to treat a s…
Browse files Browse the repository at this point in the history
…tr as a Vec

This is far more consistent with how these exceptions are usually used
  • Loading branch information
alex committed Nov 7, 2022
1 parent 7c25153 commit f7a487b
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 3 deletions.
1 change: 1 addition & 0 deletions newsfragments/2685.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use a TypeError, rather than a ValueError, when refusing to treat a str as a Vec
2 changes: 1 addition & 1 deletion pytests/tests/test_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_vec_from_bytes():


def test_vec_from_str():
with pytest.raises(ValueError):
with pytest.raises(TypeError):
sequence.vec_to_vec_pystring("123")


Expand Down
4 changes: 2 additions & 2 deletions src/types/sequence.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) 2017-present PyO3 Project and Contributors
use crate::err::{self, PyDowncastError, PyErr, PyResult};
use crate::exceptions::PyValueError;
use crate::exceptions::PyTypeError;
use crate::inspect::types::TypeInfo;
use crate::internal_tricks::get_ssize_index;
use crate::once_cell::GILOnceCell;
Expand Down Expand Up @@ -285,7 +285,7 @@ where
{
fn extract(obj: &'a PyAny) -> PyResult<Self> {
if let Ok(true) = obj.is_instance_of::<PyString>() {
return Err(PyValueError::new_err("Can't extract `str` to `Vec`"));
return Err(PyTypeError::new_err("Can't extract `str` to `Vec`"));
}
extract_sequence(obj)
}
Expand Down

0 comments on commit f7a487b

Please sign in to comment.