Skip to content

[Bug]: Python AST extraction merges same-named nested classes under different parents #4220

Closed

Description

@john-ramsey

Pre-flight checks

  • I have checked the Troubleshooting section in the README

What happened?

Graphify gives distinct nested Python classes the same node ID when they have the same name in one file but different enclosing classes. It emits one class node with multiple contains parents, losing the other definition and conflating relationships to it.

In xai-org/x-algorithm, phoenix/xrex/cutedsl/ranker_fa4/tile_scheduler.py defines five different nested Params classes. A focused extraction produced one ...tile_scheduler_params node, sourced to the first definition, with contains edges from multiple enclosing scheduler classes. A consumer that requires one lexical parent per symbol cannot process this graph; a consumer that accepts it may attribute definitions and references to the wrong class.

Source example: https://github.com/xai-org/x-algorithm/blob/78460ca8b65c57ddd3a05f9217c8aaeba214b628/phoenix/xrex/cutedsl/ranker\_fa4/tile\_scheduler.py

The likely cause is that the class ID is built from the file stem, namespace stack, and class name, but omits the enclosing class identity. The contains edge does use the enclosing class identity. The same ID construction remains in the v0.9.80 source; I reproduced the behavior at runtime on 0.9.77, not 0.9.80.

Steps to reproduce

from pathlib import Path
from tempfile import TemporaryDirectory
from graphify.extract import extract

with TemporaryDirectory() as tmp:
    root = Path(tmp)
    source = root / "scheduler.py"
    source.write_text(
        "class Forward:\n"
        "    class Params:\n"
        "        pass\n"
        "\n"
        "class Backward:\n"
        "    class Params:\n"
        "        pass\n"
    )
    result = extract([source], root=root, cache_root=root / "cache", parallel=False)
    print([(n["id"], n.get("source_location"))
           for n in result["nodes"] if n.get("label") == "Params"])
    print([(e["source"], e["target"])
           for e in result["edges"]
           if e.get("relation") == "contains" and e.get("target") == "scheduler_params"])

Error output or graph output

Actual output:


[('scheduler_params', 'L2')]
[('scheduler_forward', 'scheduler_params'),
 ('scheduler_backward', 'scheduler_params')]


Expected: two distinct `Params` nodes, each with its own source location and exactly one enclosing class. References to either nested class should resolve to the corresponding node.

Graphify version

0.9.77

Operating System

macOS

Python Version

3.13.7

Installation Method

uv tool install (recommended)

Additional Environment Details

No response

Additional context

No response

Activity

  1. added
    bug
    on Oct 8, 2026
  2. github-actions commented on Oct 8, 2026

    @github-actions

    Thanks for opening this issue, @john-ramsey. A maintainer will take a look soon.

    If you would like to discuss it in real time, come say hi on our Discord server. For longer-form questions and ideas there is also GitHub Discussions.

    To help us triage, please make sure the report includes what you expected, what actually happened, and the steps (and a small sample) to reproduce it.

  3. safishamsi commented on Oct 10, 2026

    @safishamsi
    Member

    Already fixed on v8 by #4228 (nested class identities are qualified by their enclosing class). Closing as resolved.

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions