1use crate::ffi_ptr_ext::FfiPtrExt;
2#[cfg(feature = "experimental-inspect")]
3use crate::inspect::TypeHint;
4use crate::{ffi, types::any::PyAnyMethods, Borrowed, Bound, PyAny, PyTypeInfo, Python};
5
6#[repr(transparent)]
11pub struct PyNone(PyAny);
12
13pyobject_native_type_named!(PyNone);
14
15impl PyNone {
16 #[inline]
18 pub fn get(py: Python<'_>) -> Borrowed<'_, '_, PyNone> {
19 unsafe {
21 ffi::Py_None()
22 .assume_borrowed_unchecked(py)
23 .cast_unchecked()
24 }
25 }
26}
27
28unsafe impl PyTypeInfo for PyNone {
29 const NAME: &'static str = "NoneType";
30 const MODULE: Option<&'static str> = None;
31
32 #[cfg(feature = "experimental-inspect")]
33 const TYPE_HINT: TypeHint = TypeHint::builtin("None");
34
35 fn type_object_raw(_py: Python<'_>) -> *mut ffi::PyTypeObject {
36 unsafe { ffi::Py_TYPE(ffi::Py_None()) }
37 }
38
39 #[inline]
40 fn is_type_of(object: &Bound<'_, PyAny>) -> bool {
41 Self::is_exact_type_of(object)
43 }
44
45 #[inline]
46 fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool {
47 object.is(&**Self::get(object.py()))
48 }
49}
50
51#[cfg(test)]
52mod tests {
53 use crate::types::any::PyAnyMethods;
54 use crate::types::{PyDict, PyNone};
55 use crate::{PyTypeInfo, Python};
56
57 #[test]
58 fn test_none_is_itself() {
59 Python::attach(|py| {
60 assert!(PyNone::get(py).is_instance_of::<PyNone>());
61 assert!(PyNone::get(py).is_exact_instance_of::<PyNone>());
62 })
63 }
64
65 #[test]
66 fn test_none_type_object_consistent() {
67 Python::attach(|py| {
68 assert!(PyNone::get(py).get_type().is(PyNone::type_object(py)));
69 })
70 }
71
72 #[test]
73 fn test_none_is_none() {
74 Python::attach(|py| {
75 assert!(PyNone::get(py).cast::<PyNone>().unwrap().is_none());
76 })
77 }
78
79 #[test]
80 fn test_dict_is_not_none() {
81 Python::attach(|py| {
82 assert!(PyDict::new(py).cast::<PyNone>().is_err());
83 })
84 }
85}