Skip to content

Commit

Permalink
wip:
Browse files Browse the repository at this point in the history
  • Loading branch information
op8867555 committed Feb 10, 2023
1 parent b50ff37 commit 34f3fb9
Show file tree
Hide file tree
Showing 15 changed files with 212 additions and 202 deletions.
23 changes: 17 additions & 6 deletions pyo3-macros-backend/src/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub(crate) fn generate_class_fields(
&_pyo3::inspect::fields::FieldInfo {
name: #name,
kind: _pyo3::inspect::fields::FieldKind::Getter,
py_type: ::std::option::Option::Some(|| <#typ as _pyo3::inspect::types::WithTypeInfo>::type_output()),
py_type: ::std::option::Option::Some(|| (&&&_pyo3::inspect::types::Typed::<#typ>::new()).type_output()),
arguments: &[],
}
});
Expand All @@ -54,7 +54,7 @@ pub(crate) fn generate_class_fields(
_pyo3::inspect::fields::ArgumentInfo {
name: #name,
kind: _pyo3::inspect::fields::ArgumentKind::Position,
py_type: ::std::option::Option::Some(|| <#typ as _pyo3::inspect::types::WithTypeInfo>::type_output()),
py_type: ::std::option::Option::Some(|| (&&&_pyo3::inspect::types::Type::<#typ>::new()).type_output()),
default_value: false,
is_modified: false,
}
Expand Down Expand Up @@ -96,7 +96,10 @@ pub(crate) fn generate_class_inspection(
&_pyo3::inspect::fields::FieldInfo {
name: #name,
kind: _pyo3::inspect::fields::FieldKind::Getter,
py_type: ::std::option::Option::Some(|| <#typ as _pyo3::inspect::types::WithTypeInfo>::type_output()),
py_type: ::std::option::Option::Some(|| {
use _pyo3::inspect::types::{WithTypeInfo, WithCustomTypeInfo};
(&&&_pyo3::inspect::types::Typed::<#typ>::new()).type_output()
}),
arguments: &[],
}
});
Expand All @@ -112,7 +115,10 @@ pub(crate) fn generate_class_inspection(
_pyo3::inspect::fields::ArgumentInfo {
name: #name,
kind: _pyo3::inspect::fields::ArgumentKind::Position,
py_type: ::std::option::Option::Some(|| <#typ as _pyo3::inspect::types::WithTypeInfo>::type_output()),
py_type: ::std::option::Option::Some(||{
use _pyo3::inspect::types::{WithTypeInfo, WithCustomTypeInfo};
(&&&_pyo3::inspect::types::Typed::<#typ>::new()).type_output()
}),
default_value: false,
is_modified: false,
}
Expand Down Expand Up @@ -229,7 +235,10 @@ pub(crate) fn generate_fields_inspection(
_pyo3::inspect::fields::ArgumentInfo {
name: #name,
kind: _pyo3::inspect::fields::ArgumentKind::PositionOrKeyword, //TODO
py_type: ::std::option::Option::Some(|| <#typ as _pyo3::inspect::types::WithTypeInfo>::type_input()),
py_type: ::std::option::Option::Some(|| {
use _pyo3::inspect::types::{WithTypeInfo, WithCustomTypeInfo};
(&&&_pyo3::inspect::types::Typed::<#typ>::new()).type_input()
}),
default_value: false,
is_modified: #is_mutable,
}
Expand All @@ -239,8 +248,10 @@ pub(crate) fn generate_fields_inspection(
let args_size = Literal::usize_suffixed(args.len());

let output = quote! {

fn #field_type_fn_name() -> _pyo3::inspect::types::TypeInfo {
<#field_type as _pyo3::inspect::types::WithTypeInfo>::type_output()
use _pyo3::inspect::types::{WithTypeInfo, WithCustomTypeInfo};
(&&&_pyo3::inspect::types::Typed::<#field_type>::new()).type_output()
}

const #field_args_name: [_pyo3::inspect::fields::ArgumentInfo<'static>; #args_size] = [
Expand Down
42 changes: 21 additions & 21 deletions pyo3-macros-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,27 +754,27 @@ fn impl_pytypeinfo(
};

quote! {
// XXX: Not sure why blanket impl<T:PyTypeInfo> not working for pyclasses,
// So I generate WithTypeInfo for them here
use ::std::convert::From;

#[cfg(feature="experimental-inspect")]
impl _pyo3::inspect::types::WithTypeInfo for #cls {
fn type_output() -> _pyo3::inspect::types::TypeInfo {
_pyo3::inspect::types::TypeInfo::Class {
name: ::std::borrow::Cow::from(#cls_name),
module: _pyo3::inspect::types::ModuleName::Builtin,
type_vars: ::std::vec::Vec::new(),
}
}
fn type_input() -> _pyo3::inspect::types::TypeInfo {
_pyo3::inspect::types::TypeInfo::Class {
name: ::std::borrow::Cow::from(#cls_name),
module: _pyo3::inspect::types::ModuleName::Builtin,
type_vars: ::std::vec::Vec::new(),
}
}
}
// // XXX: Not sure why blanket impl<T:PyTypeInfo> not working for pyclasses,
// // So I generate WithTypeInfo for them here
// use ::std::convert::From;

// #[cfg(feature="experimental-inspect")]
// impl _pyo3::inspect::types::WithTypeInfo for _pyo3::inspect::types::Typed<#cls> {
// fn type_output(&self) -> _pyo3::inspect::types::TypeInfo {
// _pyo3::inspect::types::TypeInfo::Class {
// name: ::std::borrow::Cow::from(#cls_name),
// module: _pyo3::inspect::types::ModuleName::Builtin,
// type_vars: ::std::vec::Vec::new(),
// }
// }
// fn type_input(&self) -> _pyo3::inspect::types::TypeInfo {
// _pyo3::inspect::types::TypeInfo::Class {
// name: ::std::borrow::Cow::from(#cls_name),
// module: _pyo3::inspect::types::ModuleName::Builtin,
// type_vars: ::std::vec::Vec::new(),
// }
// }
// }

unsafe impl _pyo3::type_object::PyTypeInfo for #cls {
type AsRefTarget = _pyo3::PyCell<Self>;
Expand Down
12 changes: 6 additions & 6 deletions src/conversions/std/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod min_const_generics {
use super::invalid_sequence_length;
use crate::conversion::{AsPyPointer, IntoPyPointer};
#[cfg(feature = "experimental-inspect")]
use crate::inspect::types::{TypeInfo, WithTypeInfo};
use crate::inspect::types::{TypeInfo, WithTypeInfo, Typed};
use crate::types::PySequence;
use crate::{
ffi, FromPyObject, IntoPy, Py, PyAny, PyDowncastError, PyObject, PyResult, Python,
Expand Down Expand Up @@ -64,14 +64,14 @@ mod min_const_generics {
#[cfg(feature = "experimental-inspect")]
impl<const N: usize, T> WithTypeInfo for [T; N]
where
T: WithTypeInfo,
Typed<T>: WithTypeInfo,
{
fn type_output() -> TypeInfo {
TypeInfo::sequence_of(T::type_output())
fn type_output(&self) -> TypeInfo {
TypeInfo::sequence_of((&&Typed::<T>::new()).type_output())
}

fn type_input() -> TypeInfo {
TypeInfo::sequence_of(T::type_input())
fn type_input(&self) -> TypeInfo {
TypeInfo::sequence_of((&&Typed::<T>::new()).type_input())
}
}

Expand Down
44 changes: 26 additions & 18 deletions src/conversions/std/map.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{cmp, collections, hash};

#[cfg(feature = "experimental-inspect")]
use crate::inspect::types::{WithTypeInfo, TypeInfo};
use crate::inspect::types::{TypeInfo, Typed, WithTypeInfo};

use crate::{
types::{IntoPyDict, PyDict},
Expand Down Expand Up @@ -43,7 +43,6 @@ where
}
}


impl<K, V> IntoPy<PyObject> for collections::BTreeMap<K, V>
where
K: cmp::Eq + IntoPy<PyObject>,
Expand All @@ -57,7 +56,6 @@ where
}
}


impl<'source, K, V, S> FromPyObject<'source> for collections::HashMap<K, V, S>
where
K: FromPyObject<'source> + cmp::Eq + hash::Hash,
Expand All @@ -72,7 +70,6 @@ where
}
Ok(ret)
}

}

impl<'source, K, V> FromPyObject<'source> for collections::BTreeMap<K, V>
Expand All @@ -91,34 +88,45 @@ where
}

#[cfg(feature = "experimental-inspect")]
impl<K, V, H> WithTypeInfo for collections::HashMap<K, V, H>
impl<K, V, H> WithTypeInfo for &Typed<collections::HashMap<K, V, H>>
where
K: WithTypeInfo,
V: WithTypeInfo,
Typed<K>: WithTypeInfo,
Typed<V>: WithTypeInfo,
{
fn type_output() -> TypeInfo {
TypeInfo::dict_of(K::type_output(), V::type_output())
fn type_output(&self) -> TypeInfo {
TypeInfo::dict_of(
(&&Typed::<K>::new()).type_output(),
(&&Typed::<V>::new()).type_output(),
)
}
fn type_input() -> TypeInfo {
TypeInfo::mapping_of(K::type_input(), V::type_input())
fn type_input(&self) -> TypeInfo {
TypeInfo::mapping_of(
(&&Typed::<K>::new()).type_output(),
(&&Typed::<V>::new()).type_output(),
)
}
}

#[cfg(feature = "experimental-inspect")]
impl<K, V> WithTypeInfo for collections::BTreeMap<K, V>
where
K: WithTypeInfo,
V: WithTypeInfo,
Typed<K>: WithTypeInfo,
Typed<V>: WithTypeInfo,
{
fn type_output() -> TypeInfo {
TypeInfo::dict_of(K::type_output(), V::type_output())
fn type_output(&self) -> TypeInfo {
TypeInfo::dict_of(
(&&Typed::<K>::new()).type_output(),
(&&Typed::<V>::new()).type_output(),
)
}
fn type_input() -> TypeInfo {
TypeInfo::mapping_of(K::type_input(), V::type_input())
fn type_input(&self) -> TypeInfo {
TypeInfo::mapping_of(
(&&Typed::<K>::new()).type_output(),
(&&Typed::<V>::new()).type_output(),
)
}
}


#[cfg(test)]
mod tests {
use super::*;
Expand Down
48 changes: 17 additions & 31 deletions src/conversions/std/num.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[cfg(feature = "experimental-inspect")]
use crate::inspect::types::{WithTypeInfo, TypeInfo};
use crate::inspect::types::{WithTypeInfo, TypeInfo, Typed};
use crate::{
exceptions, ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyErr, PyObject, PyResult, Python,
ToPyObject,
Expand Down Expand Up @@ -34,13 +34,13 @@ macro_rules! int_fits_larger_int {

}
#[cfg(feature = "experimental-inspect")]
impl WithTypeInfo for $rust_type {
fn type_output() -> TypeInfo {
<$larger_type>::type_output()
impl WithTypeInfo for Typed<$rust_type> {
fn type_output(&self) -> TypeInfo {
Typed::<$larger_type>::new().type_output()
}

fn type_input() -> TypeInfo {
<$larger_type>::type_input()
fn type_input(&self) -> TypeInfo {
Typed::<$larger_type>::new().type_input()
}
}
};
Expand Down Expand Up @@ -77,15 +77,10 @@ macro_rules! int_convert_u64_or_i64 {
}
}
#[cfg(feature = "experimental-inspect")]
impl WithTypeInfo for $rust_type {
fn type_output() -> TypeInfo {
impl WithTypeInfo for Typed<$rust_type> {
fn type_output(&self) -> TypeInfo {
TypeInfo::builtin("int")
}

#[cfg(feature = "experimental-inspect")]
fn type_input() -> TypeInfo {
Self::type_output()
}
}
};
}
Expand Down Expand Up @@ -122,13 +117,10 @@ macro_rules! int_fits_c_long {
}
}
#[cfg(feature = "experimental-inspect")]
impl WithTypeInfo for $rust_type {
fn type_output() -> TypeInfo {
impl WithTypeInfo for Typed<$rust_type> {
fn type_output(&self) -> TypeInfo {
TypeInfo::builtin("int")
}
fn type_input() -> TypeInfo {
Self::type_output()
}
}

};
Expand Down Expand Up @@ -220,13 +212,10 @@ mod fast_128bit_int_conversion {
}

#[cfg(feature = "experimental-inspect")]
impl WithTypeInfo for $rust_type {
fn type_output() -> TypeInfo {
impl WithTypeInfo for Typed<$rust_type> {
fn type_output(&self) -> TypeInfo {
TypeInfo::builtin("int")
}
fn type_input() -> TypeInfo {
Self::type_output()
}
}
};
}
Expand Down Expand Up @@ -290,13 +279,10 @@ mod slow_128bit_int_conversion {
}
}
#[cfg(feature = "experimental-inspect")]
impl WithTypeInfo for $rust_type {
fn type_output() -> TypeInfo {
impl WithTypeInfo for Typed<$rust_type> {
fn type_output(&self) -> TypeInfo {
TypeInfo::builtin("int")
}
fn type_input() -> TypeInfo {
Self::type_output()
}
}
};
}
Expand Down Expand Up @@ -343,9 +329,9 @@ macro_rules! nonzero_int_impl {
}

#[cfg(feature = "experimental-inspect")]
impl WithTypeInfo for $nonzero_type {
fn type_input() -> TypeInfo {
<$primitive_type>::type_input()
impl WithTypeInfo for Typed<$nonzero_type> {
fn type_input(&self) -> TypeInfo {
Typed::<$primitive_type>::new().type_input()
}
}
};
Expand Down
28 changes: 15 additions & 13 deletions src/conversions/std/set.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{cmp, collections, hash};

#[cfg(feature = "experimental-inspect")]
use crate::inspect::types::{WithTypeInfo, TypeInfo};
use crate::inspect::types::{WithTypeInfo, TypeInfo, Typed};
use crate::{
types::set::new_from_iter, types::PySet, FromPyObject, IntoPy,
PyAny, PyObject, PyResult, Python, ToPyObject,
Expand Down Expand Up @@ -43,17 +43,17 @@ where
}

#[cfg(feature = "experimental-inspect")]
impl<K, S> WithTypeInfo for collections::HashSet<K, S>
impl<K, S> WithTypeInfo for &Typed<collections::HashSet<K, S>>
where
K: WithTypeInfo,
Typed<K>: WithTypeInfo,
{
fn type_output() -> TypeInfo {
TypeInfo::set_of(K::type_output())
fn type_output(&self) -> TypeInfo {
TypeInfo::set_of((&&Typed::<K>::new()).type_output())
}

#[cfg(feature = "experimental-inspect")]
fn type_input() -> TypeInfo {
TypeInfo::set_of(K::type_input())
fn type_input(&self) -> TypeInfo {
TypeInfo::set_of((&&Typed::<K>::new()).type_input())
}
}

Expand Down Expand Up @@ -90,15 +90,17 @@ where
}

#[cfg(feature = "experimental-inspect")]
impl<K> WithTypeInfo for collections::BTreeSet<K>
impl<K> WithTypeInfo for &Typed<collections::BTreeSet<K>>
where
K: WithTypeInfo,
Typed<K>: WithTypeInfo,
{
fn type_output() -> TypeInfo {
TypeInfo::set_of(K::type_output())
fn type_output(&self) -> TypeInfo {
TypeInfo::set_of((&&Typed::<K>::new()).type_output())
}
fn type_input() -> TypeInfo {
TypeInfo::set_of(K::type_input())

#[cfg(feature = "experimental-inspect")]
fn type_input(&self) -> TypeInfo {
TypeInfo::set_of((&&Typed::<K>::new()).type_input())
}
}

Expand Down
Loading

0 comments on commit 34f3fb9

Please sign in to comment.