From eb4ba53251bad0b53ce2bbab660e1c5e59323c47 Mon Sep 17 00:00:00 2001 From: David Hewitt <1939362+davidhewitt@users.noreply.github.com> Date: Tue, 18 Apr 2023 19:56:37 +0100 Subject: [PATCH] Rename sequence `.list()` and `.tuple()` to `.to_list()` and `.to_tuple()` --- newsfragments/3111.changed.md | 1 + src/types/list.rs | 2 +- src/types/sequence.rs | 54 +++++++++++++++++++++++++++++------ src/types/tuple.rs | 4 +-- 4 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 newsfragments/3111.changed.md diff --git a/newsfragments/3111.changed.md b/newsfragments/3111.changed.md new file mode 100644 index 00000000000..584aed1b191 --- /dev/null +++ b/newsfragments/3111.changed.md @@ -0,0 +1 @@ +Rename `PySequence::list` and `PySequence::tuple` to `PySequence::to_list` and `PySequence::to_tuple`. (The old names continue to exist as deprecated forms.) diff --git a/src/types/list.rs b/src/types/list.rs index dbafa426c60..3e801276b95 100644 --- a/src/types/list.rs +++ b/src/types/list.rs @@ -293,7 +293,7 @@ impl PyList { /// Return a new tuple containing the contents of the list; equivalent to the Python expression `tuple(list)`. /// - /// This method is equivalent to `self.as_sequence().tuple()` and faster than `PyTuple::new(py, this_list)`. + /// This method is equivalent to `self.as_sequence().to_tuple()` and faster than `PyTuple::new(py, this_list)`. pub fn to_tuple(&self) -> &PyTuple { unsafe { self.py().from_owned_ptr(ffi::PyList_AsTuple(self.as_ptr())) } } diff --git a/src/types/sequence.rs b/src/types/sequence.rs index ce673a47443..a6248b37f50 100644 --- a/src/types/sequence.rs +++ b/src/types/sequence.rs @@ -239,22 +239,36 @@ impl PySequence { /// Returns a fresh list based on the Sequence. #[inline] - pub fn list(&self) -> PyResult<&PyList> { + pub fn to_list(&self) -> PyResult<&PyList> { unsafe { self.py() .from_owned_ptr_or_err(ffi::PySequence_List(self.as_ptr())) } } + /// Returns a fresh list based on the Sequence. + #[inline] + #[deprecated(since = "0.19.0", note = "renamed to .to_list()")] + pub fn list(&self) -> PyResult<&PyList> { + self.to_list() + } + /// Returns a fresh tuple based on the Sequence. #[inline] - pub fn tuple(&self) -> PyResult<&PyTuple> { + pub fn to_tuple(&self) -> PyResult<&PyTuple> { unsafe { self.py() .from_owned_ptr_or_err(ffi::PySequence_Tuple(self.as_ptr())) } } + /// Returns a fresh tuple based on the Sequence. + #[inline] + #[deprecated(since = "0.19.0", note = "renamed to .to_tuple()")] + pub fn tuple(&self) -> PyResult<&PyTuple> { + self.to_tuple() + } + /// Register a pyclass as a subclass of `collections.abc.Sequence` (from the Python standard /// library). This is equvalent to `collections.abc.Sequence.register(T)` in Python. /// This registration is required for a pyclass to be downcastable from `PyAny` to `PySequence`. @@ -387,7 +401,7 @@ impl Py { #[cfg(test)] mod tests { - use crate::types::{PyList, PySequence}; + use crate::types::{PyList, PySequence, PyTuple}; use crate::{AsPyPointer, Py, PyObject, Python, ToPyObject}; fn get_object() -> PyObject { @@ -802,7 +816,11 @@ mod tests { let v = vec!["foo", "bar"]; let ob = v.to_object(py); let seq = ob.downcast::(py).unwrap(); - assert!(seq.list().is_ok()); + assert!(seq.to_list().unwrap().eq(PyList::new(py, &v)).unwrap()); + #[allow(deprecated)] + { + assert!(seq.list().is_ok()); + } }); } @@ -812,7 +830,15 @@ mod tests { let v = "foo"; let ob = v.to_object(py); let seq: &PySequence = ob.downcast(py).unwrap(); - assert!(seq.list().is_ok()); + assert!(seq + .to_list() + .unwrap() + .eq(PyList::new(py, &["f", "o", "o"])) + .unwrap()); + #[allow(deprecated)] + { + assert!(seq.list().is_ok()); + } }); } @@ -822,7 +848,15 @@ mod tests { let v = ("foo", "bar"); let ob = v.to_object(py); let seq = ob.downcast::(py).unwrap(); - assert!(seq.tuple().is_ok()); + assert!(seq + .to_tuple() + .unwrap() + .eq(PyTuple::new(py, &["foo", "bar"])) + .unwrap()); + #[allow(deprecated)] + { + assert!(seq.tuple().is_ok()); + } }); } @@ -832,7 +866,11 @@ mod tests { let v = vec!["foo", "bar"]; let ob = v.to_object(py); let seq = ob.downcast::(py).unwrap(); - assert!(seq.tuple().is_ok()); + assert!(seq.to_tuple().unwrap().eq(PyTuple::new(py, &v)).unwrap()); + #[allow(deprecated)] + { + assert!(seq.tuple().is_ok()); + } }); } @@ -876,7 +914,7 @@ mod tests { let seq = ob.downcast::(py).unwrap(); let type_ptr = seq.as_ref(); let seq_from = unsafe { type_ptr.downcast_unchecked::() }; - assert!(seq_from.list().is_ok()); + assert!(seq_from.to_list().is_ok()); }); } diff --git a/src/types/tuple.rs b/src/types/tuple.rs index a7183728723..3a124d3690e 100644 --- a/src/types/tuple.rs +++ b/src/types/tuple.rs @@ -217,10 +217,10 @@ impl PyTuple { /// Return a new list containing the contents of this tuple; equivalent to the Python expression `list(tuple)`. /// - /// This method is equivalent to `self.as_sequence().list()` and faster than `PyList::new(py, self)`. + /// This method is equivalent to `self.as_sequence().to_list()` and faster than `PyList::new(py, self)`. pub fn to_list(&self) -> &PyList { self.as_sequence() - .list() + .to_list() .expect("failed to convert tuple to list") } }