Skip to content

Python: a closure variable passed as an argument fabricates indirect_call to a same-named nested function in another module #5658

Backlog

Description

@md01750272449-oss

Summary

When a nested Python function passes a variable captured from its enclosing function (a free/closure variable) as a call argument, the callback-by-name resolver treats the identifier as a function reference. It then binds it corpus-wide to any same-named function, including a function nested inside another function in a different module. That nested function cannot be reached from the caller at all.

A plain parameter or local variable of the caller is correctly shadowed: no edge is emitted. Only the enclosing-scope (closure) case leaks.

Minimal reproduction

pkg/cli.py

def placeholder(graph):
    def source(record):
        return record.get("source_file", "")
    return [n for n in graph if source(n)]

pkg/walker.py

def read_text(node, source):
    return source[node.start:node.end]


def extract_file(path):
    source = path.read_bytes()

    def walk(node):
        return read_text(node, source)   # `source` is bytes captured from extract_file

    return walk
from pathlib import Path
from graphify.extract import extract
root = Path("repro")
r = extract(sorted(root.rglob("*.py")), cache_root=root, root=root)

Actual (calls / indirect_call edges):

placeholder()  -- calls         EXTRACTED      -> pkg_cli_placeholder_source        pkg/cli.py L4
extract_file() -- indirect_call INFERRED 0.85  -> pkg_walker_extract_file_walk      pkg/walker.py L11
walk()         -- calls         EXTRACTED      -> pkg_walker_read_text              pkg/walker.py L9
walk()         -- indirect_call INFERRED 0.85  -> pkg_cli_placeholder_source        pkg/walker.py L9   <-- wrong

Expected: no edge from walk() to pkg_cli_placeholder_source. Here source is a bytes value bound in extract_file's scope. It is not a reference to cli.placeholder.<locals>.source, which is not visible from walker.py in the first place.

If source is instead a parameter of walk itself (def walk(node, source)), no false edge is produced. So parameters of the caller are shadowed, but names bound in enclosing function scopes are not.

Impact on a real corpus

Running /graphify on graphify's own graphify/ package (v0.9.124, commit d249de3) shows the problem at scale. The nested helper source() at cli.py:657 (inside _placeholder_stale_sources) becomes the #1 god node with 125 edges, 124 of them fabricated indirect_call INFERRED 0.85. They come from the language extractors, where source is the file-bytes variable captured by nested walk() / walk_calls() closures:

  • extractors/engine.py (51 edges), go.py (8), julia.py (7), r.py, rust.py, solidity.py (6 each), fortran.py, powershell.py (5 each), …
  • e.g. extractors/engine.py:9574: config.import_handler(node, source, file_nid, ...), or extractors/bash.py:844: _read_text(c, source).

The side effects:

  • source() tops "God Nodes" and walk() (v3: Codex and OpenClaw support #2) is inflated.
  • One Leiden community ("scoped walk / export macros") is held together mostly by the phantom hub.
  • The report then suggests verifying "the 124 inferred relationships involving source()".

Likely cause

The Python callback-by-name path (_emit_indirect_by_name → the cross-file resolver in extract.py, which emits "relation": "indirect_call" around L14700) resolves the bare identifier by label across the corpus. It only checks that the target is a callable, non-class definition with a source file. It does not check that:

  1. the identifier is a name bound in any enclosing function scope of the caller (assignment, parameter, for target, etc.), which would shadow any module-level or cross-file definition (LEGB); or
  2. the target is reachable by name from the caller's module. A function nested inside another function (a.<locals>.b) can never be referenced by bare name from a different module.

Either check alone would remove all 124 false edges above.

Related

Environment

  • graphifyy installed via uv tool install graphifyy (Python 3.12, macOS)
  • Repo at d249de3 (2026-11-30), pyproject.toml version 0.9.124

Activity

  1. github-actions commented on Nov 30, 2026

    @github-actions

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

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