pyo3_stub_gen/stub_type/
pyo3.rs

1use crate::stub_type::*;
2use ::pyo3::{
3    basic::CompareOp,
4    pybacked::{PyBackedBytes, PyBackedStr},
5    pyclass::boolean_struct::False,
6    types::*,
7    Bound, Py, PyClass, PyRef, PyRefMut,
8};
9use maplit::hashset;
10use std::collections::HashMap;
11
12impl PyStubType for PyAny {
13    fn type_output() -> TypeInfo {
14        TypeInfo {
15            name: "typing.Any".to_string(),
16            source_module: None,
17            import: hashset! { "typing".into() },
18            type_refs: HashMap::new(),
19        }
20    }
21}
22
23impl<T: PyStubType> PyStubType for Py<T> {
24    fn type_input() -> TypeInfo {
25        T::type_input()
26    }
27    fn type_output() -> TypeInfo {
28        T::type_output()
29    }
30}
31
32impl<T: PyStubType + PyClass> PyStubType for PyRef<'_, T> {
33    fn type_input() -> TypeInfo {
34        T::type_input()
35    }
36    fn type_output() -> TypeInfo {
37        T::type_output()
38    }
39}
40
41impl<T: PyStubType + PyClass<Frozen = False>> PyStubType for PyRefMut<'_, T> {
42    fn type_input() -> TypeInfo {
43        T::type_input()
44    }
45    fn type_output() -> TypeInfo {
46        T::type_output()
47    }
48}
49
50impl<T: PyStubType> PyStubType for Bound<'_, T> {
51    fn type_input() -> TypeInfo {
52        T::type_input()
53    }
54    fn type_output() -> TypeInfo {
55        T::type_output()
56    }
57}
58
59macro_rules! impl_builtin {
60    ($ty:ty, $pytype:expr) => {
61        impl PyStubType for $ty {
62            fn type_output() -> TypeInfo {
63                TypeInfo {
64                    name: $pytype.to_string(),
65                    source_module: None,
66                    import: HashSet::new(),
67                    type_refs: HashMap::new(),
68                }
69            }
70        }
71    };
72}
73
74impl_builtin!(PyBool, "bool");
75impl_builtin!(PyInt, "int");
76impl_builtin!(PyFloat, "float");
77impl_builtin!(PyComplex, "complex");
78impl_builtin!(PyList, "list");
79impl_builtin!(PyTuple, "tuple");
80impl_builtin!(PySlice, "slice");
81impl_builtin!(PyDict, "dict");
82impl_builtin!(PySet, "set");
83impl_builtin!(PyString, "str");
84impl_builtin!(PyBackedStr, "str");
85impl_builtin!(PyByteArray, "bytearray");
86impl_builtin!(PyBytes, "bytes");
87impl_builtin!(PyBackedBytes, "bytes");
88impl_builtin!(PyType, "type");
89impl_builtin!(CompareOp, "int");
90
91macro_rules! impl_simple {
92    ($ty:ty, $mod:expr, $pytype:expr) => {
93        impl PyStubType for $ty {
94            fn type_output() -> TypeInfo {
95                TypeInfo {
96                    name: concat!($mod, ".", $pytype).to_string(),
97                    source_module: None,
98                    import: hashset! { $mod.into() },
99                    type_refs: HashMap::new(),
100                }
101            }
102        }
103    };
104}
105
106impl_simple!(PyDate, "datetime", "date");
107impl_simple!(PyDateTime, "datetime", "datetime");
108impl_simple!(PyDelta, "datetime", "timedelta");
109impl_simple!(PyTime, "datetime", "time");
110impl_simple!(PyTzInfo, "datetime", "tzinfo");