Skip to content

Import resolver links a dotted import to any lone same-named file, ignoring its package path (false dependency edges) #3544

Closed

Description

@squid-protocol

Problem

from starlette.requests import Request in fastapi/routing.py draws a dependency edge to fastapi/requests.py. The resolver drops the package prefix, finds exactly one local file whose stem is requests, and links it. Library and stdlib imports whose last segment matches a local module name therefore become false edges in the import graph, which feeds PageRank, blast radius, dependency_density and popularity.

Mechanism

network_risk_sensor.py, the edge resolver:

  1. Stage 1 tries lookup_forms in order, ending with bare_component, the dot-rewritten final segment (starlette.requests → requests).
  2. On a hit, if len(candidates) == 1: return candidates[0] returns before Stage 2.
  3. Stage 2 is the only place the token's path context (match_cmp) is checked against the candidate's path, and it runs only when several files share the stem.

So the path context is used to choose among several candidates, but never to reject a single one. #261 decided that an ambiguous bare stem draws no edge; this is the unambiguous-but-wrong case, which that rule doesn't cover.

Measured

Python, language-crucible v1.5.0 (data/python, 259 files, scanned as one repo). Engine edge_data was compared with edges built from Python's ast module, resolving absolute and relative imports to files present in the corpus. Every engine-only edge was checked against source.

  • Engine edges: 326. AST edges: 492. In both: 285.
  • Of the 41 engine-only edges:
    • 4 are correct and the AST side missed them: numpy's __import__('genapi') and spec_from_file_location(... 'genapi.py') (3), and twisted's corpus-flattened defer.py (1).
    • About 37 are this bug. Precision is about 88%, and essentially all of the error is this mechanism.

Examples, each checked against the importing line:

importing file statement wrong target
fastapi/fastapi/routing.py from starlette.requests import Request fastapi/fastapi/requests.py
fastapi/fastapi/routing.py from starlette.concurrency import ... fastapi/fastapi/concurrency.py
twisted/transport.py import types (stdlib) fastapi/fastapi/types.py
twisted/transport.py from cryptography.exceptions import ... fastapi/fastapi/exceptions.py
fastapi/fastapi/types.py from pydantic.main import IncEx fastapi/tests/main.py
fastapi/tests/test_security_http_base.py from fastapi.security.http import ... twisted/http.py
airflow/scheduler_job_runner.py from airflow.models.dag import DagModel airflow/dag.py

The corpus mixes repos, so some of these cross repos. The same mechanism fires inside a single real repo, though: FastAPI mirrors starlette's module names (requests, responses, concurrency, websockets), so those files' blast radius is inflated on a plain FastAPI scan.

Fix direction

When the token carries path context (a dotted or compound import), require the lone candidate to pass the same check Stage 2 applies: _stem_path(candidate).endswith(match_cmp), or at least a suffix match on the package segments. Otherwise draw no edge, consistent with #261.

  • Keep relative imports and bare single-segment tokens on their current path.
  • Check languages whose import token is a bare module name with no package (e.g. import utils) aren't affected: they have no path context to fail.
  • Re-bless the golden masters and check the call-graph and ground-truth gates. COBOL COPY members go through SOURCE_MEMBER_IMPORT_LANGS and must not change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcore-engine

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions