Skip to content

Commit 721dec9

Browse files
committed
pyobject_type_info_type_hint
1 parent 3bf516e commit 721dec9

File tree

1 file changed

+18
-39
lines changed

1 file changed

+18
-39
lines changed

src/types/mod.rs

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -140,51 +140,30 @@ macro_rules! pyobject_native_static_type_object(
140140
};
141141
);
142142

143-
/// Implements the `PyTypeInfo` trait for a native Python type.
144-
///
145-
/// # Safety
146-
///
147-
/// - `$typeobject` must be a function that produces a valid `*mut PyTypeObject`
148-
/// - `$checkfunction` must be a function that accepts arbitrary `*mut PyObject` and returns true /
149-
/// false according to whether the object is an instance of the type from `$typeobject`
143+
/// Adds a TYPE_HINT constant if the `experimental-inspect` feature is enabled.
150144
#[cfg(not(feature = "experimental-inspect"))]
151145
#[doc(hidden)]
152146
#[macro_export]
153-
macro_rules! pyobject_native_type_info(
154-
($name:ty, $typeobject:expr, $type_hint_module:expr, $type_hint_name:expr, $module:expr $(, #checkfunction=$checkfunction:path)? $(;$generics:ident)*) => {
155-
// SAFETY: macro caller has upheld the safety contracts
156-
unsafe impl<$($generics,)*> $crate::type_object::PyTypeInfo for $name {
157-
const NAME: &'static str = stringify!($name);
158-
const MODULE: ::std::option::Option<&'static str> = $module;
159-
160-
#[inline]
161-
#[allow(clippy::redundant_closure_call)]
162-
fn type_object_raw(py: $crate::Python<'_>) -> *mut $crate::ffi::PyTypeObject {
163-
$typeobject(py)
164-
}
165-
166-
$(
167-
#[inline]
168-
fn is_type_of(obj: &$crate::Bound<'_, $crate::PyAny>) -> bool {
169-
#[allow(unused_unsafe, reason = "not all `$checkfunction` are unsafe fn")]
170-
// SAFETY: `$checkfunction` is being called with a valid `PyObject` pointer
171-
unsafe { $checkfunction(obj.as_ptr()) > 0 }
172-
}
173-
)?
174-
}
175-
176-
impl $name {
177-
#[doc(hidden)]
178-
pub const _PYO3_DEF: $crate::impl_::pymodule::AddTypeToModule<Self> = $crate::impl_::pymodule::AddTypeToModule::new();
147+
macro_rules! pyobject_type_info_type_hint(
148+
($module:expr, $name:expr) => {};
149+
);
179150

180-
#[allow(dead_code)]
181-
#[doc(hidden)]
182-
pub const _PYO3_INTROSPECTION_ID: &'static str = concat!(stringify!($module), stringify!($name));
183-
}
151+
#[cfg(feature = "experimental-inspect")]
152+
#[doc(hidden)]
153+
#[macro_export]
154+
macro_rules! pyobject_type_info_type_hint(
155+
($module:expr, $name:expr) => {
156+
const TYPE_HINT: $crate::inspect::TypeHint = $crate::inspect::TypeHint::module_attr($module, $name);
184157
};
185158
);
186159

187-
#[cfg(feature = "experimental-inspect")]
160+
/// Implements the `PyTypeInfo` trait for a native Python type.
161+
///
162+
/// # Safety
163+
///
164+
/// - `$typeobject` must be a function that produces a valid `*mut PyTypeObject`
165+
/// - `$checkfunction` must be a function that accepts arbitrary `*mut PyObject` and returns true /
166+
/// false according to whether the object is an instance of the type from `$typeobject`
188167
#[doc(hidden)]
189168
#[macro_export]
190169
macro_rules! pyobject_native_type_info(
@@ -193,7 +172,7 @@ macro_rules! pyobject_native_type_info(
193172
unsafe impl<$($generics,)*> $crate::type_object::PyTypeInfo for $name {
194173
const NAME: &'static str = stringify!($name);
195174
const MODULE: ::std::option::Option<&'static str> = $module;
196-
const TYPE_HINT: $crate::inspect::TypeHint = $crate::inspect::TypeHint::module_attr($type_hint_module, $type_hint_name);
175+
$crate::pyobject_type_info_type_hint!($type_hint_module, $type_hint_name);
197176

198177
#[inline]
199178
#[allow(clippy::redundant_closure_call)]

0 commit comments

Comments
 (0)