scan.find_importers

scan.find_importers(
    root,
    unit,
    public=(),
    *,
    source_dir='src',
    scan_dirs=('src', 'tests', 'scripts'),
    doc_globs=(),
)

Every file below root that depends on the module unit.

Python files under scan_dirs count when an import statement names the module, or imports one of its public names through a package that re-exports it (from samplepkg.core import add). Documents matched by doc_globs count when their text names the module, imports its leaf from the parent package or imports a re-exported public name. Attribute access through an imported package (samplepkg.core.add) is not detected.

Parameters

Name Type Description Default
root Path | str Repository root. required
unit str Dotted module name. required
public Iterable[str] Public names of the unit, used to follow package re-exports. ()
source_dir str Directory (relative to root) that holds the package. 'src'
scan_dirs Iterable[str] Directories whose Python files are scanned. ('src', 'tests', 'scripts')
doc_globs Iterable[str] Glob patterns of documents whose text is scanned. ()

Returns

Name Type Description
list[str] list[str]: Repository-relative paths, Python files first, excluding the unit’s own file.

Examples

from pathlib import Path
from tempfile import TemporaryDirectory

from spotregen import find_importers, make_sample_repo

with TemporaryDirectory() as tmp:
    root = make_sample_repo(Path(tmp) / "demo")
    hits = find_importers(
        root, "samplepkg.core.calc", ["add"], doc_globs=["docs/*.qmd"]
    )
    print(hits)
['src/samplepkg/app/main.py', 'tests/test_regen_eval_calc.py', 'docs/guide.qmd']