Skip to main content

pyo3_introspection/
model.rs

1#[derive(Debug, Eq, PartialEq, Clone, Hash)]
2pub struct Module {
3    pub name: String,
4    pub modules: Vec<Module>,
5    pub classes: Vec<Class>,
6    pub functions: Vec<Function>,
7    pub attributes: Vec<Attribute>,
8    pub incomplete: bool,
9    pub docstring: Option<String>,
10}
11
12#[derive(Debug, Eq, PartialEq, Clone, Hash)]
13pub struct Class {
14    pub name: String,
15    pub bases: Vec<Expr>,
16    pub methods: Vec<Function>,
17    pub attributes: Vec<Attribute>,
18    /// decorator like 'typing.final'
19    pub decorators: Vec<Expr>,
20    pub inner_classes: Vec<Class>,
21    pub docstring: Option<String>,
22}
23
24#[derive(Debug, Eq, PartialEq, Clone, Hash)]
25pub struct Function {
26    pub name: String,
27    /// decorator like 'property' or 'staticmethod'
28    pub decorators: Vec<Expr>,
29    pub arguments: Arguments,
30    /// return type
31    pub returns: Option<Expr>,
32    pub is_async: bool,
33    pub docstring: Option<String>,
34}
35
36#[derive(Debug, Eq, PartialEq, Clone, Hash)]
37pub struct Attribute {
38    pub name: String,
39    /// Value as a Python expression if easily expressible
40    pub value: Option<Expr>,
41    /// Type annotation as a Python expression
42    pub annotation: Option<Expr>,
43    pub docstring: Option<String>,
44}
45
46#[derive(Debug, Eq, PartialEq, Clone, Hash)]
47pub struct Arguments {
48    /// Arguments before /
49    pub positional_only_arguments: Vec<Argument>,
50    /// Regular arguments (between / and *)
51    pub arguments: Vec<Argument>,
52    /// *vararg
53    pub vararg: Option<VariableLengthArgument>,
54    /// Arguments after *
55    pub keyword_only_arguments: Vec<Argument>,
56    /// **kwarg
57    pub kwarg: Option<VariableLengthArgument>,
58}
59
60#[derive(Debug, Eq, PartialEq, Clone, Hash)]
61pub struct Argument {
62    pub name: String,
63    /// Default value as a Python expression
64    pub default_value: Option<Expr>,
65    /// Type annotation as a Python expression
66    pub annotation: Option<Expr>,
67}
68
69/// A variable length argument ie. *vararg or **kwarg
70#[derive(Debug, Eq, PartialEq, Clone, Hash)]
71pub struct VariableLengthArgument {
72    pub name: String,
73    /// Type annotation as a Python expression
74    pub annotation: Option<Expr>,
75}
76
77/// A python expression
78///
79/// This is the `expr` production of the [Python `ast` module grammar](https://docs.python.org/3/library/ast.html#abstract-grammar)
80#[derive(Debug, Eq, PartialEq, Clone, Hash)]
81pub enum Expr {
82    /// A constant like `None` or `123`
83    Constant { value: Constant },
84    /// A name
85    Name { id: String },
86    /// An attribute `value.attr`
87    Attribute { value: Box<Self>, attr: String },
88    /// A binary operator
89    BinOp {
90        left: Box<Self>,
91        op: Operator,
92        right: Box<Self>,
93    },
94    /// A tuple
95    Tuple { elts: Vec<Self> },
96    /// A list
97    List { elts: Vec<Self> },
98    /// A subscript `value[slice]`
99    Subscript { value: Box<Self>, slice: Box<Self> },
100}
101
102/// A PyO3 extension to the Python AST to know more about [`Expr::Constant`].
103///
104/// This enables advanced features like escaping.
105#[derive(Debug, Eq, PartialEq, Clone, Hash)]
106pub enum Constant {
107    /// `None`
108    None,
109    /// `True` or `False`
110    Bool(bool),
111    /// An integer in base 10
112    Int(String),
113    /// A float in base 10 (does not include Inf and NaN)
114    Float(String),
115    /// A string (unescaped!)
116    Str(String),
117    /// `...`
118    Ellipsis,
119}
120
121/// An operator used in [`Expr::BinOp`].
122#[derive(Debug, Eq, PartialEq, Clone, Copy, Hash)]
123pub enum Operator {
124    /// `|` operator
125    BitOr,
126}