module Noir::ImportGraph::Python

Overview


Python flavour.

Python imports are dotted-module paths anchored at one of:

  1. app_base_path — the project's source root (the app directory passed by the analyzer).
  2. The current file's directory, for relative imports (from . import x, from .. import x).

The resolver walks the dotted path one segment at a time, preferring directories (Python packages) over file siblings (modules) until either a leaf .py is found or the path runs out — matching the behaviour expected by the existing Django / FastAPI / Tornado analyzers.

Lives under the shared Noir::ImportGraph umbrella so a future Python analyzer migration off the legacy PythonParser (still used by Flask) can reuse the same resolver. Until that migration lands the JVM / JS / Python flavours all share the top-level module but each operates on its own data shapes — Python's hashes look different from the JVM ImportRef form because Python returns import-name → file-path mappings rather than a yield of files.

Defined in:

miniparsers/import_graph.cr

Class Method Summary

Class Method Detail

def self.find_imported_modules(app_base_path : String, file_path : String, content : String | Nil = nil) : Hash(String, Tuple(String, Int32)) #

Find every import and from … import … line in content and resolve each name to a {filepath, package_type} tuple. Returns an empty hash for files that import nothing resolvable. The map is keyed on the IMPORTED-IN-LOCAL name — from a.b import c as d keys d, not c.

app_base_path anchors the dotted path for absolute imports; file_path's directory anchors relative imports. Pass content when the caller already has the file in hand to skip the second File.read.


[View source]
def self.find_imported_package(package_path : String, dotted_as_names : String) : Array(Tuple(String, String, Int32)) #

Resolve a dotted Python identifier (a.b.c or a.b.c, d.e as f) under package_path. Walks segment by segment, preferring packages (directories) over modules (.py files). Returns each resolvable name as {name, filepath, package_type} so the caller can keep the alias mapping intact.


[View source]