Skip to content

Commit f74d374

Browse files
KLMatlockKevin Matlock
andauthored
Mapping returns PyList for keys, values, and items (PyO3#4661)
* Mappingproxy (#1) Adds in the MappingProxy type. * Move over from `iter` to `try_iter`. * Added lifetime to `try_iter`, preventing need to clone when iterating. * Remove unneccessary borrow. * Add newsfragment * Newline to newsfragment. * Remove explicit lifetime, * Review comments (#2) * Addressing more comments * Remove extract_bound. * Remove extract methods. * Update mapping to return PyList instead of Sequence. * Update comments for list return type. * Add newfragment. * Reimpliment copy with PyMapping type. * Trigger Build --------- Co-authored-by: Kevin Matlock <kevin.matlock@omicsautomation.com>
1 parent ca2f639 commit f74d374

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

newsfragments/4661.changed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
`PyMapping`'s `keys`, `values` and `items` methods return `PyList` instead of `PySequence`.

src/types/mapping.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::py_result_ext::PyResultExt;
66
use crate::sync::GILOnceCell;
77
use crate::type_object::PyTypeInfo;
88
use crate::types::any::PyAnyMethods;
9-
use crate::types::{PyAny, PyDict, PySequence, PyType};
9+
use crate::types::{PyAny, PyDict, PyList, PyType};
1010
use crate::{ffi, Py, PyTypeCheck, Python};
1111

1212
/// Represents a reference to a Python object supporting the mapping protocol.
@@ -77,14 +77,14 @@ pub trait PyMappingMethods<'py>: crate::sealed::Sealed {
7777
where
7878
K: IntoPyObject<'py>;
7979

80-
/// Returns a sequence containing all keys in the mapping.
81-
fn keys(&self) -> PyResult<Bound<'py, PySequence>>;
80+
/// Returns a list containing all keys in the mapping.
81+
fn keys(&self) -> PyResult<Bound<'py, PyList>>;
8282

83-
/// Returns a sequence containing all values in the mapping.
84-
fn values(&self) -> PyResult<Bound<'py, PySequence>>;
83+
/// Returns a list containing all values in the mapping.
84+
fn values(&self) -> PyResult<Bound<'py, PyList>>;
8585

86-
/// Returns a sequence of tuples of all (key, value) pairs in the mapping.
87-
fn items(&self) -> PyResult<Bound<'py, PySequence>>;
86+
/// Returns a list of all (key, value) pairs in the mapping.
87+
fn items(&self) -> PyResult<Bound<'py, PyList>>;
8888
}
8989

9090
impl<'py> PyMappingMethods<'py> for Bound<'py, PyMapping> {
@@ -133,7 +133,7 @@ impl<'py> PyMappingMethods<'py> for Bound<'py, PyMapping> {
133133
}
134134

135135
#[inline]
136-
fn keys(&self) -> PyResult<Bound<'py, PySequence>> {
136+
fn keys(&self) -> PyResult<Bound<'py, PyList>> {
137137
unsafe {
138138
ffi::PyMapping_Keys(self.as_ptr())
139139
.assume_owned_or_err(self.py())
@@ -142,7 +142,7 @@ impl<'py> PyMappingMethods<'py> for Bound<'py, PyMapping> {
142142
}
143143

144144
#[inline]
145-
fn values(&self) -> PyResult<Bound<'py, PySequence>> {
145+
fn values(&self) -> PyResult<Bound<'py, PyList>> {
146146
unsafe {
147147
ffi::PyMapping_Values(self.as_ptr())
148148
.assume_owned_or_err(self.py())
@@ -151,7 +151,7 @@ impl<'py> PyMappingMethods<'py> for Bound<'py, PyMapping> {
151151
}
152152

153153
#[inline]
154-
fn items(&self) -> PyResult<Bound<'py, PySequence>> {
154+
fn items(&self) -> PyResult<Bound<'py, PyList>> {
155155
unsafe {
156156
ffi::PyMapping_Items(self.as_ptr())
157157
.assume_owned_or_err(self.py())

0 commit comments

Comments
 (0)