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

move IntoPy::type_output to IntoPyObject::type_output #4657

Merged
merged 3 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/4657.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`IntoPy::type_output` moved to `IntoPyObject::type_output`
34 changes: 18 additions & 16 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,6 @@ pub trait ToPyObject {
pub trait IntoPy<T>: Sized {
/// Performs the conversion.
fn into_py(self, py: Python<'_>) -> T;

/// Extracts the type hint information for this type when it appears as a return value.
///
/// For example, `Vec<u32>` would return `List[int]`.
/// The default implementation returns `Any`, which is correct for any type.
///
/// For most types, the return value for this method will be identical to that of [`FromPyObject::type_input`].
/// It may be different for some types, such as `Dict`, to allow duck-typing: functions return `Dict` but take `Mapping` as argument.
#[cfg(feature = "experimental-inspect")]
fn type_output() -> TypeInfo {
TypeInfo::Any
}
}

/// Defines a conversion from a Rust type to a Python object, which may fail.
Expand Down Expand Up @@ -205,6 +193,18 @@ pub trait IntoPyObject<'py>: Sized {
/// Performs the conversion.
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error>;

/// Extracts the type hint information for this type when it appears as a return value.
///
/// For example, `Vec<u32>` would return `List[int]`.
/// The default implementation returns `Any`, which is correct for any type.
///
/// For most types, the return value for this method will be identical to that of [`FromPyObject::type_input`].
/// It may be different for some types, such as `Dict`, to allow duck-typing: functions return `Dict` but take `Mapping` as argument.
#[cfg(feature = "experimental-inspect")]
fn type_output() -> TypeInfo {
TypeInfo::Any
}

/// Converts sequence of Self into a Python object. Used to specialize `Vec<u8>`, `[u8; N]`
/// and `SmallVec<[u8; N]>` as a sequence of bytes into a `bytes` object.
#[doc(hidden)]
Expand Down Expand Up @@ -379,8 +379,9 @@ pub trait FromPyObject<'py>: Sized {
/// For example, `Vec<u32>` would return `Sequence[int]`.
/// The default implementation returns `Any`, which is correct for any type.
///
/// For most types, the return value for this method will be identical to that of [`IntoPy::type_output`].
/// It may be different for some types, such as `Dict`, to allow duck-typing: functions return `Dict` but take `Mapping` as argument.
/// For most types, the return value for this method will be identical to that of
/// [`IntoPyObject::type_output`]. It may be different for some types, such as `Dict`,
/// to allow duck-typing: functions return `Dict` but take `Mapping` as argument.
#[cfg(feature = "experimental-inspect")]
fn type_input() -> TypeInfo {
TypeInfo::Any
Expand Down Expand Up @@ -440,8 +441,9 @@ pub trait FromPyObjectBound<'a, 'py>: Sized + from_py_object_bound_sealed::Seale
/// For example, `Vec<u32>` would return `Sequence[int]`.
/// The default implementation returns `Any`, which is correct for any type.
///
/// For most types, the return value for this method will be identical to that of [`IntoPy::type_output`].
/// It may be different for some types, such as `Dict`, to allow duck-typing: functions return `Dict` but take `Mapping` as argument.
/// For most types, the return value for this method will be identical to that of
/// [`IntoPyObject::type_output`]. It may be different for some types, such as `Dict`,
/// to allow duck-typing: functions return `Dict` but take `Mapping` as argument.
#[cfg(feature = "experimental-inspect")]
fn type_input() -> TypeInfo {
TypeInfo::Any
Expand Down
16 changes: 11 additions & 5 deletions src/conversions/smallvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ where
let list = new_from_iter(py, &mut iter);
list.into()
}

#[cfg(feature = "experimental-inspect")]
fn type_output() -> TypeInfo {
TypeInfo::list_of(A::Item::type_output())
}
}

impl<'py, A> IntoPyObject<'py> for SmallVec<A>
Expand All @@ -75,12 +70,18 @@ where
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
<A::Item>::owned_sequence_into_pyobject(self, py, crate::conversion::private::Token)
}

#[cfg(feature = "experimental-inspect")]
fn type_output() -> TypeInfo {
TypeInfo::list_of(A::Item::type_output())
}
}

impl<'a, 'py, A> IntoPyObject<'py> for &'a SmallVec<A>
where
A: Array,
&'a A::Item: IntoPyObject<'py>,
A::Item: 'a, // MSRV
{
type Target = PyAny;
type Output = Bound<'py, Self::Target>;
Expand All @@ -90,6 +91,11 @@ where
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
self.as_slice().into_pyobject(py)
}

#[cfg(feature = "experimental-inspect")]
fn type_output() -> TypeInfo {
TypeInfo::list_of(<&A::Item>::type_output())
}
}

impl<'py, A> FromPyObject<'py> for SmallVec<A>
Expand Down
34 changes: 24 additions & 10 deletions src/conversions/std/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ where
}
dict.into_any().unbind()
}

#[cfg(feature = "experimental-inspect")]
fn type_output() -> TypeInfo {
TypeInfo::dict_of(K::type_output(), V::type_output())
}
}

impl<'py, K, V, H> IntoPyObject<'py> for collections::HashMap<K, V, H>
Expand All @@ -79,12 +74,19 @@ where
}
Ok(dict)
}

#[cfg(feature = "experimental-inspect")]
fn type_output() -> TypeInfo {
TypeInfo::dict_of(K::type_output(), V::type_output())
}
}

impl<'a, 'py, K, V, H> IntoPyObject<'py> for &'a collections::HashMap<K, V, H>
where
&'a K: IntoPyObject<'py> + cmp::Eq + hash::Hash,
&'a V: IntoPyObject<'py>,
K: 'a, // MSRV
V: 'a, // MSRV
H: hash::BuildHasher,
{
type Target = PyDict;
Expand All @@ -98,6 +100,11 @@ where
}
Ok(dict)
}

#[cfg(feature = "experimental-inspect")]
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 @@ -112,11 +119,6 @@ where
}
dict.into_any().unbind()
}

#[cfg(feature = "experimental-inspect")]
fn type_output() -> TypeInfo {
TypeInfo::dict_of(K::type_output(), V::type_output())
}
}

impl<'py, K, V> IntoPyObject<'py> for collections::BTreeMap<K, V>
Expand All @@ -135,12 +137,19 @@ where
}
Ok(dict)
}

#[cfg(feature = "experimental-inspect")]
fn type_output() -> TypeInfo {
TypeInfo::dict_of(K::type_output(), V::type_output())
}
}

impl<'a, 'py, K, V> IntoPyObject<'py> for &'a collections::BTreeMap<K, V>
where
&'a K: IntoPyObject<'py> + cmp::Eq,
&'a V: IntoPyObject<'py>,
K: 'a,
V: 'a,
{
type Target = PyDict;
type Output = Bound<'py, Self::Target>;
Expand All @@ -153,6 +162,11 @@ where
}
Ok(dict)
}

#[cfg(feature = "experimental-inspect")]
fn type_output() -> TypeInfo {
TypeInfo::dict_of(<&K>::type_output(), <&V>::type_output())
}
}

impl<'py, K, V, S> FromPyObject<'py> for collections::HashMap<K, V, S>
Expand Down
Loading
Loading