Skip to content

Python: absolute imports drop all calls edges when the scan root is a subdirectory of the import root #3843

Closed

Description

@jonasplima

graphifyy==0.9.68 (uv tool), Python 3.11, macOS. AST only (--code-only --no-cluster), no LLM.

Summary

In a monorepo, a team often scans only its own subfolder. Python imports in that subfolder are still absolute from the repo root, so they carry a prefix that does not exist under the scan root. The imports_from edge then targets a dotted-name id that matches no node, the receiver is never bound to the local module, and every mod.func() call through it loses its calls edge.

The files are byte-identical in both runs below. Only the scan root changes.

Minimal reproducer

Company/__init__.py                          (empty)
Company/Apps/__init__.py                     (empty)
Company/Apps/Jobs/__init__.py                (empty)
Company/Apps/Jobs/Team/__init__.py           (empty)
Company/Apps/Jobs/Team/lib/__init__.py       (empty)
Company/Apps/Jobs/Team/app/__init__.py       (empty)
Company/Apps/Jobs/Team/lib/delivery.py
Company/Apps/Jobs/Team/app/main.py
# Company/Apps/Jobs/Team/lib/delivery.py
def deliver(files):
    return len(files)
# Company/Apps/Jobs/Team/app/main.py
from Company.Apps.Jobs.Team.lib import delivery


def run():
    return delivery.deliver(["a"])

Scan from the import root: works

cd <root> && graphify extract . --code-only --no-cluster
imports_from: company_apps_jobs_team_lib_init, company_apps_jobs_team_lib_delivery
calls:        company_apps_jobs_team_app_main_run -> company_apps_jobs_team_lib_delivery_deliver

Scan from the team subfolder: no calls edge

cd <root>/Company/Apps/Jobs/Team && graphify extract . --code-only --no-cluster
imports_from: company_apps_jobs_team_lib      (no node with this id)
calls:        (none)

Expected

With Team/ as the scan root, Company.Apps.Jobs.Team.lib should resolve to the local lib/ package, and run() should get calls -> lib_delivery_deliver.

Why this is not a duplicate

Real impact

On a private repo, scanning one team's 349-file subfolder gave 0 incoming calls edges for a shared helper that is called from 4 automation files. That is the impact analysis case the scope was chosen for. In the reproducer, scanning from the import root restores the edge. On the real repo that means indexing the whole monorepo, which is what the subfolder scan was avoiding.

Suggestion

Either one would work for this layout:

  1. Probe the dotted name after stripping leading components until the rest resolves under the scan root, when the stripped prefix matches the scan root's own path relative to a parent that has __init__.py files up the chain.
  2. An explicit option, for example --python-root <dir> or a .graphify config key, that tells the resolver which directory absolute imports are relative to.

Option 2 is smaller and has no guessing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions