Skip to content

Commit ee8cdef

Browse files
committed
Code review feedback
1 parent 8deaef8 commit ee8cdef

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/types/any.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ fn PyObject_Check(_: *mut ffi::PyObject) -> c_int {
4040
1
4141
}
4242

43+
// We follow stub writing guidelines and use "object" instead of "typing.Any": https://typing.python.org/en/latest/guides/writing_stubs.html#using-any
4344
pyobject_native_type_info!(
4445
PyAny,
4546
pyobject_native_static_type_object!(ffi::PyBaseObject_Type),

src/types/mod.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,44 @@ macro_rules! pyobject_native_static_type_object(
147147
/// - `$typeobject` must be a function that produces a valid `*mut PyTypeObject`
148148
/// - `$checkfunction` must be a function that accepts arbitrary `*mut PyObject` and returns true /
149149
/// false according to whether the object is an instance of the type from `$typeobject`
150+
#[cfg(not(feature = "experimental-inspect"))]
151+
#[doc(hidden)]
152+
#[macro_export]
153+
macro_rules! pyobject_native_type_info(
154+
($name:ty, $typeobject:expr, $module:expr, $python_name: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> = ::std::option::Option::Some($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();
179+
180+
#[allow(dead_code)]
181+
#[doc(hidden)]
182+
pub const _PYO3_INTROSPECTION_ID: &'static str = concat!(stringify!($module), stringify!($name));
183+
}
184+
};
185+
);
186+
187+
#[cfg(feature = "experimental-inspect")]
150188
#[doc(hidden)]
151189
#[macro_export]
152190
macro_rules! pyobject_native_type_info(
@@ -155,7 +193,6 @@ macro_rules! pyobject_native_type_info(
155193
unsafe impl<$($generics,)*> $crate::type_object::PyTypeInfo for $name {
156194
const NAME: &'static str = stringify!($name);
157195
const MODULE: ::std::option::Option<&'static str> = ::std::option::Option::Some($module);
158-
#[cfg(feature = "experimental-inspect")]
159196
const TYPE_HINT: $crate::inspect::TypeHint = $crate::inspect::TypeHint::module_attr($module, $python_name);
160197

161198
#[inline]

0 commit comments

Comments
 (0)