#[cfg(feature = "experimental-inspect")]
use crate::inspect::{type_hint_identifier, PyStaticExpr};
use crate::{
ffi, ffi_ptr_ext::FfiPtrExt, types::any::PyAnyMethods, Borrowed, Bound, PyAny, PyTypeInfo,
Python,
};
#[repr(transparent)]
pub struct PyNotImplemented(PyAny);
pyobject_native_type_named!(PyNotImplemented);
impl PyNotImplemented {
#[inline]
pub fn get(py: Python<'_>) -> Borrowed<'_, '_, PyNotImplemented> {
unsafe {
ffi::Py_NotImplemented()
.assume_borrowed_unchecked(py)
.cast_unchecked()
}
}
}
unsafe impl PyTypeInfo for PyNotImplemented {
const NAME: &'static str = "NotImplementedType";
const MODULE: Option<&'static str> = None;
#[cfg(feature = "experimental-inspect")]
const TYPE_HINT: PyStaticExpr = type_hint_identifier!("types", "NotImplementedType");
fn type_object_raw(_py: Python<'_>) -> *mut ffi::PyTypeObject {
unsafe { ffi::Py_TYPE(ffi::Py_NotImplemented()) }
}
#[inline]
fn is_type_of(object: &Bound<'_, PyAny>) -> bool {
Self::is_exact_type_of(object)
}
#[inline]
fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool {
object.is(&**Self::get(object.py()))
}
}
#[cfg(test)]
mod tests {
use crate::types::any::PyAnyMethods;
use crate::types::{PyDict, PyNotImplemented};
use crate::{PyTypeInfo, Python};
#[test]
fn test_notimplemented_is_itself() {
Python::attach(|py| {
assert!(PyNotImplemented::get(py).is_instance_of::<PyNotImplemented>());
assert!(PyNotImplemented::get(py).is_exact_instance_of::<PyNotImplemented>());
})
}
#[test]
fn test_notimplemented_type_object_consistent() {
Python::attach(|py| {
assert!(PyNotImplemented::get(py)
.get_type()
.is(PyNotImplemented::type_object(py)));
})
}
#[test]
fn test_dict_is_not_notimplemented() {
Python::attach(|py| {
assert!(PyDict::new(py).cast::<PyNotImplemented>().is_err());
})
}
}