Skip to content

[Bug]: Python: from-importing a function that has a nested def makes every module.func() call from that file lose its calls edge (0.9.58+) #3887

Closed

Description

@wpoPR

What happened?

Since 0.9.58, if a file does from pkg import mod and also from pkg.mod import f, where f is a function
that contains a nested def, every mod.<anything>() call in that file loses its calls edge. 0.9.57
resolves them. Still reproduces on 0.9.70 and on v8@4c21b15e9b.

Root cause (found by instrumenting the resolver; the intermediate edge does not survive to graph.json):
since #3410 an outer function with a nested def is the source of a contains edge. The module arm of
_resolve_python_member_calls treats any imports target that has contains children as a candidate module:

mods = [t for t in imported_by_filenode.get(caller_file, ())
        if t in contains_children
        and (_module_stem_key(t) == rkey or file_aliases.get(t) == rkey)]
if len(mods) != 1:  # not an imported module, or ambiguous -> bail
    continue

The imports views -> sort_todos edge makes sort_todos a candidate, and _module_stem_key takes the stem
of its file (store), which matches the receiver. So mods == [store.py, sort_todos], and the resolver
bails as ambiguous.

Steps to reproduce

mkdir -p repro/app/api repro/app/services && cd repro
touch app/api/__init__.py app/services/__init__.py
cat > app/services/store.py <<'EOF'
def sort_todos(todos):
    def key(t):
        return t
    return sorted(todos, key=key)


def parse_todo_file(path):
    return path
EOF
cat > app/api/views.py <<'EOF'
from services import store
from services.store import sort_todos


def get_archived_panel():
    return store.parse_todo_file(1)


def board():
    return sort_todos([])
EOF
git init -q . && graphify update .

Error output or graph output

calls edges from app/api/views.py
0.9.57:  board() -> sort_todos(); get_archived_panel() -> parse_todo_file()
0.9.58, 0.9.62, 0.9.66, 0.9.70:  board() -> sort_todos()        <- parse_todo_file() lost
controls: without the nested `def key` in sort_todos -> both edges on every version

Suggested fix (one line, verified): a node that is itself contained by another node is not a module:

             mods = [t for t in imported_by_filenode.get(caller_file, ())
                     if t in contains_children
+                    and t not in file_of_node
                     and (_module_stem_key(t) == rkey or file_aliases.get(t) == rkey)]

On a 485-file Python subtree of our repo this line alone restores exactly the 8 lost mod.func() edges
and adds or removes nothing else (full graph diff, 0.9.70 vs 0.9.70 + this line: +8 calls, −0). On the
whole ~2,470-file repo, combined with the fix from the companion issue, the 8 are recovered and no edge is
removed.

Version: 0.9.58 → 0.9.70 (last good: 0.9.57) · OS: macOS · Python: 3.11 · Install: uv
(uv venv + uv pip install graphifyy==<v>; reproduced on a clean fixture, no LLM backend)

Additional context: related but different: #2447 (other causes in the same arm), #2428 / PR #2429 (alias
stem; the un-aliased repro above is not covered by #2429), #3272 (package-form import outside the package tree).

Activity

  1. github-actions commented on Sep 28, 2026

    @github-actions

    Thanks for opening this issue, @wpoPR. 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.

  2. harshaygadekar commented on Sep 28, 2026

    @harshaygadekar
    Contributor

    Confirmed and reproduced.

    Root Cause

    In graphify/extract.py within _resolve_python_member_calls:

    mods = [t for t in imported_by_filenode.get(caller_file, ())
            if t in contains_children
            and (_module_stem_key(t) == rkey or file_aliases.get(t) == rkey)]
    if len(mods) != 1:  # not an imported module, or ambiguous -> bail
        continue

    When sort_todos defines an inner def key(t):, PR #3410 added a contains edge from sort_todos to key. As a result:

    • sort_todos in contains_children evaluates to True.
    • _module_stem_key(sort_todos) evaluates to Path(node["source_file"]).stem, which is "store".
    • mods contains both the module node (services/store.py) and the function node (sort_todos).
    • Because len(mods) == 2 != 1, the ambiguity guard triggers and skips resolving any store.<func>() call from that file.

    Minimal Fix

    Since symbol nodes (functions, classes, nested functions) are always the target of a contains edge, they are recorded in the existing file_of_node map (file_of_node[tgt] = src), while module/file nodes are never the target of contains.

    Adding and t not in file_of_node to the candidate filter ensures only top-level module nodes match:

    --- a/graphify/extract.py
    +++ b/graphify/extract.py
    @@ -3972,6 +3972,7 @@ def _resolve_python_member_calls(
                 file_aliases = import_alias_by_filenode.get(caller_file, {})
                 mods = [t for t in imported_by_filenode.get(caller_file, ())
                         if t in contains_children
    +                    and t not in file_of_node
                         and (_module_stem_key(t) == rkey or file_aliases.get(t) == rkey)]
                 if len(mods) != 1:  # not an imported module, or ambiguous -> bail
                     continue

    I have a branch with this 1-line fix and a regression test ready. Opening a PR shortly.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions