Skip to content

Python adapter: build the import graph #56

Closed

Description

@adityankale190895

Part of the Python (pytest) adapter: #61.

Build the file-level dependency graph for a Python project, so a change to app/models.py selects only the tests that import it directly or transitively.

Scope

  • Parse import x, import x.y as z, from x import y and relative imports (from . import y, from ..pkg import y), and resolve them to files in the repository (x/y.py or x/y/__init__.py), for both flat and src/ layouts.
  • Imports of packages outside the repository are ignored, not treated as errors.
  • Importing a nested module also runs every parent package's __init__.py (import pkg.sub.deep runs pkg/__init__.py and pkg/sub/__init__.py), so add edges to each of those too.
  • from pkg import name loads pkg/name.py only when pkg/__init__.py doesn't already define name. Don't try to tell the cases apart: add edges to pkg/__init__.py and, if it exists, to pkg/name.py.
  • conftest.py applies to every test in its directory and below, so add edges from those tests to it.
  • Return DependencyEdges with kind: "import" from the adapter's analyze().

Must fall back (blocker)

  • importlib.import_module, __import__, or sys.path changes in test or source files.
  • An import that looks local but can't be resolved.
  • Namespace packages without __init__.py when more than one root could match.

Done when

  • Fixtures under tests/repo/language-adapters.test.ts cover a direct dependency, a transitive dependency, a change to a parent package's __init__.py, from pkg import name where name is a submodule, an unrelated change, a conftest.py change, and a dynamic import that falls back.

Parsing approach: a careful line-based scan is fine for a first version if it handles multi-line from x import (a, b) and ignores imports inside strings. Say in the PR what it doesn't handle.

Safety rule: when the adapter is unsure, it adds a blockers entry so DiffCI runs the full suite. An empty graph must never look like "nothing affected".


New to DiffCI? Read the adapter guide and CONTRIBUTING.md. Sign off commits with git commit -s. Comment here to claim it, and ask questions on the issue at any point.

Activity

  1. added
    enhancement
    help wanted
    on Oct 29, 2026
  2. robertwmccarty-caelum commented on Oct 30, 2026

    @robertwmccarty-caelum
    Contributor

    Read-only, AI-assisted contribution for the human BuddyLists / Linked AIs operator (not an independently operated agent account). I tested a Python import-graph false-negative risk relevant to #56's selective-test safety rule.

    An import of a nested module executes its ancestor package initializers. Resolving import pkg.sub.deep only to pkg/sub/deep.py misses executable code in pkg/__init__.py and pkg/sub/__init__.py. A change to either initializer can affect tests importing the deep module, even if the deep module's bytes are unchanged.

    A second ambiguity: from pkg import child may resolve child as an attribute already defined in pkg/__init__.py, without importing pkg/child.py, or it may import that submodule when the attribute is absent. A scanner that always maps this statement solely to pkg/child.py is unsound.

    Isolated reproduction: Python 3 standard-library unittest + temporary packages + fresh subprocesses; 5/5 passed:

    1. import pkg.sub loads pkg and pkg.sub.
    2. import pkg.sub.deep loads all three package/module levels.
    3. from pkg import child loads the child module if the package doesn't define the attribute.
    4. The same statement does not load the child module if pkg/__init__.py already defines child = 42.
    5. A deliberate exception in pkg/__init__.py prevents import pkg.sub from completing, proving the initializer is on the execution path.

    Suggested fixture assertions: create pkg/__init__.py, pkg/sub/__init__.py, pkg/sub/deep.py and a test importing pkg.sub.deep; changing either initializer must select that test. For from pkg import child, conservatively include the package initializer and plausible local submodule, or mark ambiguous cases as blockers/full-suite fallback. If a namespace package makes ancestor resolution ambiguous, keep the existing blocker behavior.

    Reference: Python import-system documentation, especially parent packages and submodules: https://docs.python.org/3/reference/import.html#submodules .

    These tests reproduce Python interpreter behavior only; I did not execute DiffCI's repository tests or validate its current adapter. No registration, product pitch, or production change.

  3. adityankale190895 commented on Oct 31, 2026

    @adityankale190895
    MemberAuthor

    Thanks, both points are right. I've added them to the scope: an import now also adds edges to every parent package's __init__.py, and from pkg import name adds edges to both pkg/__init__.py and pkg/name.py (when it exists) instead of trying to work out which one Python loads. The fixture list now includes a parent-__init__.py change and the from pkg import name case.

  4. adityankale190895 commented on Nov 7, 2026

    @adityankale190895
    MemberAuthor

    Shipped in CLI 0.3.33 (Core 0fd9ed0) as part of the pytest adapter. Real-repository feedback is tracked in #75.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    adapterenhancementhelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions