pyo3_stub_gen/stub_type/
builtins.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//! Define PyStubType for built-in types based on <https://pyo3.rs/v0.22.2/conversions/tables#argument-types>

use crate::stub_type::*;
use std::{
    borrow::Cow,
    ffi::{OsStr, OsString},
    path::PathBuf,
    time::SystemTime,
};

use chrono::{DateTime, FixedOffset, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc};

macro_rules! impl_builtin {
    ($ty:ty, $pytype:expr) => {
        impl PyStubType for $ty {
            fn type_output() -> TypeInfo {
                TypeInfo {
                    name: $pytype.to_string(),
                    import: HashSet::new(),
                }
            }
        }
    };
}

macro_rules! impl_with_module {
    ($ty:ty, $pytype:expr, $module:expr) => {
        impl PyStubType for $ty {
            fn type_output() -> TypeInfo {
                TypeInfo::with_module($pytype, $module.into())
            }
        }
    };
}

impl_builtin!((), "None");
impl_builtin!(bool, "bool");
impl_builtin!(u8, "int");
impl_builtin!(u16, "int");
impl_builtin!(u32, "int");
impl_builtin!(u64, "int");
impl_builtin!(u128, "int");
impl_builtin!(usize, "int");
impl_builtin!(i8, "int");
impl_builtin!(i16, "int");
impl_builtin!(i32, "int");
impl_builtin!(i64, "int");
impl_builtin!(i128, "int");
impl_builtin!(isize, "int");
impl_builtin!(f32, "float");
impl_builtin!(f64, "float");
impl_builtin!(num_complex::Complex32, "complex");
impl_builtin!(num_complex::Complex64, "complex");

impl_builtin!(char, "str");
impl_builtin!(&str, "str");
impl_builtin!(OsStr, "str");
impl_builtin!(String, "str");
impl_builtin!(OsString, "str");
impl_builtin!(Cow<'_, str>, "str");
impl_builtin!(Cow<'_, OsStr>, "str");

impl PyStubType for PathBuf {
    fn type_output() -> TypeInfo {
        TypeInfo::builtin("str")
    }
    fn type_input() -> TypeInfo {
        TypeInfo::builtin("str")
            | TypeInfo::with_module("os.PathLike", "os".into())
            | TypeInfo::with_module("pathlib.Path", "pathlib".into())
    }
}

impl<Tz: TimeZone> PyStubType for DateTime<Tz> {
    fn type_output() -> TypeInfo {
        TypeInfo::with_module("datetime.datetime", "datetime".into())
    }
}

impl_with_module!(SystemTime, "datetime.datetime", "datetime");
impl_with_module!(NaiveDateTime, "datetime.datetime", "datetime");
impl_with_module!(NaiveDate, "datetime.date", "datetime");
impl_with_module!(NaiveTime, "datetime.time", "datetime");
impl_with_module!(FixedOffset, "datetime.tzinfo", "datetime");
impl_with_module!(Utc, "datetime.tzinfo", "datetime");
impl_with_module!(std::time::Duration, "datetime.timedelta", "datetime");
impl_with_module!(chrono::Duration, "datetime.timedelta", "datetime");