Merge pull request #2685 from alex/type-error

Use a TypeError, rather than a ValueError, when refusing to treat a str as a Vec
This commit is contained in:
David Hewitt 2022-11-09 07:36:40 +00:00 committed by GitHub
commit 4b7e0ee5b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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)
}