-
Notifications
You must be signed in to change notification settings - Fork 763
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
val
ndarray object cannot be converted to Sequence
#2615
Comments
There certainly was no intentional change in the In any case, could you point out the specific code in question or better post a minimal reproducer of the pattern that worked in 0.16 but fails in 0.17? |
I think a workaround would be to accept an enum that derives If #2477 is indeed the underlying issue, we might want to relax the EDIT: More specifically, the |
Sure, this worked in #[pyclass]
struct Foo {}
#[pymethods]
impl Foo {
#[staticmethod]
fn from_string_container(val: Vec<String>) -> Foo {
Foo{}
}
} |
Yes, this sounds like a PyO3 bug so will transfer it over. @ritchie46 can you list the types which are having issues with the |
We have a few conversion from python to our types. So I assume it is Here are a few examples of types that failed. I think the common denominator is the ObjectValue (an opaque python object)#[derive(Clone, Debug)]
#[repr(transparent)]
pub struct ObjectValue {
pub inner: PyObject,
}
impl From<PyObject> for ObjectValue {
fn from(p: PyObject) -> Self {
Self { inner: p }
}
}
impl<'a> FromPyObject<'a> for ObjectValue {
fn extract(ob: &'a PyAny) -> PyResult<Self> {
Python::with_gil(|py| {
Ok(ObjectValue {
inner: ob.to_object(py),
})
})
}
} AnyValue (An enum over our allowed values )impl<'s> FromPyObject<'s> for Wrap<AnyValue<'s>> {
fn extract(ob: &'s PyAny) -> PyResult<Self> {
... hidden implementation
}
} |
It sounds like it's the outer (sequence) type that's causing the issue, not the inner (item) type. If so I think it is indeed a victim of the breaking change introduced by #2477. 😕 Unfortunately If the Sorry if I'm off the mark here but as a fan of both rust-numpy and polars I'm motivated to find a good fix for this and will do my best to help. |
I think the code that needs to be relaxed is
as this - passing I guess we could implement |
I don't want to over-focus on To your point though I suppose there could be a more relaxed version of |
@davidhewitt I think it would personally prefer to manually call Switching the implementation to |
This allows us to handle types like one-dimensional NumPy arrays which implement just enough of the sequence protocol to support the extract_sequence function but are not an instance of the sequence ABC.
This allows us to handle types like one-dimensional NumPy arrays which implement just enough of the sequence protocol to support the extract_sequence function but are not an instance of the sequence ABC.
This allows us to handle types like one-dimensional NumPy arrays which implement just enough of the sequence protocol to support the extract_sequence function but are not an instance of the sequence ABC.
This allows us to handle types like one-dimensional NumPy arrays which implement just enough of the sequence protocol to support the extract_sequence function but are not an instance of the sequence ABC.
This allows us to handle types like one-dimensional NumPy arrays which implement just enough of the sequence protocol to support the extract_sequence function but are not an instance of the sequence ABC.
This allows us to handle types like one-dimensional NumPy arrays which implement just enough of the sequence protocol to support the extract_sequence function but are not an instance of the sequence ABC.
This allows us to handle types like one-dimensional NumPy arrays which implement just enough of the sequence protocol to support the extract_sequence function but are not an instance of the sequence ABC.
This issue seems to not have been fixed yet. I am getting TypeError: argument 'oned': 'ndarray' object cannot be converted to 'Sequence' running test.py with the following files: src/lib.rs
Cargo.toml
pyproject.toml
test.py
|
This is a slightly different code path affected by the same underlying issue. The OP reported failures to extract |
Hi all, First, thank you for working on PyO3 ! 🙌 Currently we are facing a similar problem in pola-rs/polars#6531 when trying to upgrade Py03 to 0.18.0. We are planning in the mean time to introduce a constructor for this case. Do you think this is a good approach? Do you have any recommendations? Thanks! |
@jjerphan Py03 0.18 does contain the fix and we do test for this, c.f. https://github.com/PyO3/pyo3/blob/v0.18.0/pytests/tests/test_sequence.py#L28 Can you add more details of what is failing and how? |
In updating to pyo3/numpy 0.17 upstream in polars we've hit the case where we have a function that accepts a
Vec<T>
whereT: FromPyObject
to do the conversion from any sequence passed from python.This function is opaque to use, in that anything that can be converted to a
Vec<>
is fine. Such as lists, tuples, but also numpy arrays.Now with updating it seems that this design is not possible anymore. Is this intentional?
What would be the recommended function signature for such generic sequence?
The text was updated successfully, but these errors were encountered: