Use a TypeError, rather than a ValueError, when refusing to treat a str as a Vec

This is far more consistent with how these exceptions are usually used
This commit is contained in:
Alex Gaynor 2022-10-13 21:14:44 -04:00
parent 7c25153363
commit f7a487b809
Failed to extract signature
3 changed files with 4 additions and 3 deletions

View File

@ -0,0 +1 @@
Use a TypeError, rather than a ValueError, when refusing to treat a str as a Vec

View File

@ -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")

View File

@ -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;
@ -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)
}