Skip to content
Closed
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
28 changes: 3 additions & 25 deletions pyo3-macros-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()
};
Expand Down Expand Up @@ -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()
};
Expand Down
6 changes: 3 additions & 3 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ where
type Error = PyClassGuardError<'a, 'py>;

#[cfg(feature = "experimental-inspect")]
const INPUT_TYPE: TypeHint = <T as crate::PyTypeInfo>::TYPE_HINT;
const INPUT_TYPE: TypeHint = <T as crate::PyTypeCheck>::TYPE_HINT;

fn extract(obj: Borrowed<'a, 'py, PyAny>) -> Result<Self, Self::Error> {
Ok(obj.extract::<PyClassGuard<'_, T>>()?.clone())
Expand All @@ -536,7 +536,7 @@ where
type Error = PyClassGuardError<'a, 'py>;

#[cfg(feature = "experimental-inspect")]
const INPUT_TYPE: TypeHint = <T as crate::PyTypeInfo>::TYPE_HINT;
const INPUT_TYPE: TypeHint = <T as crate::PyTypeCheck>::TYPE_HINT;

fn extract(obj: Borrowed<'a, 'py, PyAny>) -> Result<Self, Self::Error> {
obj.cast::<T>()
Expand All @@ -553,7 +553,7 @@ where
type Error = PyClassGuardMutError<'a, 'py>;

#[cfg(feature = "experimental-inspect")]
const INPUT_TYPE: TypeHint = <T as crate::PyTypeInfo>::TYPE_HINT;
const INPUT_TYPE: TypeHint = <T as crate::PyTypeCheck>::TYPE_HINT;

fn extract(obj: Borrowed<'a, 'py, PyAny>) -> Result<Self, Self::Error> {
obj.cast::<T>()
Expand Down
2 changes: 2 additions & 0 deletions src/pycell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/pyclass/guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions src/type_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -127,7 +123,11 @@ where
const NAME: &'static str = T::NAME;

#[cfg(feature = "experimental-inspect")]
const TYPE_HINT: TypeHint = <T as PyTypeInfo>::TYPE_HINT;
const TYPE_HINT: TypeHint = if let Some(module) = <T as PyTypeInfo>::MODULE {
TypeHint::module_attr(module, <T as PyTypeInfo>::NAME)
} else {
TypeHint::builtin(<T as PyTypeInfo>::NAME)
};

#[inline]
fn type_check(object: &Bound<'_, PyAny>) -> bool {
Expand Down
Loading