From 34f3fb91ef9080c0762f6050c78cd22148fde82b Mon Sep 17 00:00:00 2001 From: Ming-Yang Lu Date: Fri, 10 Feb 2023 23:06:28 +0800 Subject: [PATCH] wip: --- pyo3-macros-backend/src/inspect.rs | 23 +++-- pyo3-macros-backend/src/pyclass.rs | 42 ++++----- src/conversions/std/array.rs | 12 +-- src/conversions/std/map.rs | 44 ++++++---- src/conversions/std/num.rs | 48 ++++------ src/conversions/std/set.rs | 28 +++--- src/conversions/std/slice.rs | 9 +- src/conversions/std/string.rs | 10 +-- src/conversions/std/vec.rs | 14 +-- src/inspect/classes.rs | 6 +- src/inspect/types.rs | 136 ++++++++++++++++------------- src/types/boolobject.rs | 10 +-- src/types/floatob.rs | 16 ++-- src/types/set.rs | 2 - src/types/tuple.rs | 14 +-- 15 files changed, 212 insertions(+), 202 deletions(-) diff --git a/pyo3-macros-backend/src/inspect.rs b/pyo3-macros-backend/src/inspect.rs index 095c111dd1e..b26a832940e 100644 --- a/pyo3-macros-backend/src/inspect.rs +++ b/pyo3-macros-backend/src/inspect.rs @@ -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: &[], } }); @@ -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, } @@ -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: &[], } }); @@ -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, } @@ -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, } @@ -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] = [ diff --git a/pyo3-macros-backend/src/pyclass.rs b/pyo3-macros-backend/src/pyclass.rs index 2cf1e8039cd..9b8b642418f 100644 --- a/pyo3-macros-backend/src/pyclass.rs +++ b/pyo3-macros-backend/src/pyclass.rs @@ -754,27 +754,27 @@ fn impl_pytypeinfo( }; quote! { - // XXX: Not sure why blanket impl 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 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; diff --git a/src/conversions/std/array.rs b/src/conversions/std/array.rs index 0b8b448ac44..ff5201528e8 100644 --- a/src/conversions/std/array.rs +++ b/src/conversions/std/array.rs @@ -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, @@ -64,14 +64,14 @@ mod min_const_generics { #[cfg(feature = "experimental-inspect")] impl WithTypeInfo for [T; N] where - T: WithTypeInfo, + Typed: WithTypeInfo, { - fn type_output() -> TypeInfo { - TypeInfo::sequence_of(T::type_output()) + fn type_output(&self) -> TypeInfo { + TypeInfo::sequence_of((&&Typed::::new()).type_output()) } - fn type_input() -> TypeInfo { - TypeInfo::sequence_of(T::type_input()) + fn type_input(&self) -> TypeInfo { + TypeInfo::sequence_of((&&Typed::::new()).type_input()) } } diff --git a/src/conversions/std/map.rs b/src/conversions/std/map.rs index a9fc9697da8..7f5bf048d13 100644 --- a/src/conversions/std/map.rs +++ b/src/conversions/std/map.rs @@ -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}, @@ -43,7 +43,6 @@ where } } - impl IntoPy for collections::BTreeMap where K: cmp::Eq + IntoPy, @@ -57,7 +56,6 @@ where } } - impl<'source, K, V, S> FromPyObject<'source> for collections::HashMap where K: FromPyObject<'source> + cmp::Eq + hash::Hash, @@ -72,7 +70,6 @@ where } Ok(ret) } - } impl<'source, K, V> FromPyObject<'source> for collections::BTreeMap @@ -91,34 +88,45 @@ where } #[cfg(feature = "experimental-inspect")] -impl WithTypeInfo for collections::HashMap +impl WithTypeInfo for &Typed> where - K: WithTypeInfo, - V: WithTypeInfo, + Typed: WithTypeInfo, + Typed: WithTypeInfo, { - fn type_output() -> TypeInfo { - TypeInfo::dict_of(K::type_output(), V::type_output()) + fn type_output(&self) -> TypeInfo { + TypeInfo::dict_of( + (&&Typed::::new()).type_output(), + (&&Typed::::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::::new()).type_output(), + (&&Typed::::new()).type_output(), + ) } } #[cfg(feature = "experimental-inspect")] impl WithTypeInfo for collections::BTreeMap where - K: WithTypeInfo, - V: WithTypeInfo, + Typed: WithTypeInfo, + Typed: WithTypeInfo, { - fn type_output() -> TypeInfo { - TypeInfo::dict_of(K::type_output(), V::type_output()) + fn type_output(&self) -> TypeInfo { + TypeInfo::dict_of( + (&&Typed::::new()).type_output(), + (&&Typed::::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::::new()).type_output(), + (&&Typed::::new()).type_output(), + ) } } - #[cfg(test)] mod tests { use super::*; diff --git a/src/conversions/std/num.rs b/src/conversions/std/num.rs index 13330231937..4636c5107cb 100644 --- a/src/conversions/std/num.rs +++ b/src/conversions/std/num.rs @@ -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, @@ -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() } } }; @@ -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() - } } }; } @@ -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() - } } }; @@ -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() - } } }; } @@ -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() - } } }; } @@ -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() } } }; diff --git a/src/conversions/std/set.rs b/src/conversions/std/set.rs index 83d04d91b88..896aac68636 100644 --- a/src/conversions/std/set.rs +++ b/src/conversions/std/set.rs @@ -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, @@ -43,17 +43,17 @@ where } #[cfg(feature = "experimental-inspect")] -impl WithTypeInfo for collections::HashSet +impl WithTypeInfo for &Typed> where - K: WithTypeInfo, + Typed: WithTypeInfo, { - fn type_output() -> TypeInfo { - TypeInfo::set_of(K::type_output()) + fn type_output(&self) -> TypeInfo { + TypeInfo::set_of((&&Typed::::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::::new()).type_input()) } } @@ -90,15 +90,17 @@ where } #[cfg(feature = "experimental-inspect")] -impl WithTypeInfo for collections::BTreeSet +impl WithTypeInfo for &Typed> where - K: WithTypeInfo, + Typed: WithTypeInfo, { - fn type_output() -> TypeInfo { - TypeInfo::set_of(K::type_output()) + fn type_output(&self) -> TypeInfo { + TypeInfo::set_of((&&Typed::::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::::new()).type_input()) } } diff --git a/src/conversions/std/slice.rs b/src/conversions/std/slice.rs index 68a4154feac..514a73afdd6 100644 --- a/src/conversions/std/slice.rs +++ b/src/conversions/std/slice.rs @@ -1,5 +1,5 @@ #[cfg(feature = "experimental-inspect")] -use crate::inspect::types::{WithTypeInfo, TypeInfo}; +use crate::inspect::types::{WithTypeInfo, TypeInfo, Typed}; use crate::{ types::PyBytes, FromPyObject, IntoPy, PyAny, PyObject, PyResult, Python, ToPyObject, @@ -18,13 +18,10 @@ impl<'a> FromPyObject<'a> for &'a [u8] { } #[cfg(feature = "experimental-inspect")] -impl<'a> WithTypeInfo for &'a [u8] { - fn type_output() -> TypeInfo { +impl<'a> WithTypeInfo for &Typed<&'a [u8]> { + fn type_output(&self) -> TypeInfo { TypeInfo::builtin("bytes") } - fn type_input() -> TypeInfo { - Self::type_output() - } } #[cfg(test)] diff --git a/src/conversions/std/string.rs b/src/conversions/std/string.rs index 67d6041c9a1..6c11171b2e6 100644 --- a/src/conversions/std/string.rs +++ b/src/conversions/std/string.rs @@ -11,10 +11,7 @@ macro_rules! with_typeinfo_impl { ($ty: ty) => { #[cfg(feature = "experimental-inspect")] impl WithTypeInfo for $ty { - fn type_input() -> TypeInfo { - TypeInfo::builtin("str") - } - fn type_output() -> TypeInfo { + fn type_input(&self) -> TypeInfo { TypeInfo::builtin("str") } } @@ -22,10 +19,7 @@ macro_rules! with_typeinfo_impl { ($ty: ty, $param: tt) => { #[cfg(feature = "experimental-inspect")] impl<$param> WithTypeInfo for & $param $ty { - fn type_input() -> TypeInfo { - TypeInfo::builtin("str") - } - fn type_output() -> TypeInfo { + fn type_input(&self) -> TypeInfo { TypeInfo::builtin("str") } } diff --git a/src/conversions/std/vec.rs b/src/conversions/std/vec.rs index 01a55fb578a..9e1c3965cac 100644 --- a/src/conversions/std/vec.rs +++ b/src/conversions/std/vec.rs @@ -1,5 +1,5 @@ #[cfg(feature = "experimental-inspect")] -use crate::inspect::types::{WithTypeInfo, TypeInfo}; +use crate::inspect::types::{WithTypeInfo, TypeInfo, Typed}; use crate::types::list::new_from_iter; use crate::{IntoPy, PyObject, Python, ToPyObject}; @@ -35,13 +35,13 @@ where } #[cfg(feature = "experimental-inspect")] -impl WithTypeInfo for Vec -where T: WithTypeInfo { - fn type_input() -> TypeInfo { - TypeInfo::sequence_of(T::type_input()) +impl WithTypeInfo for &Typed> +where Typed: WithTypeInfo { + fn type_input(&self) -> TypeInfo { + TypeInfo::sequence_of((&&&Typed::::new()).type_input()) } - fn type_output() -> TypeInfo { - TypeInfo::list_of(T::type_output()) + fn type_output(&self) -> TypeInfo { + TypeInfo::sequence_of((&&&Typed::::new()).type_output()) } } diff --git a/src/inspect/classes.rs b/src/inspect/classes.rs index 1091cd68eb0..35771d436a7 100644 --- a/src/inspect/classes.rs +++ b/src/inspect/classes.rs @@ -36,7 +36,11 @@ impl ClassInfo { /// - struct attributes annotated with `#[getter]` or `#[setter]` /// - methods that appear in a `#[pymethods]` block pub fn fields(&self) -> impl Iterator> { - self.class.fields.iter().cloned().chain(self.fields.iter().cloned()) + self.class + .fields + .iter() + .cloned() + .chain(self.fields.iter().cloned()) } } diff --git a/src/inspect/types.rs b/src/inspect/types.rs index 15ef032e855..58a005c8c7f 100644 --- a/src/inspect/types.rs +++ b/src/inspect/types.rs @@ -2,6 +2,15 @@ use std::borrow::Cow; use std::fmt::{Display, Formatter}; +use std::marker::PhantomData; + +pub struct Typed(PhantomData); + +impl Typed { + pub fn new() -> Self { + Self(PhantomData) + } +} pub trait WithTypeInfo { /// Extracts the type hint information for this type when it appears as a return value. @@ -12,9 +21,9 @@ pub trait WithTypeInfo { /// For most types, the return value for this method will be identical to that of [`WithTypeInfo::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 - } + fn type_output(&self) -> TypeInfo { + self.type_input() + } /// Extracts the type hint information for this type when it appears as an argument. /// @@ -24,12 +33,21 @@ pub trait WithTypeInfo { /// For most types, the return value for this method will be identical to that of [`WithTypeInfo::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 + fn type_input(&self) -> TypeInfo { + self.type_output() } - } - +} +pub trait WithCustomTypeInfo{ + #[cfg(feature = "experimental-inspect")] + fn type_output(&self) -> TypeInfo { + self.type_input() + } + #[cfg(feature = "experimental-inspect")] + fn type_input(&self) -> TypeInfo { + self.type_output() + } +} /// Designation of a Python type. /// @@ -314,8 +332,8 @@ impl Display for TypeInfo { } } -impl WithTypeInfo for &T { - fn type_input() -> TypeInfo { +impl WithTypeInfo for Typed { + fn type_input(&self) -> TypeInfo { TypeInfo::Class { name: ::std::borrow::Cow::Borrowed(T::NAME), module: T::MODULE @@ -325,55 +343,49 @@ impl WithTypeInfo for &T { type_vars: ::std::vec::Vec::new(), } } +} - fn type_output() -> TypeInfo { - TypeInfo::Class { - name: ::std::borrow::Cow::Borrowed(T::NAME), - module: T::MODULE - .map(Cow::from) - .map(ModuleName::Module) - .unwrap_or(ModuleName::CurrentModule), - type_vars: ::std::vec::Vec::new(), - } +impl WithTypeInfo for &Typed<&T> +where + Typed: WithTypeInfo, +{ + fn type_input(&self) -> TypeInfo { + (&&Typed::::new()).type_input() + } + fn type_output(&self) -> TypeInfo { + (&&Typed::::new()).type_output() } } -impl WithTypeInfo for crate::class::basic::CompareOp { - fn type_output() -> TypeInfo { - TypeInfo::Any +impl WithTypeInfo for &&Typed +where + T: WithCustomTypeInfo, +{ + fn type_input(&self) -> TypeInfo { + todo!() } - - fn type_input() -> TypeInfo { - TypeInfo::Any + fn type_output(&self) -> TypeInfo { + todo!() } } -impl WithTypeInfo for () { - fn type_output() -> TypeInfo { - TypeInfo::None +impl WithTypeInfo for Typed { + fn type_input(&self) -> TypeInfo { + TypeInfo::Any } +} - fn type_input() -> TypeInfo { +impl WithTypeInfo for Typed<()> { + fn type_output(&self) -> TypeInfo { TypeInfo::None } } -impl WithTypeInfo for crate::PyRef<'_, T> +impl WithTypeInfo for &Typed> where T: crate::PyClass, { - fn type_input() -> TypeInfo { - TypeInfo::Class { - name: ::std::borrow::Cow::Borrowed(T::NAME), - module: T::MODULE - .map(Cow::from) - .map(ModuleName::Module) - .unwrap_or(ModuleName::CurrentModule), - type_vars: ::std::vec::Vec::new(), - } - } - - fn type_output() -> TypeInfo { + fn type_input(&self) -> TypeInfo { TypeInfo::Class { name: ::std::borrow::Cow::Borrowed(T::NAME), module: T::MODULE @@ -385,39 +397,45 @@ where } } -impl WithTypeInfo for Option { - fn type_output() -> TypeInfo { - TypeInfo::Optional(Box::new(T::type_output())) +impl WithTypeInfo for &Typed> +where + Typed: WithTypeInfo, +{ + fn type_output(&self) -> TypeInfo { + TypeInfo::Optional(Box::new((&&Typed::::new()).type_output())) } - fn type_input() -> TypeInfo { - TypeInfo::Optional(Box::new(T::type_input())) + fn type_input(&self) -> TypeInfo { + TypeInfo::Optional(Box::new((&&Typed::::new()).type_input())) } } -impl WithTypeInfo for Result { - fn type_output() -> TypeInfo { - T::type_output() +impl WithTypeInfo for &Typed> +where + Typed: WithTypeInfo, +{ + fn type_output(&self) -> TypeInfo { + (&&Typed::::new()).type_output() } - fn type_input() -> TypeInfo { - T::type_input() + fn type_input(&self) -> TypeInfo { + (&&Typed::::new()).type_input() } } -impl WithTypeInfo for crate::Py { - fn type_output() -> TypeInfo { - T::type_output() +impl WithTypeInfo for &Typed> +where + Typed: WithTypeInfo, +{ + fn type_output(&self) -> TypeInfo { + (&&Typed::::new()).type_output() } - fn type_input() -> TypeInfo { - T::type_input() + fn type_input(&self) -> TypeInfo { + (&&Typed::::new()).type_input() } } -impl WithTypeInfo for crate::PyAny { -} - #[cfg(test)] mod test { use std::borrow::Cow; diff --git a/src/types/boolobject.rs b/src/types/boolobject.rs index 1e4d61b2da5..dfaadd8e8f0 100644 --- a/src/types/boolobject.rs +++ b/src/types/boolobject.rs @@ -1,6 +1,6 @@ // Copyright (c) 2017-present PyO3 Project and Contributors #[cfg(feature = "experimental-inspect")] -use crate::inspect::types::{WithTypeInfo, TypeInfo}; +use crate::inspect::types::{WithTypeInfo, TypeInfo, Typed}; use crate::{ ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyObject, PyResult, Python, ToPyObject, }; @@ -61,14 +61,10 @@ impl<'source> FromPyObject<'source> for bool { } #[cfg(feature = "experimental-inspect")] -impl WithTypeInfo for bool { - fn type_output() -> TypeInfo { +impl WithTypeInfo for Typed { + fn type_output(&self) -> TypeInfo { TypeInfo::builtin("bool") } - - fn type_input() -> TypeInfo { - Self::type_output() - } } #[cfg(test)] diff --git a/src/types/floatob.rs b/src/types/floatob.rs index 620350013ec..36cd7c93d9b 100644 --- a/src/types/floatob.rs +++ b/src/types/floatob.rs @@ -2,7 +2,7 @@ // // based on Daniel Grunwald's https://github.com/dgrunwald/rust-cpython #[cfg(feature = "experimental-inspect")] -use crate::inspect::types::{WithTypeInfo, TypeInfo}; +use crate::inspect::types::{WithTypeInfo, TypeInfo, Typed}; use crate::{ ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyErr, PyObject, PyResult, Python, ToPyObject, }; @@ -66,13 +66,10 @@ impl<'source> FromPyObject<'source> for f64 { } #[cfg(feature = "experimental-inspect")] -impl WithTypeInfo for f64 { - fn type_output() -> TypeInfo { +impl WithTypeInfo for Typed { + fn type_output(&self) -> TypeInfo { TypeInfo::builtin("float") } - fn type_input() -> TypeInfo { - Self::type_output() - } } impl ToPyObject for f32 { @@ -95,13 +92,10 @@ impl<'source> FromPyObject<'source> for f32 { } #[cfg(feature = "experimental-inspect")] -impl WithTypeInfo for f32 { - fn type_output() -> TypeInfo { +impl WithTypeInfo for Typed { + fn type_output(&self) -> TypeInfo { TypeInfo::builtin("float") } - fn type_input() -> TypeInfo { - Self::type_output() - } } #[cfg(test)] diff --git a/src/types/set.rs b/src/types/set.rs index 230ed39c1cd..36774b9a014 100644 --- a/src/types/set.rs +++ b/src/types/set.rs @@ -235,8 +235,6 @@ mod impl_ { } pub use impl_::*; -#[cfg(feature = "experimental-inspect")] -use crate::inspect::types::TypeInfo; #[inline] pub(crate) fn new_from_iter( diff --git a/src/types/tuple.rs b/src/types/tuple.rs index 318db9a1940..9994d792cff 100644 --- a/src/types/tuple.rs +++ b/src/types/tuple.rs @@ -4,7 +4,7 @@ use std::convert::TryInto; use crate::ffi::{self, Py_ssize_t}; #[cfg(feature = "experimental-inspect")] -use crate::inspect::types::{WithTypeInfo, TypeInfo}; +use crate::inspect::types::{WithTypeInfo, TypeInfo, Typed}; use crate::internal_tricks::get_ssize_index; use crate::types::PySequence; use crate::{ @@ -309,12 +309,14 @@ macro_rules! tuple_conversion ({$length:expr,$(($refN:ident, $n:tt, $T:ident)),+ } #[cfg(feature = "experimental-inspect")] - impl <$($T: WithTypeInfo),+> WithTypeInfo for ($($T,)+) { - fn type_input() -> TypeInfo { - TypeInfo::Tuple(Some(vec![$( $T::type_input() ),+])) + impl <$($T),+> WithTypeInfo for &Typed<($($T,)+)> where + $(Typed<$T>: WithTypeInfo ,)+ { + + fn type_input(&self) -> TypeInfo { + TypeInfo::Tuple(Some(vec![$( (&&Typed::<$T>::new()).type_input()),+])) } - fn type_output() -> TypeInfo { - TypeInfo::Tuple(Some(vec![$( $T::type_output() ),+])) + fn type_output(&self) -> TypeInfo { + TypeInfo::Tuple(Some(vec![$( (&&Typed::<$T>::new()).type_output() ),+])) } }