Skip to content

Python: @receiver.name(...) decorators bind to an unrelated module-level function of the same bare name — false edges create a god node (regression from #2154, 0.9.27+) #2315

Open

Description

@dispather

Summary

Python decorators of the form @receiver.name(...) are resolved by their bare attribute name only — the receiver is dropped. If any module-level function in the corpus happens to share that name, every decorated function gets a false references (and at scale, calls) edge to it, across files that never import it.

The common real-world trigger is web frameworks: @app.get(...), @router.get(...), @app.post(...). Any project that also defines a helper named get/post gets a fabricated hub.

This appears to come from decorator edges added in #2154 (v0.9.27).

Minimal reproduction

Two files, no import between them:

# settings_store.py — project-defined module-level function named `get`
def get(key: str):
    return key
# routes.py — deliberately does NOT import settings_store
class _App:
    def get(self, path):
        def deco(fn):
            return fn
        return deco

app = _App()

@app.get("/health")
def health():
    return "ok"

@app.get("/status")
def status():
    return "ok"
graphify . --no-cluster

Actual — graph.json contains:

references  health() (routes.py)  -->  get() (settings_store.py)
references  status() (routes.py)  -->  get() (settings_store.py)

Expected — no cross-file edge. app is a local _App instance; settings_store.get is unrelated and never imported.

Note the resolver also skips the correct candidate: _App.get is defined in the same file (it appears in the graph as a .get() node) and is the actual receiver's method, but the edge goes to the unrelated module-level function instead.

Control experiment (isolates the cause)

Rename only the project-defined function, def get -> def get_knob, leaving the @app.get decorators untouched:

references  health()  -->  get   [stub node, source_file empty]
references  status()  -->  get   [stub node, source_file empty]

The edges now land on an unresolved stub — correct behaviour. So the binding keys purely on the bare decorator attribute name matching a module-level definition; nothing about the receiver or the import graph is consulted.

Real-world impact

On a FastAPI codebase (197 Python files, 3.3k nodes) upgrading 0.9.26 -> 0.9.30:

  • settings_store.get() degree: 27 -> 149, becoming the v3: semantic query with embeddings #1 god node in GRAPH_REPORT.md, above genuine core abstractions.
  • 122 of the 200 newly added edges (61%) were this artifact (references 67 + calls 55), all confidence: EXTRACTED.
  • Sources correlate 1:1 with route decorators, not with call sites: api.py has 12 @app.get decorators, zero dict.get( calls, and exactly 12 false edges; api_infra.py 6/0/6. Files with both produce ~2 edges per decorator.
  • Query impact: the node bridges 16 otherwise-unconnected files, so depth-2 BFS context from any route handler is ~71% nodes pulled in through this one hub; blast radius (nodes with it within 2 hops) went 6.7% -> 15.7% of the graph.

Suggested direction

For @receiver.attr(...) decorators, either resolve attr against the receiver's type when known, or leave it unresolved (stub) rather than falling back to a bare-name match against module-level definitions. That matches how the rename control already behaves.

This is the same class as #2241 (call arguments binding to an unrelated same-named symbol), which was fixed for JS/TS closure parameters; the Python decorator path still has it. Possibly related: #2041 (bare-vs-dotted call resolution via label-text match), #2313 (god-node guards).

Environment

  • graphify 0.9.30 (also reproduces the artifact on a 0.9.30 clean rebuild)
  • Python 3.13.12, Linux x86_64
  • graphify . --no-cluster, AST-only (no LLM)

Activity

  1. Rishet11 commented on Jul 31, 2026

    @Rishet11
    Contributor

    Reproduced exactly as reported, on v8 (0.9.31). Two files, routes.py with a local _App instance and settings_store.py with an unrelated module-level def get:

    EDGE references routes_health -> settings_store_get  context=decorator
    EDGE references routes_status -> settings_store_get  context=decorator
    NODE routes_app_get      | .get() | routes.py          <- correct receiver method, never referenced
    NODE settings_store_get  | get()  | settings_store.py
    

    So the edge is wrong, not merely missing, which is the part that matters: it points at a real unrelated node in another file.

    Root cause is a two-step drop of the receiver:

    1. _python_decorator_name (graphify/extractors/engine.py:4725, dotted/call branch at :4739-4741) returns only the last attribute segment — "get" — and discards app entirely.
    2. The emission site (engine.py:3784) then calls ensure_named_node("get", line). In ensure_named_node (engine.py:2393-2415) the same-file scoped id _make_id(stem, ns, name) cannot match a plain module-level function id, so it falls through to _make_id(name) — a bare global id with no file or class scoping. Any node already in seen_ids under that bare name wins silently, wherever it came from.

    The receiver expression and the import graph are never consulted, so this misfires for every @app.get(...), @router.post(...), @receiver.connect(...) shape whenever the corpus happens to contain a module-level function of the same bare name — which for names like get, post, run, send is common.

    Worth noting for scope: test_attribute_decorator_targets_the_symbol_not_the_module (tests/test_python_decorators.py:94) deliberately locks taking the bare attribute name when the receiver is an imported symbol, and that behavior is fine there (no collision, it becomes a sourceless stub the rewire pass can collapse). The uncovered case is a receiver whose attribute name collides with an unrelated module-level def. Any fix has to keep the imported-receiver case working, so I do not intend to change the stub path — only to stop the bare-name fallback from binding an attribute-form decorator to an unrelated real definition.

    I wrote the original Python decorator path (#2154 / 794cf9e), so this is mine to clean up. Claiming it; PR against v8.

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