Skip to content

Python dynamic imports via importlib and __import__() not indexed as symbol references #2056

Description

@Widthdom

Summary

Python code using importlib.import_module('module.name'), __import__('package'), or importlib.util.find_spec() never creates import symbols or references. These dynamic import patterns are idiomatic in plugin systems, configuration-driven module loading, and lazy imports. Currently only static import and from ... import statements are recognized; all programmatic import sites are invisible to the call graph and symbol dependency map.

Where

  • src/CodeIndex/Indexer/Symbols/SymbolExtractor.Python.cs:56-126 — TryExpandPythonImportSymbols only processes static import statements (regex on lines 12-13)
  • src/CodeIndex/Indexer/References/Languages/PythonReferenceExtractor.cs:118-150 — EmitDecoratorReferences does not cover dynamic module resolution patterns

Suggested approach

  1. Add regex patterns for importlib.import_module(...), __import__(...), and importlib.util.find_spec(...)
  2. Extract the module name argument as an import symbol (with dynamic fallback kind if string is not a literal)
  3. Register calls to importlib functions as references to the importlib module

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions