1mod builtins;
2mod collections;
3mod pyo3;
4
5#[cfg(feature = "numpy")]
6mod numpy;
7
8#[cfg(feature = "either")]
9mod either;
10
11#[cfg(feature = "rust_decimal")]
12mod rust_decimal;
13
14use maplit::hashset;
15use std::cmp::Ordering;
16use std::{collections::HashSet, fmt, ops};
17
18#[derive(Debug, Clone, PartialEq, Eq, Hash)]
22pub enum ImportRef {
23 Module(ModuleRef),
24 Type(TypeRef),
25}
26
27impl From<&str> for ImportRef {
28 fn from(value: &str) -> Self {
29 ImportRef::Module(value.into())
30 }
31}
32
33impl PartialOrd for ImportRef {
34 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
35 Some(self.cmp(other))
36 }
37}
38
39impl Ord for ImportRef {
40 fn cmp(&self, other: &Self) -> Ordering {
41 match (self, other) {
42 (ImportRef::Module(a), ImportRef::Module(b)) => a.get().cmp(&b.get()),
43 (ImportRef::Type(a), ImportRef::Type(b)) => a.cmp(b),
44 (ImportRef::Module(_), ImportRef::Type(_)) => Ordering::Greater,
45 (ImportRef::Type(_), ImportRef::Module(_)) => Ordering::Less,
46 }
47 }
48}
49
50#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Default, Hash)]
51pub enum ModuleRef {
52 Named(String),
53
54 #[default]
63 Default,
64}
65
66impl ModuleRef {
67 pub fn get(&self) -> Option<&str> {
68 match self {
69 Self::Named(name) => Some(name),
70 Self::Default => None,
71 }
72 }
73}
74
75impl From<&str> for ModuleRef {
76 fn from(s: &str) -> Self {
77 Self::Named(s.to_string())
78 }
79}
80
81#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Default, Hash)]
85pub struct TypeRef {
86 pub module: ModuleRef,
87 pub name: String,
88}
89
90impl TypeRef {
91 pub fn new(module_ref: ModuleRef, name: String) -> Self {
92 Self {
93 module: module_ref,
94 name,
95 }
96 }
97}
98
99#[derive(Debug, Clone, PartialEq, Eq)]
101pub struct TypeInfo {
102 pub name: String,
104
105 pub import: HashSet<ImportRef>,
110}
111
112impl fmt::Display for TypeInfo {
113 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
114 write!(f, "{}", self.name)
115 }
116}
117
118impl TypeInfo {
119 pub fn none() -> Self {
121 Self {
124 name: "None".to_string(),
125 import: HashSet::new(),
126 }
127 }
128
129 pub fn any() -> Self {
131 Self {
132 name: "typing.Any".to_string(),
133 import: hashset! { "typing".into() },
134 }
135 }
136
137 pub fn list_of<T: PyStubType>() -> Self {
139 let TypeInfo { name, mut import } = T::type_output();
140 import.insert("builtins".into());
141 TypeInfo {
142 name: format!("builtins.list[{name}]"),
143 import,
144 }
145 }
146
147 pub fn set_of<T: PyStubType>() -> Self {
149 let TypeInfo { name, mut import } = T::type_output();
150 import.insert("builtins".into());
151 TypeInfo {
152 name: format!("builtins.set[{name}]"),
153 import,
154 }
155 }
156
157 pub fn dict_of<K: PyStubType, V: PyStubType>() -> Self {
159 let TypeInfo {
160 name: name_k,
161 mut import,
162 } = K::type_output();
163 let TypeInfo {
164 name: name_v,
165 import: import_v,
166 } = V::type_output();
167 import.extend(import_v);
168 import.insert("builtins".into());
169 TypeInfo {
170 name: format!("builtins.set[{name_k}, {name_v}]"),
171 import,
172 }
173 }
174
175 pub fn builtin(name: &str) -> Self {
177 Self {
178 name: format!("builtins.{name}"),
179 import: hashset! { "builtins".into() },
180 }
181 }
182
183 pub fn unqualified(name: &str) -> Self {
185 Self {
186 name: name.to_string(),
187 import: hashset! {},
188 }
189 }
190
191 pub fn with_module(name: &str, module: ModuleRef) -> Self {
197 let mut import = HashSet::new();
198 import.insert(ImportRef::Module(module));
199 Self {
200 name: name.to_string(),
201 import,
202 }
203 }
204
205 pub fn locally_defined(type_name: &str, module: ModuleRef) -> Self {
216 let mut import = HashSet::new();
217 let type_ref = TypeRef::new(module, type_name.to_string());
218 import.insert(ImportRef::Type(type_ref));
219
220 Self {
221 name: type_name.to_string(),
222 import,
223 }
224 }
225}
226
227impl ops::BitOr for TypeInfo {
228 type Output = Self;
229
230 fn bitor(mut self, rhs: Self) -> Self {
231 self.import.extend(rhs.import);
232 Self {
233 name: format!("{} | {}", self.name, rhs.name),
234 import: self.import,
235 }
236 }
237}
238
239#[macro_export]
269macro_rules! impl_stub_type {
270 ($ty: ty = $($base:ty)|+) => {
271 impl ::pyo3_stub_gen::PyStubType for $ty {
272 fn type_output() -> ::pyo3_stub_gen::TypeInfo {
273 $(<$base>::type_output()) | *
274 }
275 fn type_input() -> ::pyo3_stub_gen::TypeInfo {
276 $(<$base>::type_input()) | *
277 }
278 }
279 };
280 ($ty:ty = $base:ty) => {
281 impl ::pyo3_stub_gen::PyStubType for $ty {
282 fn type_output() -> ::pyo3_stub_gen::TypeInfo {
283 <$base>::type_output()
284 }
285 fn type_input() -> ::pyo3_stub_gen::TypeInfo {
286 <$base>::type_input()
287 }
288 }
289 };
290}
291
292pub trait PyStubType {
294 fn type_output() -> TypeInfo;
296
297 fn type_input() -> TypeInfo {
302 Self::type_output()
303 }
304}
305
306#[cfg(test)]
307mod test {
308 use super::*;
309 use maplit::hashset;
310 use std::collections::HashMap;
311 use test_case::test_case;
312
313 #[test_case(bool::type_input(), "builtins.bool", hashset! { "builtins".into() } ; "bool_input")]
314 #[test_case(<&str>::type_input(), "builtins.str", hashset! { "builtins".into() } ; "str_input")]
315 #[test_case(Vec::<u32>::type_input(), "typing.Sequence[builtins.int]", hashset! { "typing".into(), "builtins".into() } ; "Vec_u32_input")]
316 #[test_case(Vec::<u32>::type_output(), "builtins.list[builtins.int]", hashset! { "builtins".into() } ; "Vec_u32_output")]
317 #[test_case(HashMap::<u32, String>::type_input(), "typing.Mapping[builtins.int, builtins.str]", hashset! { "typing".into(), "builtins".into() } ; "HashMap_u32_String_input")]
318 #[test_case(HashMap::<u32, String>::type_output(), "builtins.dict[builtins.int, builtins.str]", hashset! { "builtins".into() } ; "HashMap_u32_String_output")]
319 #[test_case(indexmap::IndexMap::<u32, String>::type_input(), "typing.Mapping[builtins.int, builtins.str]", hashset! { "typing".into(), "builtins".into() } ; "IndexMap_u32_String_input")]
320 #[test_case(indexmap::IndexMap::<u32, String>::type_output(), "builtins.dict[builtins.int, builtins.str]", hashset! { "builtins".into() } ; "IndexMap_u32_String_output")]
321 #[test_case(HashMap::<u32, Vec<u32>>::type_input(), "typing.Mapping[builtins.int, typing.Sequence[builtins.int]]", hashset! { "builtins".into(), "typing".into() } ; "HashMap_u32_Vec_u32_input")]
322 #[test_case(HashMap::<u32, Vec<u32>>::type_output(), "builtins.dict[builtins.int, builtins.list[builtins.int]]", hashset! { "builtins".into() } ; "HashMap_u32_Vec_u32_output")]
323 #[test_case(HashSet::<u32>::type_input(), "builtins.set[builtins.int]", hashset! { "builtins".into() } ; "HashSet_u32_input")]
324 #[test_case(indexmap::IndexSet::<u32>::type_input(), "builtins.set[builtins.int]", hashset! { "builtins".into() } ; "IndexSet_u32_input")]
325 fn test(tinfo: TypeInfo, name: &str, import: HashSet<ImportRef>) {
326 assert_eq!(tinfo.name, name);
327 if import.is_empty() {
328 assert!(tinfo.import.is_empty());
329 } else {
330 assert_eq!(tinfo.import, import);
331 }
332 }
333}