Skip to content

Python: module.attr() calls unresolved, and nested functions resolved to unrelated files as EXTRACTED/1.0 #3405

Open

Description

@jerryjohn1702

Two Python call-resolution defects, found while indexing a 602-file polyglot monorepo with v0.9.56. They are separate bugs but share a root cause: call targets appear to be matched on bare name against a global symbol table, without consulting the importing module's namespace or the enclosing lexical scope.

The second is the more serious. It produces wrong edges labelled EXTRACTED with confidence_score: 1.0, which is worse than a missing edge, because nothing downstream can distinguish it from a real one.

Corpus: 602 code files → 3477 nodes, 6221 edges. graphify extract . --code-only (no LLM), Python 3.14, Windows. Paths below are anonymised; the shapes are verbatim.


1. import module + module.func() calls are never resolved

from mod import func → func() resolves correctly. import mod → mod.func() produces no edge at all.

# pkg_a/collect.py
def q(conn, sql): ...      # returns tuples
def qd(conn, sql): ...     # returns dicts

# pkg_a/store.py
import collect
rows = collect.qd(conn, sql)          # line 119

# pkg_a/waker.py
import collect
rows = collect.q(conn, SIGNALS_SQL)   # line 103

graphify explain on qd() reports degree 2 — its contains edge and a docstring rationale_for edge. The call from store.py is absent. q() shows only its one same-file caller and misses both cross-module ones.

This is not a general cross-file failure: 331 of 2476 calls edges (13%) are cross-file and correct. The split is exactly import style. In this corpus one package uses from x import y throughout and its call graph is good; a second uses import x + x.y() and its cross-module edges are essentially all missing.

Why it is easy to miss: "what calls this?" is the question the graph exists to answer, and a package where resolution fails returns nothing rather than an error. A function with no callers looks like a leaf, not like a gap.

2. Nested functions resolve to same-named module-level functions in unrelated files

This one manufactures edges that do not exist.

# pkg_b/probe.py
def trace(...):
    def walk(n, d=0):     # line 53 — nested, local to trace()
        ...
    walk(root)

There is exactly one module-level walk in the corpus, in an unrelated tree:

# pkg_c/overlay.py
def walk(node, path=""):   # line 74

GRAPH_REPORT.md promotes the collision into the "Surprising Connections (you probably didn't know these)" section:

- `trace()` --calls--> `walk()`  [INFERRED]
  pkg_b/probe.py → pkg_c/overlay.py

The two files share no import, no package and no call path. The nested walk shadows everything else; the local definition should win unconditionally.

A second entry in the same section is worse, because it is not even marked inferred:

- `parse_date()` --references--> `_date()`  [EXTRACTED]
  pkg_d/deadlines.py → pkg_b/grid.py

pkg_d/deadlines.py imports only stdlib, requests, and one local config module. _date is defined only in pkg_b/grid.py. There is no relationship of any kind. The edge carries confidence: EXTRACTED, confidence_score: 1.0.

Of the five entries in "Surprising Connections" on this corpus, we could not verify a single one as real. Because that section is framed as insight rather than as a guess, name collisions in a polyglot monorepo surface there as confident discoveries — and short generic names (walk, main, trace, diff, esc, _date) are exactly the ones that collide.


Suggested fixes

  1. Resolve module.attr() through the import table. The imports edges are already correct and 100% cross-file, so the alias→module binding is present — it just is not consulted when resolving a dotted call.
  2. Scope-check before falling back to global name matching. If a name is bound by a def/lambda/assignment in an enclosing scope, resolve there and stop.
  3. Never emit a cross-file edge as EXTRACTED / 1.0 on a bare name match alone. If resolution came from a global name table with no import path connecting the two files, it is by definition inferred, and low confidence. Gating "Surprising Connections" on the existence of an import path between the two files would have removed all five false positives here.

Reproduces every time on this corpus; happy to test a patch.

Activity

  1. rohit-jsfreaky commented on Sep 16, 2026

    @rohit-jsfreaky
    Contributor

    Tested both halves on v8 (0.9.63) with graphify extract <dir> --code-only.

    Bug 2 no longer reproduces — a nested walk() inside trace(), with one unrelated module-level walk() elsewhere, resolves to the local nested definition. Looks like #3410 covered it.

    Bug 1 only reproduces when the importing file's directory has an __init__.py:

    shape call edge
    import collect — sibling, no __init__.py resolves
    from . import collect resolves
    from pkg_a import collect resolves
    import pkg_a.collect as col resolves
    import collect — sibling, with __init__.py no edge

    The failing shape is an implicit relative import, which Python 3 removed, and _repoint_python_sibling_imports skips package directories deliberately:

    # PEP 328 guard: if the directory is a package, implicit relative imports
    # are forbidden in Python 3. Do not index packages as loose sibling directories.
    

    That function landed with #3430, after this was filed, and its docstring names the same symptom ("causing downstream member calls (greeter.greet()) to be dropped"). So the valid-Python side of bug 1 looks covered on current v8.

    Worth re-checking your corpus against 0.9.63 — does the directory holding collect.py and store.py have an __init__.py? If it does, import collect there is an implicit relative import and would not run under Python 3 either, which would make the missing edge correct rather than a gap.

  2. omsenjalia commented on Oct 18, 2026

    @omsenjalia
    Contributor

    This looks fixed on the v8 branch by c967797f (Fix Python nested function resolution). Could a maintainer confirm and close?

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