pydep-mapper 0.1.5

Fast Rust CLI for analyzing Python dependencies with external package declarations support.
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
use anyhow::Result;
use rustpython_parser::ast::{Mod, Stmt};
use rustpython_parser::{Mode, parse};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;

/// Represents the origin type of a Python module.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ModuleOrigin {
    External, // Standard library and third-party packages
    Internal, // Project modules within the same codebase
}

/// Unique identifier for a Python module.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ModuleIdentifier {
    pub origin: ModuleOrigin,
    pub canonical_path: String,
}

/// Extracts the root module name from a dotted module path.
fn extract_root_module(module_name: &str) -> &str {
    module_name.split('.').next().unwrap_or(module_name)
}

/// Resolves relative imports to absolute module paths.
fn resolve_relative_import(module_name: &str, level: u32, current_module: &str) -> Option<String> {
    if level == 0 {
        return Some(module_name.to_string());
    }

    let current_parts: Vec<&str> = current_module.split('.').collect();

    // Check if we have enough parent levels
    if level as usize > current_parts.len() {
        return None; // Invalid relative import - goes beyond package root
    }

    // Calculate the target parent level
    let parent_level = current_parts.len() - level as usize;
    let parent_path = current_parts[..parent_level].join(".");

    if module_name.is_empty() {
        // from . import something or from .. import something
        Some(parent_path)
    } else {
        // from .module import something or from ..parent.module import something
        if parent_path.is_empty() {
            Some(module_name.to_string())
        } else {
            Some(format!("{}.{}", parent_path, module_name))
        }
    }
}

/// Resolves a module name to a ModuleIdentifier.
fn resolve_module_identifier(module_name: &str) -> ModuleIdentifier {
    let origin = if crate::pyproject::is_internal_module(module_name) {
        ModuleOrigin::Internal
    } else {
        ModuleOrigin::External
    };

    let canonical_path = match origin {
        ModuleOrigin::Internal => crate::pyproject::normalize_module_name(module_name)
            .unwrap_or_else(|_| module_name.to_string()),
        _ => extract_root_module(module_name).to_string(),
    };

    ModuleIdentifier {
        origin,
        canonical_path,
    }
}

/// Processes a Python AST statement and extracts module dependencies.
fn process_stmt(
    stmt: &Stmt,
    modules: &mut HashSet<ModuleIdentifier>,
    current_module: Option<&str>,
) {
    match stmt {
        Stmt::Import(import_stmt) => {
            for alias in &import_stmt.names {
                let module_id = resolve_module_identifier(&alias.name);
                modules.insert(module_id);
            }
        }
        Stmt::ImportFrom(import_from_stmt) => {
            // Handle relative imports by resolving them to absolute paths
            let level = import_from_stmt
                .level
                .as_ref()
                .map(|_level_int| {
                    // For now, we'll extract the level by parsing the debug representation
                    // This is a limitation of the current rustpython-parser API
                    let debug_str = format!("{:?}", _level_int);

                    // Look for a numeric value in the debug string
                    for char in debug_str.chars() {
                        if char.is_ascii_digit() {
                            if let Some(digit) = char.to_digit(10) {
                                return digit;
                            }
                        }
                    }

                    // If no digit found, assume level 1 for relative imports
                    1
                })
                .unwrap_or(0);

            if level > 0 {
                // This is a relative import
                if let Some(current_mod) = current_module {
                    let module_name = import_from_stmt.module.as_deref().unwrap_or("");
                    if let Some(resolved_module) =
                        resolve_relative_import(module_name, level, current_mod)
                    {
                        let module_id = resolve_module_identifier(&resolved_module);
                        modules.insert(module_id);
                    }
                }
                // If no current_module context, we can't resolve relative imports, so skip
            } else if let Some(module) = &import_from_stmt.module {
                // Regular absolute import
                let module_id = resolve_module_identifier(module);
                modules.insert(module_id);
            }
        }
        _ => {}
    }
}

/// Processes a collection of Python AST statements.
fn process_body(
    body: &[Stmt],
    modules: &mut HashSet<ModuleIdentifier>,
    current_module: Option<&str>,
) {
    for stmt in body {
        process_stmt(stmt, modules, current_module);
    }
}

/// Extracts module dependencies from Python source code with context for resolution.
pub fn extract_module_deps(
    python_code: &str,
    current_module: Option<&str>,
) -> Result<Vec<ModuleIdentifier>> {
    let ast = parse(python_code, Mode::Module, "<string>")?;
    let mut modules = HashSet::new();

    match ast {
        Mod::Module(module) => process_body(&module.body, &mut modules, current_module),
        Mod::Interactive(interactive) => {
            process_body(&interactive.body, &mut modules, current_module)
        }
        Mod::Expression(_) => {} // No statements to visit in expression mode
        Mod::FunctionType(_) => {} // No statements to visit in function type mode
    }

    Ok(modules.into_iter().collect())
}

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

    #[test]
    fn test_relative_imports_resolved() {
        // Test that relative imports are properly resolved to internal dependencies
        let python_code = r#"
from .module import something
from ..parent import other
from ...grandparent import more
import os
"#;
        let current_module = "common.models.submodule.current";
        let modules = extract_module_deps(python_code, Some(current_module)).unwrap();

        // Should contain resolved relative imports plus "os" (but root extracted)
        assert_eq!(modules.len(), 2); // "common" (from all relative imports) + "os"

        let module_names: std::collections::HashSet<String> =
            modules.iter().map(|m| m.canonical_path.clone()).collect();

        // Since we don't have pyproject.toml in tests, all modules are external and get root extracted
        // The important thing is that relative imports were resolved and included
        assert!(module_names.contains("common")); // All relative imports resolve to "common" root
        assert!(module_names.contains("os")); // import os
    }

    #[test]
    fn test_relative_imports_without_context() {
        // Test that relative imports are ignored when no current module context is provided
        let python_code = r#"
from .module import something
from ..parent import other
import os
"#;
        let modules = extract_module_deps(python_code, None).unwrap();

        // Should only contain "os", relative imports are skipped without context
        assert_eq!(modules.len(), 1);
        assert_eq!(modules[0].canonical_path, "os");
        assert_eq!(modules[0].origin, ModuleOrigin::External);
    }

    #[test]
    fn test_relative_imports_edge_cases() {
        // Test edge cases in relative import resolution
        let python_code = r#"
from . import something
from .. import other  
from .sub.module import func
"#;
        let current_module = "package.subpackage.module";
        let modules = extract_module_deps(python_code, Some(current_module)).unwrap();

        assert_eq!(modules.len(), 1); // All resolve to "package" root since no pyproject.toml
        let module_names: std::collections::HashSet<String> =
            modules.iter().map(|m| m.canonical_path.clone()).collect();

        assert!(module_names.contains("package")); // All relative imports resolve to "package" root
    }

    #[test]
    fn test_simple_import() {
        let python_code = "import os";
        let modules = extract_module_deps(python_code, None).unwrap();

        assert_eq!(modules.len(), 1);
        assert_eq!(modules[0].canonical_path, "os");
    }

    #[test]
    fn test_multiple_simple_imports() {
        let python_code = "import os, sys, json";
        let modules = extract_module_deps(python_code, None).unwrap();

        assert_eq!(modules.len(), 3);
        let module_names: HashSet<String> =
            modules.iter().map(|m| m.canonical_path.clone()).collect();
        assert!(module_names.contains("os"));
        assert!(module_names.contains("sys"));
        assert!(module_names.contains("json"));
    }

    #[test]
    fn test_from_import() {
        let python_code = "from collections import defaultdict";
        let modules = extract_module_deps(python_code, None).unwrap();

        assert_eq!(modules.len(), 1);
        assert_eq!(modules[0].canonical_path, "collections");
    }

    #[test]
    fn test_from_import_multiple() {
        let python_code = "from os.path import join, exists, dirname";
        let modules = extract_module_deps(python_code, None).unwrap();

        assert_eq!(modules.len(), 1);
        assert_eq!(modules[0].canonical_path, "os");
    }

    #[test]
    fn test_from_import_star() {
        let python_code = "from math import *";
        let modules = extract_module_deps(python_code, None).unwrap();

        assert_eq!(modules.len(), 1);
        assert_eq!(modules[0].canonical_path, "math");
    }

    #[test]
    fn test_mixed_imports() {
        let python_code = r#"
import os
from sys import argv
from collections import *
import json, re
from typing import List, Dict
"#;
        let modules = extract_module_deps(python_code, None).unwrap();

        assert_eq!(modules.len(), 6);
        let module_names: HashSet<String> =
            modules.iter().map(|m| m.canonical_path.clone()).collect();
        assert!(module_names.contains("os"));
        assert!(module_names.contains("sys"));
        assert!(module_names.contains("collections"));
        assert!(module_names.contains("json"));
        assert!(module_names.contains("re"));
        assert!(module_names.contains("typing"));
    }

    #[test]
    fn test_no_imports() {
        let python_code = r#"
def hello():
    print("Hello, world!")

x = 42
"#;
        let modules = extract_module_deps(python_code, None).unwrap();
        assert_eq!(modules.len(), 0);
    }

    #[test]
    fn test_invalid_python_code() {
        let python_code = "import os\ndef invalid syntax here";
        let result = extract_module_deps(python_code, None);
        assert!(result.is_err());
    }

    #[test]
    fn test_empty_code() {
        let python_code = "";
        let modules = extract_module_deps(python_code, None).unwrap();
        assert_eq!(modules.len(), 0);
    }

    #[test]
    fn test_nested_from_import() {
        let python_code: &'static str = "from package.submodule.deep import function_name";
        let modules = extract_module_deps(python_code, None).unwrap();
        assert_eq!(modules.len(), 1);
        assert_eq!(modules[0].canonical_path, "package");
    }

    #[test]
    fn test_import_aliases() {
        let python_code: &'static str = r#"
from collections import defaultdict as dd
import numpy as np
"#;
        let modules = extract_module_deps(python_code, None).unwrap();
        assert_eq!(modules.len(), 2);
        let module_names: HashSet<String> =
            modules.iter().map(|m| m.canonical_path.clone()).collect();
        assert!(module_names.contains("collections"));
        assert!(module_names.contains("numpy"));

        // Check origins
        let collections_module = modules
            .iter()
            .find(|m| m.canonical_path == "collections")
            .unwrap();
        assert_eq!(collections_module.origin, ModuleOrigin::External);

        let numpy_module = modules
            .iter()
            .find(|m| m.canonical_path == "numpy")
            .unwrap();
        assert_eq!(numpy_module.origin, ModuleOrigin::External);
    }

    #[test]
    fn test_builtin_vs_internal_detection() {
        let python_code = r#"
import os
import sys
import custom_module
"#;
        let modules = extract_module_deps(python_code, None).unwrap();
        assert_eq!(modules.len(), 3);
        let module_names: HashSet<String> =
            modules.iter().map(|m| m.canonical_path.clone()).collect();
        assert!(module_names.contains("os"));
        assert!(module_names.contains("sys"));
        assert!(module_names.contains("custom_module"));

        // os should be detected as external
        let os_module = modules.iter().find(|m| m.canonical_path == "os").unwrap();
        assert_eq!(os_module.origin, ModuleOrigin::External);

        // sys should be detected as external
        let sys_module = modules.iter().find(|m| m.canonical_path == "sys").unwrap();
        assert_eq!(sys_module.origin, ModuleOrigin::External);

        // custom_module should be detected as external (since no pyproject.toml in test)
        let custom_module = modules
            .iter()
            .find(|m| m.canonical_path == "custom_module")
            .unwrap();
        assert_eq!(custom_module.origin, ModuleOrigin::External);
    }

    #[test]
    fn test_root_module_extraction() {
        let python_code = r#"
import os.path
from collections.abc import Mapping
import numpy.testing.utils
from requests.auth import HTTPBasicAuth
"#;
        let modules = extract_module_deps(python_code, None).unwrap();

        assert_eq!(modules.len(), 4);
        let module_names: HashSet<String> =
            modules.iter().map(|m| m.canonical_path.clone()).collect();

        // All should be normalized to root modules
        assert!(module_names.contains("os"));
        assert!(module_names.contains("collections"));
        assert!(module_names.contains("numpy"));
        assert!(module_names.contains("requests"));

        // Verify they don't contain full paths
        assert!(!module_names.contains("os.path"));
        assert!(!module_names.contains("collections.abc"));
        assert!(!module_names.contains("numpy.testing.utils"));
        assert!(!module_names.contains("requests.auth"));
    }
}