portalis-ingest 0.1.0

Python code ingestion and parsing agent
Documentation
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//! Enhanced Python Parser using rustpython-parser
//!
//! Production-grade AST parsing with full Python language support.

use crate::{PythonAst, PythonClass, PythonFunction, PythonImport, PythonParameter};
use portalis_core::{Error, Result};
use rustpython_parser::{ast, Parse};

/// Enhanced parser using rustpython-parser for full AST support
pub struct EnhancedParser;

impl EnhancedParser {
    pub fn new() -> Self {
        Self
    }

    /// Parse Python source code into our AST representation
    pub fn parse(&self, source: &str) -> Result<PythonAst> {
        // Parse using rustpython-parser
        let parsed = ast::Suite::parse(source, "<input>")
            .map_err(|e| Error::Parse(format!("Python parse error: {}", e)))?;

        let mut ast = PythonAst {
            functions: Vec::new(),
            classes: Vec::new(),
            imports: Vec::new(),
        };

        // Walk the AST and extract functions, classes, and imports
        for stmt in parsed.iter() {
            match stmt {
                ast::Stmt::FunctionDef(func) => {
                    ast.functions.push(self.extract_function(func)?);
                }
                ast::Stmt::ClassDef(class) => {
                    ast.classes.push(self.extract_class(class)?);
                }
                ast::Stmt::Import(import) => {
                    for alias in &import.names {
                        ast.imports.push(PythonImport {
                            module: alias.name.to_string(),
                            items: Vec::new(),
                            alias: alias.asname.as_ref().map(|a| a.to_string()),
                        });
                    }
                }
                ast::Stmt::ImportFrom(import) => {
                    let module = import
                        .module
                        .as_ref()
                        .map(|m| m.to_string())
                        .unwrap_or_else(|| "".to_string());

                    let items: Vec<String> = import
                        .names
                        .iter()
                        .map(|alias| alias.name.to_string())
                        .collect();

                    ast.imports.push(PythonImport {
                        module,
                        items,
                        alias: None,
                    });
                }
                _ => {}
            }
        }

        Ok(ast)
    }

    /// Extract function information from AST node
    fn extract_function(&self, func: &ast::StmtFunctionDef) -> Result<PythonFunction> {
        let name = func.name.to_string();

        // Extract parameters
        let params = self.extract_parameters(&func.args)?;

        // Extract return type annotation
        let return_type = func
            .returns
            .as_ref()
            .and_then(|expr| self.extract_type_from_expr(expr));

        // Extract decorators
        let decorators: Vec<String> = func
            .decorator_list
            .iter()
            .filter_map(|dec| self.expr_to_string(dec))
            .collect();

        // For now, we don't parse the body - we'll use it later for advanced analysis
        let body = String::new();

        Ok(PythonFunction {
            name,
            params,
            return_type,
            body,
            decorators,
        })
    }

    /// Extract parameters from function arguments
    fn extract_parameters(&self, args: &ast::Arguments) -> Result<Vec<PythonParameter>> {
        let mut params = Vec::new();

        // Regular positional arguments (rustpython 0.3 uses posonlyargs + args)
        for arg_with_default in &args.args {
            let arg = &arg_with_default.def;
            let name = arg.arg.to_string();
            let type_hint = arg.annotation.as_ref().and_then(|ann| self.extract_type_from_expr(ann));

            // Default value is in the ArgWithDefault struct
            let default = arg_with_default.default.as_ref().and_then(|expr| self.expr_to_string(expr));

            params.push(PythonParameter {
                name,
                type_hint,
                default,
            });
        }

        Ok(params)
    }

    /// Extract class information from AST node
    fn extract_class(&self, class: &ast::StmtClassDef) -> Result<PythonClass> {
        let name = class.name.to_string();

        // Extract base classes
        let bases: Vec<String> = class
            .bases
            .iter()
            .filter_map(|base| self.expr_to_string(base))
            .collect();

        // Extract methods and attributes
        let mut methods = Vec::new();
        let mut attributes = Vec::new();

        for stmt in &class.body {
            if let ast::Stmt::FunctionDef(func) = stmt {
                let method = self.extract_function(func)?;

                // Extract attributes from __init__ method
                if func.name.as_str() == "__init__" {
                    attributes.extend(self.extract_attributes_from_init(func));
                }

                methods.push(method);
            }
        }

        Ok(PythonClass {
            name,
            bases,
            methods,
            attributes,
        })
    }

    /// Extract class attributes from __init__ method
    fn extract_attributes_from_init(&self, func: &ast::StmtFunctionDef) -> Vec<crate::PythonAttribute> {
        use crate::PythonAttribute;
        let mut attributes = Vec::new();

        // Walk through the function body looking for self.attribute assignments
        for stmt in &func.body {
            if let ast::Stmt::Assign(assign) = stmt {
                for target in &assign.targets {
                    if let ast::Expr::Attribute(attr) = target {
                        // Check if it's self.something
                        if let ast::Expr::Name(name) = &*attr.value {
                            if name.id.as_str() == "self" {
                                attributes.push(PythonAttribute {
                                    name: attr.attr.to_string(),
                                    type_hint: None, // Type inference could be added later
                                });
                            }
                        }
                    }
                }
            } else if let ast::Stmt::AnnAssign(ann_assign) = stmt {
                // Handle annotated assignments: self.x: int = 0
                if let ast::Expr::Attribute(attr) = &*ann_assign.target {
                    if let ast::Expr::Name(name) = &*attr.value {
                        if name.id.as_str() == "self" {
                            let type_hint = self.extract_type_from_expr(&ann_assign.annotation);

                            attributes.push(PythonAttribute {
                                name: attr.attr.to_string(),
                                type_hint,
                            });
                        }
                    }
                }
            }
        }

        attributes
    }

    /// Extract type annotation from expression
    fn extract_type_from_expr(&self, expr: &ast::Expr) -> Option<String> {
        match expr {
            ast::Expr::Name(name) => Some(name.id.to_string()),
            ast::Expr::Constant(constant) => {
                // Handle string type annotations
                if let ast::Constant::Str(s) = &constant.value {
                    Some(s.to_string())
                } else {
                    None
                }
            }
            ast::Expr::Subscript(subscript) => {
                // Handle generic types like List[int], Dict[str, int]
                let base = self.expr_to_string(&subscript.value)?;
                let slice = self.expr_to_string(&subscript.slice)?;
                Some(format!("{}[{}]", base, slice))
            }
            ast::Expr::Tuple(tuple) => {
                // Handle tuple type annotations
                let elements: Vec<String> = tuple
                    .elts
                    .iter()
                    .filter_map(|e| self.expr_to_string(e))
                    .collect();
                Some(format!("({})", elements.join(", ")))
            }
            _ => self.expr_to_string(expr),
        }
    }

    /// Convert expression to string (simplified)
    fn expr_to_string(&self, expr: &ast::Expr) -> Option<String> {
        match expr {
            ast::Expr::Name(name) => Some(name.id.to_string()),
            ast::Expr::Constant(constant) => match &constant.value {
                ast::Constant::Int(i) => Some(i.to_string()),
                ast::Constant::Float(f) => Some(f.to_string()),
                ast::Constant::Str(s) => Some(s.to_string()),
                ast::Constant::Bool(b) => Some(b.to_string()),
                ast::Constant::None => Some("None".to_string()),
                _ => None,
            },
            ast::Expr::Attribute(attr) => {
                let value = self.expr_to_string(&attr.value)?;
                Some(format!("{}.{}", value, attr.attr))
            }
            _ => None,
        }
    }
}

impl Default for EnhancedParser {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_simple_function() {
        let parser = EnhancedParser::new();
        let source = r#"
def add(a: int, b: int) -> int:
    return a + b
"#;

        let ast = parser.parse(source).unwrap();
        assert_eq!(ast.functions.len(), 1);
        assert_eq!(ast.functions[0].name, "add");
        assert_eq!(ast.functions[0].params.len(), 2);
        assert_eq!(ast.functions[0].return_type, Some("int".to_string()));
    }

    #[test]
    fn test_parse_multiple_functions() {
        let parser = EnhancedParser::new();
        let source = r#"
def add(a: int, b: int) -> int:
    return a + b

def multiply(x: int, y: int) -> int:
    return x * y

def greet(name: str) -> str:
    return f"Hello, {name}"
"#;

        let ast = parser.parse(source).unwrap();
        assert_eq!(ast.functions.len(), 3);
        assert_eq!(ast.functions[0].name, "add");
        assert_eq!(ast.functions[1].name, "multiply");
        assert_eq!(ast.functions[2].name, "greet");
    }

    #[test]
    fn test_parse_class() {
        let parser = EnhancedParser::new();
        let source = r#"
class Calculator:
    def add(self, a: int, b: int) -> int:
        return a + b

    def subtract(self, a: int, b: int) -> int:
        return a - b
"#;

        let ast = parser.parse(source).unwrap();
        assert_eq!(ast.classes.len(), 1);
        assert_eq!(ast.classes[0].name, "Calculator");
        assert_eq!(ast.classes[0].methods.len(), 2);
    }

    #[test]
    fn test_parse_class_with_init() {
        let parser = EnhancedParser::new();
        let source = r#"
class Counter:
    def __init__(self):
        self.count = 0

    def increment(self) -> int:
        self.count = self.count + 1
        return self.count
"#;

        let ast = parser.parse(source).unwrap();
        assert_eq!(ast.classes.len(), 1);
        assert_eq!(ast.classes[0].name, "Counter");
        assert_eq!(ast.classes[0].methods.len(), 2);
        assert_eq!(ast.classes[0].attributes.len(), 1);
        assert_eq!(ast.classes[0].attributes[0].name, "count");
    }

    #[test]
    fn test_parse_class_with_typed_attributes() {
        let parser = EnhancedParser::new();
        let source = r#"
class Rectangle:
    def __init__(self, width: float, height: float):
        self.width = width
        self.height = height

    def area(self) -> float:
        return self.width * self.height
"#;

        let ast = parser.parse(source).unwrap();
        assert_eq!(ast.classes.len(), 1);
        assert_eq!(ast.classes[0].name, "Rectangle");
        assert_eq!(ast.classes[0].methods.len(), 2);
        assert_eq!(ast.classes[0].attributes.len(), 2);
        assert_eq!(ast.classes[0].attributes[0].name, "width");
        assert_eq!(ast.classes[0].attributes[1].name, "height");
    }

    #[test]
    fn test_parse_imports() {
        let parser = EnhancedParser::new();
        let source = r#"
import os
import sys
from typing import List, Dict
from pathlib import Path
"#;

        let ast = parser.parse(source).unwrap();
        assert!(ast.imports.len() >= 2);
    }

    #[test]
    fn test_parse_function_with_defaults() {
        let parser = EnhancedParser::new();
        let source = r#"
def greet(name: str, greeting: str = "Hello") -> str:
    return f"{greeting}, {name}"
"#;

        let ast = parser.parse(source).unwrap();
        assert_eq!(ast.functions.len(), 1);
        assert_eq!(ast.functions[0].params.len(), 2);
        assert_eq!(ast.functions[0].params[1].default, Some("Hello".to_string()));
    }

    #[test]
    fn test_parse_complex_types() {
        let parser = EnhancedParser::new();
        let source = r#"
def process(items: list) -> dict:
    return {}
"#;

        let ast = parser.parse(source).unwrap();
        assert_eq!(ast.functions.len(), 1);
        // Type annotations should be captured
        assert!(ast.functions[0].return_type.is_some());
        assert_eq!(ast.functions[0].return_type, Some("dict".to_string()));
    }

    #[test]
    fn test_parse_decorators() {
        let parser = EnhancedParser::new();
        let source = r#"
@property
def name(self) -> str:
    return self._name
"#;

        let ast = parser.parse(source).unwrap();
        assert_eq!(ast.functions.len(), 1);
        assert_eq!(ast.functions[0].decorators.len(), 1);
        assert_eq!(ast.functions[0].decorators[0], "property");
    }
}