depyler-knowledge 3.23.0

Sovereign Type Database for Python library type extraction
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//! Extractor: Parse .pyi stub files to extract type facts.
//!
//! Uses `rustpython_parser` to parse Python stub files and extract
//! function signatures, class definitions, and method types.

use crate::{KnowledgeError, Result, TypeFact, TypeFactKind};
use rustpython_ast::{self as ast, Stmt};
use rustpython_parser::{parse, Mode};
use std::path::Path;
use tracing::{debug, warn};

/// Extractor for parsing Python stub files.
pub struct Extractor {
    /// Whether to include private symbols (starting with _)
    include_private: bool,
}

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

impl Extractor {
    /// Create a new Extractor.
    pub fn new() -> Self {
        Self {
            include_private: false,
        }
    }

    /// Include private symbols (starting with _).
    pub fn with_private(mut self) -> Self {
        self.include_private = true;
        self
    }

    /// Extract type facts from a single .pyi or .py file.
    pub fn extract_file(&self, path: &Path, module: &str) -> Result<Vec<TypeFact>> {
        let source = std::fs::read_to_string(path)?;
        self.extract_source(&source, module, path.to_string_lossy().as_ref())
    }

    /// Extract type facts from source code.
    pub fn extract_source(
        &self,
        source: &str,
        module: &str,
        filename: &str,
    ) -> Result<Vec<TypeFact>> {
        let parsed =
            parse(source, Mode::Module, filename).map_err(|e| KnowledgeError::StubParseError {
                file: filename.to_string(),
                message: e.to_string(),
            })?;

        let mut facts = Vec::new();

        // The parsed result is a Module containing statements
        if let ast::Mod::Module(module_ast) = parsed {
            for stmt in module_ast.body {
                self.extract_stmt(&stmt, module, &mut facts);
            }
        }

        debug!(
            module = %module,
            facts = facts.len(),
            "Extracted type facts"
        );

        Ok(facts)
    }

    /// Extract type facts from a statement.
    fn extract_stmt(&self, stmt: &Stmt, module: &str, facts: &mut Vec<TypeFact>) {
        match stmt {
            Stmt::FunctionDef(func) => {
                if self.should_include(&func.name) {
                    if let Some(fact) = self.extract_function(func, module) {
                        facts.push(fact);
                    }
                }
            }
            Stmt::AsyncFunctionDef(func) => {
                if self.should_include(&func.name) {
                    if let Some(fact) = self.extract_async_function(func, module) {
                        facts.push(fact);
                    }
                }
            }
            Stmt::ClassDef(class) => {
                if self.should_include(&class.name) {
                    self.extract_class(class, module, facts);
                }
            }
            Stmt::AnnAssign(assign) => {
                if let Some(fact) = self.extract_annotated_assign(assign, module) {
                    facts.push(fact);
                }
            }
            _ => {}
        }
    }

    /// Check if a symbol should be included based on privacy settings.
    fn should_include(&self, name: &str) -> bool {
        self.include_private || !name.starts_with('_')
    }

    /// Extract a function definition.
    fn extract_function(&self, func: &ast::StmtFunctionDef, module: &str) -> Option<TypeFact> {
        let signature = self.build_signature(&func.args, &func.returns);
        let return_type = self.type_to_string(&func.returns);

        Some(TypeFact {
            module: module.to_string(),
            symbol: func.name.to_string(),
            kind: TypeFactKind::Function,
            signature,
            return_type,
        })
    }

    /// Extract an async function definition.
    fn extract_async_function(
        &self,
        func: &ast::StmtAsyncFunctionDef,
        module: &str,
    ) -> Option<TypeFact> {
        let signature = self.build_signature(&func.args, &func.returns);
        let return_type = self.type_to_string(&func.returns);

        Some(TypeFact {
            module: module.to_string(),
            symbol: func.name.to_string(),
            kind: TypeFactKind::Function,
            signature: format!("async {signature}"),
            return_type,
        })
    }

    /// Extract a class and its methods.
    fn extract_class(&self, class: &ast::StmtClassDef, module: &str, facts: &mut Vec<TypeFact>) {
        // Add the class itself
        facts.push(TypeFact::class(module, &class.name));

        // Extract methods
        for stmt in &class.body {
            match stmt {
                Stmt::FunctionDef(method) => {
                    if self.should_include(&method.name) {
                        if let Some(fact) = self.extract_method(method, module, &class.name) {
                            facts.push(fact);
                        }
                    }
                }
                Stmt::AsyncFunctionDef(method) => {
                    if self.should_include(&method.name) {
                        if let Some(fact) = self.extract_async_method(method, module, &class.name) {
                            facts.push(fact);
                        }
                    }
                }
                Stmt::AnnAssign(assign) => {
                    if let Some(fact) = self.extract_class_attribute(assign, module, &class.name) {
                        facts.push(fact);
                    }
                }
                _ => {}
            }
        }
    }

    /// Extract a method from a class.
    fn extract_method(
        &self,
        method: &ast::StmtFunctionDef,
        module: &str,
        class_name: &str,
    ) -> Option<TypeFact> {
        let signature = self.build_signature(&method.args, &method.returns);
        let return_type = self.type_to_string(&method.returns);

        Some(TypeFact::method(
            module,
            class_name,
            &method.name,
            &signature,
            &return_type,
        ))
    }

    /// Extract an async method from a class.
    fn extract_async_method(
        &self,
        method: &ast::StmtAsyncFunctionDef,
        module: &str,
        class_name: &str,
    ) -> Option<TypeFact> {
        let signature = self.build_signature(&method.args, &method.returns);
        let return_type = self.type_to_string(&method.returns);

        Some(TypeFact::method(
            module,
            class_name,
            &method.name,
            &format!("async {signature}"),
            &return_type,
        ))
    }

    /// Extract an annotated assignment (module-level attribute).
    fn extract_annotated_assign(
        &self,
        assign: &ast::StmtAnnAssign,
        module: &str,
    ) -> Option<TypeFact> {
        let target = match assign.target.as_ref() {
            ast::Expr::Name(name) => name.id.to_string(),
            _ => return None,
        };

        if !self.should_include(&target) {
            return None;
        }

        let type_str = self.expr_to_string(&assign.annotation);

        Some(TypeFact {
            module: module.to_string(),
            symbol: target,
            kind: TypeFactKind::Attribute,
            signature: String::new(),
            return_type: type_str,
        })
    }

    /// Extract a class attribute.
    fn extract_class_attribute(
        &self,
        assign: &ast::StmtAnnAssign,
        module: &str,
        class_name: &str,
    ) -> Option<TypeFact> {
        let target = match assign.target.as_ref() {
            ast::Expr::Name(name) => name.id.to_string(),
            _ => return None,
        };

        if !self.should_include(&target) {
            return None;
        }

        let type_str = self.expr_to_string(&assign.annotation);

        Some(TypeFact {
            module: module.to_string(),
            symbol: format!("{class_name}.{target}"),
            kind: TypeFactKind::Attribute,
            signature: String::new(),
            return_type: type_str,
        })
    }

    /// Build a signature string from arguments and return type.
    fn build_signature(&self, args: &ast::Arguments, returns: &Option<Box<ast::Expr>>) -> String {
        let mut parts = Vec::new();

        // Positional-only params
        for param in &args.posonlyargs {
            parts.push(self.arg_with_default_to_string(param));
        }

        if !args.posonlyargs.is_empty() && !args.args.is_empty() {
            parts.push("/".to_string());
        }

        // Regular args
        for param in &args.args {
            parts.push(self.arg_with_default_to_string(param));
        }

        // *args
        if let Some(vararg) = &args.vararg {
            parts.push(format!("*{}", self.arg_to_string(vararg)));
        }

        // Keyword-only args
        for param in &args.kwonlyargs {
            parts.push(self.arg_with_default_to_string(param));
        }

        // **kwargs
        if let Some(kwarg) = &args.kwarg {
            parts.push(format!("**{}", self.arg_to_string(kwarg)));
        }

        let params_str = parts.join(", ");
        let return_str = self.type_to_string(returns);

        format!("({params_str}) -> {return_str}")
    }

    /// Convert an ArgWithDefault to string.
    fn arg_with_default_to_string(&self, arg: &ast::ArgWithDefault) -> String {
        let name = &arg.def.arg;
        let type_str = arg
            .def
            .annotation
            .as_ref()
            .map(|a| self.expr_to_string(a))
            .unwrap_or_default();

        if type_str.is_empty() {
            if arg.default.is_some() {
                format!("{name} = ...")
            } else {
                name.to_string()
            }
        } else if arg.default.is_some() {
            format!("{name}: {type_str} = ...")
        } else {
            format!("{name}: {type_str}")
        }
    }

    /// Convert an Arg to string (for vararg/kwarg).
    fn arg_to_string(&self, arg: &ast::Arg) -> String {
        let name = &arg.arg;
        let type_str = arg
            .annotation
            .as_ref()
            .map(|a| self.expr_to_string(a))
            .unwrap_or_default();

        if type_str.is_empty() {
            name.to_string()
        } else {
            format!("{name}: {type_str}")
        }
    }

    /// Convert return type to string.
    fn type_to_string(&self, returns: &Option<Box<ast::Expr>>) -> String {
        match returns {
            Some(expr) => self.expr_to_string(expr),
            None => "None".to_string(),
        }
    }

    /// Convert an expression to a type string.
    fn expr_to_string(&self, expr: &ast::Expr) -> String {
        match expr {
            ast::Expr::Name(name) => name.id.to_string(),
            ast::Expr::Attribute(attr) => {
                let value = self.expr_to_string(&attr.value);
                format!("{value}.{}", attr.attr)
            }
            ast::Expr::Subscript(sub) => {
                let value = self.expr_to_string(&sub.value);
                let slice = self.expr_to_string(&sub.slice);
                format!("{value}[{slice}]")
            }
            ast::Expr::Tuple(tuple) => {
                let elts: Vec<_> = tuple.elts.iter().map(|e| self.expr_to_string(e)).collect();
                elts.join(", ")
            }
            ast::Expr::BinOp(binop) => {
                // Handle Union types written as X | Y
                if matches!(binop.op, ast::Operator::BitOr) {
                    let left = self.expr_to_string(&binop.left);
                    let right = self.expr_to_string(&binop.right);
                    format!("{left} | {right}")
                } else {
                    "Unknown".to_string()
                }
            }
            ast::Expr::Constant(c) => match &c.value {
                ast::Constant::None => "None".to_string(),
                ast::Constant::Str(s) => format!("\"{s}\""),
                ast::Constant::Int(i) => i.to_string(),
                ast::Constant::Float(f) => f.to_string(),
                ast::Constant::Bool(b) => b.to_string(),
                ast::Constant::Ellipsis => "...".to_string(),
                _ => "Unknown".to_string(),
            },
            ast::Expr::List(list) => {
                let elts: Vec<_> = list.elts.iter().map(|e| self.expr_to_string(e)).collect();
                format!("[{}]", elts.join(", "))
            }
            _ => {
                warn!("Unknown expression type in type annotation");
                "Unknown".to_string()
            }
        }
    }
}

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

    #[test]
    fn test_extract_simple_function() {
        let source = r#"
def get(url: str) -> Response: ...
"#;
        let extractor = Extractor::new();
        let facts = extractor
            .extract_source(source, "requests", "test.pyi")
            .unwrap();

        assert_eq!(facts.len(), 1);
        assert_eq!(facts[0].symbol, "get");
        assert_eq!(facts[0].kind, TypeFactKind::Function);
        assert!(facts[0].signature.contains("url: str"));
        assert_eq!(facts[0].return_type, "Response");
    }

    #[test]
    fn test_extract_function_with_optional() {
        let source = r#"
def get(url: str, params: dict | None = ...) -> Response: ...
"#;
        let extractor = Extractor::new();
        let facts = extractor
            .extract_source(source, "requests", "test.pyi")
            .unwrap();

        assert_eq!(facts.len(), 1);
        assert!(facts[0].signature.contains("params: dict | None"));
    }

    #[test]
    fn test_extract_class_with_methods() {
        let source = r#"
class Response:
    status_code: int
    def json(self) -> dict: ...
    def text(self) -> str: ...
"#;
        let extractor = Extractor::new();
        let facts = extractor
            .extract_source(source, "requests.models", "test.pyi")
            .unwrap();

        // Should have: class, status_code attribute, json method, text method
        assert_eq!(facts.len(), 4);

        let class_fact = facts.iter().find(|f| f.symbol == "Response").unwrap();
        assert_eq!(class_fact.kind, TypeFactKind::Class);

        let json_fact = facts.iter().find(|f| f.symbol == "Response.json").unwrap();
        assert_eq!(json_fact.kind, TypeFactKind::Method);
        assert_eq!(json_fact.return_type, "dict");
    }

    #[test]
    fn test_excludes_private_by_default() {
        let source = r#"
def _private(): ...
def public(): ...
"#;
        let extractor = Extractor::new();
        let facts = extractor
            .extract_source(source, "test", "test.pyi")
            .unwrap();

        assert_eq!(facts.len(), 1);
        assert_eq!(facts[0].symbol, "public");
    }

    #[test]
    fn test_includes_private_when_enabled() {
        let source = r#"
def _private(): ...
def public(): ...
"#;
        let extractor = Extractor::new().with_private();
        let facts = extractor
            .extract_source(source, "test", "test.pyi")
            .unwrap();

        assert_eq!(facts.len(), 2);
    }

    #[test]
    fn test_extract_kwargs() {
        let source = r#"
def get(url: str, **kwargs) -> Response: ...
"#;
        let extractor = Extractor::new();
        let facts = extractor
            .extract_source(source, "requests", "test.pyi")
            .unwrap();

        assert!(facts[0].signature.contains("**kwargs"));
    }
}