Skip to content

Commit

Permalink
Declare the Python type of Rust collections
Browse files Browse the repository at this point in the history
  • Loading branch information
CLOVIS-AI committed Aug 1, 2022
1 parent fa4f51d commit dc4ad08
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/inspect/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ mod test {
mod conversion {
use crate::inspect::types::test::assert_display;
use crate::{FromPyObject, IntoPy};
use std::collections::{HashMap, HashSet};

#[test]
fn unsigned_int() {
Expand Down Expand Up @@ -459,4 +460,18 @@ mod conversion {
assert_display(&<&[u8]>::type_output(), "bytes");
assert_display(&<&[u8]>::type_input(), "bytes");
}

#[test]
fn collections() {
assert_display(&<Vec<usize>>::type_output(), "List[int]");
assert_display(&<Vec<usize>>::type_input(), "Sequence[int]");

assert_display(&<HashSet<usize>>::type_output(), "Set[int]");
assert_display(&<HashSet<usize>>::type_input(), "Set[int]");

assert_display(&<HashMap<usize, f32>>::type_output(), "Dict[int, float]");
assert_display(&<HashMap<usize, f32>>::type_input(), "Mapping[int, float]");

assert_display(&<(usize, f32)>::type_input(), "Tuple[int, float]");
}
}
17 changes: 17 additions & 0 deletions src/types/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use super::PyMapping;
use crate::err::{self, PyErr, PyResult};
use crate::ffi::Py_ssize_t;
use crate::inspect::types::TypeInfo;
use crate::types::{PyAny, PyList};
#[cfg(not(PyPy))]
use crate::IntoPyPointer;
Expand Down Expand Up @@ -361,6 +362,10 @@ where
.map(|(k, v)| (k.into_py(py), v.into_py(py)));
IntoPyDict::into_py_dict(iter, py).into()
}

fn type_output() -> TypeInfo {
TypeInfo::dict_of(K::type_output(), V::type_output())
}
}

impl<K, V> IntoPy<PyObject> for collections::BTreeMap<K, V>
Expand All @@ -374,6 +379,10 @@ where
.map(|(k, v)| (k.into_py(py), v.into_py(py)));
IntoPyDict::into_py_dict(iter, py).into()
}

fn type_output() -> TypeInfo {
TypeInfo::dict_of(K::type_output(), V::type_output())
}
}

/// Conversion trait that allows a sequence of tuples to be converted into `PyDict`
Expand Down Expand Up @@ -451,6 +460,10 @@ where
}
Ok(ret)
}

fn type_input() -> TypeInfo {
TypeInfo::mapping_of(K::type_input(), V::type_input())
}
}

impl<'source, K, V> FromPyObject<'source> for BTreeMap<K, V>
Expand All @@ -466,6 +479,10 @@ where
}
Ok(ret)
}

fn type_input() -> TypeInfo {
TypeInfo::mapping_of(K::type_input(), V::type_input())
}
}

#[cfg(test)]
Expand Down
5 changes: 5 additions & 0 deletions src/types/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::convert::TryInto;

use crate::err::{self, PyResult};
use crate::ffi::{self, Py_ssize_t};
use crate::inspect::types::TypeInfo;
use crate::internal_tricks::get_ssize_index;
use crate::types::PySequence;
use crate::{
Expand Down Expand Up @@ -368,6 +369,10 @@ where
let list = new_from_iter(py, &mut iter);
list.into()
}

fn type_output() -> TypeInfo {
TypeInfo::list_of(T::type_output())
}
}

#[cfg(test)]
Expand Down
5 changes: 5 additions & 0 deletions src/types/sequence.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2017-present PyO3 Project and Contributors
use crate::err::{self, PyDowncastError, PyErr, PyResult};
use crate::exceptions::PyValueError;
use crate::inspect::types::TypeInfo;
use crate::internal_tricks::get_ssize_index;
use crate::types::{PyAny, PyList, PyString, PyTuple};
use crate::{ffi, PyNativeType, ToPyObject};
Expand Down Expand Up @@ -275,6 +276,10 @@ where
}
extract_sequence(obj)
}

fn type_input() -> TypeInfo {
TypeInfo::sequence_of(T::type_input())
}
}

fn extract_sequence<'s, T>(obj: &'s PyAny) -> PyResult<Vec<T>>
Expand Down
17 changes: 17 additions & 0 deletions src/types/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//

use crate::err::{self, PyErr, PyResult};
use crate::inspect::types::TypeInfo;
#[cfg(Py_LIMITED_API)]
use crate::types::PyIterator;
use crate::{ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyObject, Python, ToPyObject};
Expand Down Expand Up @@ -271,6 +272,10 @@ where
}
set.into()
}

fn type_output() -> TypeInfo {
TypeInfo::set_of(K::type_output())
}
}

impl<'source, K, S> FromPyObject<'source> for HashSet<K, S>
Expand All @@ -282,6 +287,10 @@ where
let set: &PySet = ob.downcast()?;
set.iter().map(K::extract).collect()
}

fn type_input() -> TypeInfo {
TypeInfo::set_of(K::type_input())
}
}

impl<K> IntoPy<PyObject> for BTreeSet<K>
Expand All @@ -297,6 +306,10 @@ where
}
set.into()
}

fn type_output() -> TypeInfo {
TypeInfo::set_of(K::type_output())
}
}

impl<'source, K> FromPyObject<'source> for BTreeSet<K>
Expand All @@ -307,6 +320,10 @@ where
let set: &PySet = ob.downcast()?;
set.iter().map(K::extract).collect()
}

fn type_input() -> TypeInfo {
TypeInfo::set_of(K::type_input())
}
}

#[cfg(test)]
Expand Down
13 changes: 13 additions & 0 deletions src/types/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::convert::TryInto;

use crate::ffi::{self, Py_ssize_t};
use crate::inspect::types::TypeInfo;
use crate::internal_tricks::get_ssize_index;
use crate::types::PySequence;
use crate::{
Expand Down Expand Up @@ -293,6 +294,10 @@ macro_rules! tuple_conversion ({$length:expr,$(($refN:ident, $n:tt, $T:ident)),+
ret
}
}

fn type_output() -> TypeInfo {
TypeInfo::Tuple(Some(vec![$( $T::type_output() ),+]))
}
}

impl <$($T: IntoPy<PyObject>),+> IntoPy<Py<PyTuple>> for ($($T,)+) {
Expand All @@ -304,6 +309,10 @@ macro_rules! tuple_conversion ({$length:expr,$(($refN:ident, $n:tt, $T:ident)),+
ret
}
}

fn type_output() -> TypeInfo {
TypeInfo::Tuple(Some(vec![$( $T::type_output() ),+]))
}
}

impl<'s, $($T: FromPyObject<'s>),+> FromPyObject<'s> for ($($T,)+) {
Expand All @@ -320,6 +329,10 @@ macro_rules! tuple_conversion ({$length:expr,$(($refN:ident, $n:tt, $T:ident)),+
Err(wrong_tuple_length(t, $length))
}
}

fn type_input() -> TypeInfo {
TypeInfo::Tuple(Some(vec![$( $T::type_input() ),+]))
}
}
});

Expand Down

0 comments on commit dc4ad08

Please sign in to comment.