Skip to content
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

add as_sequence() method on dict views #2527

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -22,3 +22,6 @@ pip-wheel-metadata
valgrind-python.supp
*.pyd
lcov.info
.idea/
.vscode/
.python-version
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Add `as_sequence()` method on dict views. [#2527](https://github.com/PyO3/pyo3/pull/2527)
- Add `timezone_utc()`. [#1588](https://github.com/PyO3/pyo3/pull/1588)
- Implement `ToPyObject` for `[T; N]`. [#2313](https://github.com/PyO3/pyo3/pull/2313)
- Added the internal `IntoPyResult` trait to give better error messages when function return types do not implement `IntoPy`. [#2326](https://github.com/PyO3/pyo3/pull/2326)
Expand Down
49 changes: 48 additions & 1 deletion src/types/dict.rs
Expand Up @@ -3,7 +3,7 @@
use super::PyMapping;
use crate::err::{self, PyErr, PyResult};
use crate::ffi::Py_ssize_t;
use crate::types::{PyAny, PyList};
use crate::types::{PyAny, PyList, PySequence};
#[cfg(not(PyPy))]
use crate::IntoPyPointer;
use crate::{ffi, AsPyPointer, FromPyObject, IntoPy, PyObject, PyTryFrom, Python, ToPyObject};
Expand Down Expand Up @@ -34,6 +34,13 @@ pyobject_native_type_core!(
#checkfunction=ffi::PyDictKeys_Check
);

#[cfg(not(PyPy))]
impl PyDictKeys {
pub fn as_sequence(&self) -> &PySequence {
unsafe { PySequence::try_from_unchecked(self) }
}
}

/// Represents a Python `dict_values`.
#[cfg(not(PyPy))]
#[repr(transparent)]
Expand All @@ -46,6 +53,13 @@ pyobject_native_type_core!(
#checkfunction=ffi::PyDictValues_Check
);

#[cfg(not(PyPy))]
impl PyDictValues {
pub fn as_sequence(&self) -> &PySequence {
unsafe { PySequence::try_from_unchecked(self) }
}
}

PrettyWood marked this conversation as resolved.
Show resolved Hide resolved
/// Represents a Python `dict_items`.
#[cfg(not(PyPy))]
#[repr(transparent)]
Expand All @@ -58,6 +72,13 @@ pyobject_native_type_core!(
#checkfunction=ffi::PyDictItems_Check
);

#[cfg(not(PyPy))]
impl PyDictItems {
pub fn as_sequence(&self) -> &PySequence {
unsafe { PySequence::try_from_unchecked(self) }
}
}

impl PyDict {
/// Creates a new empty dictionary.
pub fn new(py: Python<'_>) -> &PyDict {
Expand Down Expand Up @@ -972,6 +993,14 @@ mod tests {
let dict = abc_dict(py);
let keys = dict.call_method0("keys").unwrap();
assert!(keys.is_instance(PyDictKeys::type_object(py)).unwrap());
assert_eq!(
keys.cast_as::<PyDictKeys>()
.unwrap()
.as_sequence()
.len()
.unwrap(),
3
);
})
}

Expand All @@ -982,6 +1011,15 @@ mod tests {
let dict = abc_dict(py);
let values = dict.call_method0("values").unwrap();
assert!(values.is_instance(PyDictValues::type_object(py)).unwrap());
assert_eq!(
values
.cast_as::<PyDictValues>()
.unwrap()
.as_sequence()
.len()
.unwrap(),
3
);
})
}

Expand All @@ -992,6 +1030,15 @@ mod tests {
let dict = abc_dict(py);
let items = dict.call_method0("items").unwrap();
assert!(items.is_instance(PyDictItems::type_object(py)).unwrap());
assert_eq!(
items
.cast_as::<PyDictItems>()
.unwrap()
.as_sequence()
.len()
.unwrap(),
3
);
})
}
}