Skip to content

Commit

Permalink
Mapping returns PyList for keys, values, and items (#4661)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
KLMatlock and Kevin Matlock authored Oct 29, 2024
1 parent ca2f639 commit f74d374
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
1 change: 1 addition & 0 deletions newsfragments/4661.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`PyMapping`'s `keys`, `values` and `items` methods return `PyList` instead of `PySequence`.
20 changes: 10 additions & 10 deletions src/types/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::py_result_ext::PyResultExt;
use crate::sync::GILOnceCell;
use crate::type_object::PyTypeInfo;
use crate::types::any::PyAnyMethods;
use crate::types::{PyAny, PyDict, PySequence, PyType};
use crate::types::{PyAny, PyDict, PyList, PyType};
use crate::{ffi, Py, PyTypeCheck, Python};

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

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

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

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

impl<'py> PyMappingMethods<'py> for Bound<'py, PyMapping> {
Expand Down Expand Up @@ -133,7 +133,7 @@ impl<'py> PyMappingMethods<'py> for Bound<'py, PyMapping> {
}

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

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

#[inline]
fn items(&self) -> PyResult<Bound<'py, PySequence>> {
fn items(&self) -> PyResult<Bound<'py, PyList>> {
unsafe {
ffi::PyMapping_Items(self.as_ptr())
.assume_owned_or_err(self.py())
Expand Down

0 comments on commit f74d374

Please sign in to comment.