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