diff --git a/pyo3-macros-backend/src/pyclass.rs b/pyo3-macros-backend/src/pyclass.rs index 07fccf3d655..e45c1016dfc 100644 --- a/pyo3-macros-backend/src/pyclass.rs +++ b/pyo3-macros-backend/src/pyclass.rs @@ -435,18 +435,6 @@ fn get_class_python_name<'a>(cls: &'a syn::Ident, args: &'a PyClassArgs) -> Cow< .unwrap_or_else(|| Cow::Owned(cls.unraw())) } -#[cfg(feature = "experimental-inspect")] -fn get_class_type_hint(cls: &Ident, args: &PyClassArgs, ctx: &Ctx) -> TokenStream { - let pyo3_path = &ctx.pyo3_path; - let name = get_class_python_name(cls, args).to_string(); - if let Some(module) = &args.options.module { - let module = module.value.value(); - quote! { #pyo3_path::inspect::TypeHint::module_attr(#module, #name) } - } else { - quote! { #pyo3_path::inspect::TypeHint::builtin(#name) } - } -} - fn impl_class( cls: &syn::Ident, args: &PyClassArgs, @@ -1119,7 +1107,7 @@ fn impl_complex_enum( } }); let output_type = if cfg!(feature = "experimental-inspect") { - quote!(const OUTPUT_TYPE: #pyo3_path::inspect::TypeHint = <#cls as #pyo3_path::PyTypeInfo>::TYPE_HINT;) + quote!(const OUTPUT_TYPE: #pyo3_path::inspect::TypeHint = <#cls as #pyo3_path::PyTypeCheck>::TYPE_HINT;) } else { TokenStream::new() }; @@ -1959,21 +1947,11 @@ fn impl_pytypeinfo(cls: &syn::Ident, attr: &PyClassArgs, ctx: &Ctx) -> TokenStre quote! { ::core::option::Option::None } }; - #[cfg(feature = "experimental-inspect")] - let type_hint = { - let type_hint = get_class_type_hint(cls, attr, ctx); - quote! { const TYPE_HINT: #pyo3_path::inspect::TypeHint = #type_hint; } - }; - #[cfg(not(feature = "experimental-inspect"))] - let type_hint = quote! {}; - quote! { unsafe impl #pyo3_path::type_object::PyTypeInfo for #cls { const NAME: &'static str = #cls_name; const MODULE: ::std::option::Option<&'static str> = #module; - #type_hint - #[inline] fn type_object_raw(py: #pyo3_path::Python<'_>) -> *mut #pyo3_path::ffi::PyTypeObject { use #pyo3_path::prelude::PyTypeMethods; @@ -2349,7 +2327,7 @@ impl<'a> PyClassImplsBuilder<'a> { // If #cls is not extended type, we allow Self->PyObject conversion if attr.options.extends.is_none() { let output_type = if cfg!(feature = "experimental-inspect") { - quote!(const OUTPUT_TYPE: #pyo3_path::inspect::TypeHint = <#cls as #pyo3_path::PyTypeInfo>::TYPE_HINT;) + quote!(const OUTPUT_TYPE: #pyo3_path::inspect::TypeHint = <#cls as #pyo3_path::PyTypeCheck>::TYPE_HINT;) } else { TokenStream::new() }; @@ -2515,7 +2493,7 @@ impl<'a> PyClassImplsBuilder<'a> { self.attr.options.from_py_object { let input_type = if cfg!(feature = "experimental-inspect") { - quote!(const INPUT_TYPE: #pyo3_path::inspect::TypeHint = <#cls as #pyo3_path::PyTypeInfo>::TYPE_HINT;) + quote!(const INPUT_TYPE: #pyo3_path::inspect::TypeHint = <#cls as #pyo3_path::PyTypeCheck>::TYPE_HINT;) } else { TokenStream::new() }; diff --git a/src/conversion.rs b/src/conversion.rs index d4f5c513173..11d9876d11b 100644 --- a/src/conversion.rs +++ b/src/conversion.rs @@ -522,7 +522,7 @@ where type Error = PyClassGuardError<'a, 'py>; #[cfg(feature = "experimental-inspect")] - const INPUT_TYPE: TypeHint = ::TYPE_HINT; + const INPUT_TYPE: TypeHint = ::TYPE_HINT; fn extract(obj: Borrowed<'a, 'py, PyAny>) -> Result { Ok(obj.extract::>()?.clone()) @@ -536,7 +536,7 @@ where type Error = PyClassGuardError<'a, 'py>; #[cfg(feature = "experimental-inspect")] - const INPUT_TYPE: TypeHint = ::TYPE_HINT; + const INPUT_TYPE: TypeHint = ::TYPE_HINT; fn extract(obj: Borrowed<'a, 'py, PyAny>) -> Result { obj.cast::() @@ -553,7 +553,7 @@ where type Error = PyClassGuardMutError<'a, 'py>; #[cfg(feature = "experimental-inspect")] - const INPUT_TYPE: TypeHint = ::TYPE_HINT; + const INPUT_TYPE: TypeHint = ::TYPE_HINT; fn extract(obj: Borrowed<'a, 'py, PyAny>) -> Result { obj.cast::() diff --git a/src/pycell.rs b/src/pycell.rs index a9a282b24f0..b565943bf36 100644 --- a/src/pycell.rs +++ b/src/pycell.rs @@ -197,6 +197,8 @@ use crate::conversion::IntoPyObject; use crate::exceptions::PyRuntimeError; use crate::ffi_ptr_ext::FfiPtrExt; use crate::pyclass::{boolean_struct::False, PyClass}; +#[cfg(feature = "experimental-inspect")] +use crate::type_object::PyTypeCheck; use crate::{ffi, Borrowed, Bound, PyErr, Python}; use std::convert::Infallible; use std::fmt; diff --git a/src/pyclass/guard.rs b/src/pyclass/guard.rs index 6a58b8a47a5..71032eede86 100644 --- a/src/pyclass/guard.rs +++ b/src/pyclass/guard.rs @@ -4,6 +4,8 @@ use crate::inspect::TypeHint; use crate::pycell::PyBorrowMutError; use crate::pycell::{impl_::PyClassBorrowChecker, PyBorrowError}; use crate::pyclass::boolean_struct::False; +#[cfg(feature = "experimental-inspect")] +use crate::type_object::PyTypeCheck; use crate::{ffi, Borrowed, CastError, FromPyObject, IntoPyObject, Py, PyClass, PyErr}; use std::convert::Infallible; use std::fmt; diff --git a/src/type_object.rs b/src/type_object.rs index e15a9c6b8c0..af346d6d22d 100644 --- a/src/type_object.rs +++ b/src/type_object.rs @@ -48,10 +48,6 @@ pub unsafe trait PyTypeInfo: Sized { /// Module name, if any. const MODULE: Option<&'static str>; - /// Provides the full python type as a type hint. - #[cfg(feature = "experimental-inspect")] - const TYPE_HINT: TypeHint = TypeHint::module_attr("typing", "Any"); - /// Returns the PyTypeObject instance for this type. fn type_object_raw(py: Python<'_>) -> *mut ffi::PyTypeObject; @@ -127,7 +123,11 @@ where const NAME: &'static str = T::NAME; #[cfg(feature = "experimental-inspect")] - const TYPE_HINT: TypeHint = ::TYPE_HINT; + const TYPE_HINT: TypeHint = if let Some(module) = ::MODULE { + TypeHint::module_attr(module, ::NAME) + } else { + TypeHint::builtin(::NAME) + }; #[inline] fn type_check(object: &Bound<'_, PyAny>) -> bool {