diff --git a/CHANGELOG.md b/CHANGELOG.md index d2e7db80..180012c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## Unreleased +### Fixed + +* `FromPyObject` implementation for `PySequence`. [#827](https://github.com/PyO3/pyo3/pull/827) + ## [0.9.0] ### Changed diff --git a/src/types/sequence.rs b/src/types/sequence.rs index fed2dbd6..71f92000 100644 --- a/src/types/sequence.rs +++ b/src/types/sequence.rs @@ -16,6 +16,7 @@ use crate::{FromPyObject, PyTryFrom, ToBorrowedObject}; #[repr(transparent)] pub struct PySequence(PyObject, Unsendable); pyobject_native_type_named!(PySequence); +pyobject_native_type_extract!(PySequence); impl PySequence { /// Returns the number of objects in sequence. diff --git a/tests/test_methods.rs b/tests/test_methods.rs index 5aaf9f6e..da473fcc 100644 --- a/tests/test_methods.rs +++ b/tests/test_methods.rs @@ -499,3 +499,32 @@ fn test_cfg_attrs() { py_assert!(py, inst, "not hasattr(inst, 'never_compiled_method')"); } + +#[pyclass] +#[derive(Default)] +struct FromSequence { + #[pyo3(get)] + numbers: Vec, +} + +#[pymethods] +impl FromSequence { + #[new] + fn new(seq: Option<&pyo3::types::PySequence>) -> PyResult { + if let Some(seq) = seq { + Ok(FromSequence { + numbers: seq.as_ref().extract::>()?, + }) + } else { + Ok(FromSequence::default()) + } + } +} + +#[test] +fn test_from_sequence() { + let gil = Python::acquire_gil(); + let py = gil.python(); + let typeobj = py.get_type::(); + py_assert!(py, typeobj, "typeobj(range(0, 4)).numbers == [0, 1, 2, 3]") +}