Slice 1 of #1566. A function referenced as a VALUE in a dict/list/set/ tuple literal — the registry/route-table idiom (`ROUTES = {"create": create_user}`, `HOOKS = [on_start, on_stop]`) — is an indirect dependency that blast-radius must see, but the call-argument capture (#1565) didn't cover it. Emit an indirect_call edge for each callable value: - module-level tables attribute to the file node; function-scoped tables attribute to the enclosing function. Same-file and cross-file (an imported handler in a table routes through the cross-file resolver). - dict KEYS are excluded (only values are references); non-callable values (a number, a string) never resolve; a name shadowed by a param/local or rebound at module scope is the local value, not the function. Refactors the resolve-and-emit logic shared by the argument and table paths into one guarded helper (_emit_python_indirect_ref) and threads the edge `context` ("argument" | "collection") through the cross-file pass. Adds _python_module_bound_names for the module-scope shadow set. 7 new tests (module dict/list, function-scoped, dict-keys-excluded, non-callable-value, module-reassign shadow, cross-file table). Full suite 2717 passed. Python only; assignment/return refs + other languages remain on #1566. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
350 lines
12 KiB
Python
350 lines
12 KiB
Python
"""Indirect dispatch edges.
|
|
|
|
A function passed BY NAME as a call argument (`executor.submit(fn)`, `Thread(target=fn)`) is a
|
|
real dependency, but the callee-only call scan never recorded it — so `affected` (blast radius)
|
|
dropped those callers. These tests pin that such calls now emit a distinct `indirect_call` edge
|
|
(leaving the precise `calls` relation untouched) and that `affected` picks them up.
|
|
|
|
They also pin the soundness guards: an argument that is a PARAMETER or a LOCAL binding of the
|
|
enclosing function is a local value, not the module-level function it shares a name with, and a
|
|
non-callable same-named node is never a dispatch target — neither may manufacture an edge.
|
|
"""
|
|
import os
|
|
from pathlib import Path
|
|
|
|
import networkx as nx
|
|
|
|
from graphify.affected import affected_nodes
|
|
from graphify.extract import extract, extract_python
|
|
|
|
SRC = '''\
|
|
import threading
|
|
|
|
|
|
def handler(x):
|
|
return x * 2
|
|
|
|
|
|
def direct():
|
|
return handler(1) # direct call -> `calls`
|
|
|
|
|
|
def via_submit(pool):
|
|
pool.submit(handler, 1) # indirect: positional arg
|
|
|
|
|
|
def via_thread():
|
|
threading.Thread(target=handler).start() # indirect: keyword arg
|
|
|
|
|
|
def via_map(xs):
|
|
return map(handler, xs) # indirect: map(fn, xs)
|
|
'''
|
|
|
|
|
|
def _build(tmp_path):
|
|
(tmp_path / "dispatch.py").write_text(SRC)
|
|
r = extract_python(tmp_path / "dispatch.py")
|
|
nid = {n["label"].rstrip("()"): n["id"] for n in r["nodes"]} # labels are "handler()"
|
|
return r, nid
|
|
|
|
|
|
def _rels(r, relation):
|
|
return {(e["source"], e["target"]) for e in r["edges"] if e["relation"] == relation}
|
|
|
|
|
|
def test_emits_indirect_call_edges_and_keeps_calls_precise(tmp_path):
|
|
r, nid = _build(tmp_path)
|
|
calls = _rels(r, "calls")
|
|
indirect = _rels(r, "indirect_call")
|
|
handler = nid["handler"]
|
|
|
|
# the direct caller stays a `calls` edge — precise relation not regressed
|
|
assert (nid["direct"], handler) in calls
|
|
# the indirect callers are captured, and under the DISTINCT relation
|
|
assert (nid["via_submit"], handler) in indirect
|
|
assert (nid["via_thread"], handler) in indirect
|
|
assert (nid["via_map"], handler) in indirect
|
|
# ...and never leak into the strict `calls` relation
|
|
assert (nid["via_submit"], handler) not in calls
|
|
assert (nid["via_thread"], handler) not in calls
|
|
assert (nid["via_map"], handler) not in calls
|
|
|
|
for e in (e for e in r["edges"] if e["relation"] == "indirect_call"):
|
|
assert e["context"] == "argument" and e["confidence"] == "INFERRED"
|
|
|
|
|
|
def test_affected_includes_indirect_callers(tmp_path):
|
|
r, nid = _build(tmp_path)
|
|
g = nx.DiGraph()
|
|
for n in r["nodes"]:
|
|
g.add_node(n["id"], **n)
|
|
for e in r["edges"]:
|
|
g.add_edge(e["source"], e["target"], **e)
|
|
|
|
affected = {h.node_id for h in affected_nodes(g, nid["handler"])}
|
|
# blast radius of `handler` now includes the dispatchers it used to drop
|
|
assert nid["via_submit"] in affected
|
|
assert nid["via_thread"] in affected
|
|
assert nid["via_map"] in affected
|
|
|
|
|
|
# ── Soundness: the guards that kill the PR's false positives ──────────────────
|
|
|
|
def _extract(tmp_path, src):
|
|
(tmp_path / "m.py").write_text(src)
|
|
r = extract_python(tmp_path / "m.py")
|
|
nid = {n["label"].rstrip("()"): n["id"] for n in r["nodes"]}
|
|
return r, nid
|
|
|
|
|
|
PARAM_SHADOW = '''\
|
|
def handler():
|
|
return 1
|
|
|
|
|
|
def via(pool, handler):
|
|
pool.submit(handler) # `handler` is a PARAMETER, not the module fn
|
|
'''
|
|
|
|
|
|
def test_param_shadow_emits_no_indirect_call(tmp_path):
|
|
r, nid = _extract(tmp_path, PARAM_SHADOW)
|
|
indirect = _rels(r, "indirect_call")
|
|
assert (nid["via"], nid["handler"]) not in indirect
|
|
# nothing else snuck an edge in to the module-level handler either
|
|
assert all(t != nid["handler"] for _s, t in indirect)
|
|
|
|
|
|
LOCAL_SHADOW = '''\
|
|
def handler():
|
|
return 1
|
|
|
|
|
|
def make():
|
|
return lambda: None
|
|
|
|
|
|
def via(pool):
|
|
handler = make() # `handler` is a LOCAL binding, not the module fn
|
|
pool.submit(handler)
|
|
'''
|
|
|
|
|
|
def test_local_assignment_shadow_emits_no_indirect_call(tmp_path):
|
|
r, nid = _extract(tmp_path, LOCAL_SHADOW)
|
|
indirect = _rels(r, "indirect_call")
|
|
assert (nid["via"], nid["handler"]) not in indirect
|
|
assert all(t != nid["handler"] for _s, t in indirect)
|
|
|
|
|
|
DATA_VAR = '''\
|
|
def config():
|
|
return {"k": "v"}
|
|
|
|
|
|
def process(x):
|
|
return x
|
|
|
|
|
|
def use():
|
|
config = {"k": "v"} # local DATA var that happens to match `config()`
|
|
process(config) # arg resolves to a non-callable local, not the fn
|
|
'''
|
|
|
|
|
|
def test_data_var_matching_function_name_emits_no_indirect_call(tmp_path):
|
|
r, nid = _extract(tmp_path, DATA_VAR)
|
|
indirect = _rels(r, "indirect_call")
|
|
# the local `config` must NOT create use -> config()
|
|
assert (nid["use"], nid["config"]) not in indirect
|
|
|
|
|
|
REAL_PASS = '''\
|
|
def handler():
|
|
return 1
|
|
|
|
|
|
def via(pool):
|
|
pool.submit(handler) # genuine module-level fn passed by name
|
|
'''
|
|
|
|
|
|
def test_genuine_module_function_still_emits_indirect_call(tmp_path):
|
|
"""No recall regression: a real module fn passed by name still emits an edge."""
|
|
r, nid = _extract(tmp_path, REAL_PASS)
|
|
indirect = _rels(r, "indirect_call")
|
|
assert (nid["via"], nid["handler"]) in indirect
|
|
|
|
|
|
# ── Cross-file indirect dispatch ──────────────────────────────────────────────
|
|
# The dominant real-world shape: the callback is defined in ANOTHER module and
|
|
# imported. The in-file label map can't see it, so it is deferred to the
|
|
# cross-file resolver, which emits a distinct INFERRED `indirect_call` only when
|
|
# the name resolves to exactly one real callable (the single-definition guard).
|
|
|
|
def _extract_dir(tmp_path, files: dict[str, str]):
|
|
base = tmp_path / "pkg"
|
|
base.mkdir()
|
|
for name, body in files.items():
|
|
(base / name).write_text(body)
|
|
old = os.getcwd()
|
|
try:
|
|
os.chdir(tmp_path)
|
|
r = extract(
|
|
[Path("pkg") / name for name in files],
|
|
cache_root=Path(".cache"), parallel=False,
|
|
)
|
|
finally:
|
|
os.chdir(old)
|
|
nid = {n["label"].rstrip("()"): n["id"] for n in r["nodes"]}
|
|
return r, nid
|
|
|
|
|
|
def test_cross_file_imported_callback_emits_indirect_call(tmp_path):
|
|
r, nid = _extract_dir(tmp_path, {
|
|
"handlers.py": "def on_event(x):\n return x\n",
|
|
"scheduler.py": (
|
|
"from handlers import on_event\n\n\n"
|
|
"def schedule(pool):\n"
|
|
" pool.submit(on_event)\n" # callback imported from another module
|
|
),
|
|
})
|
|
indirect = _rels(r, "indirect_call")
|
|
calls = _rels(r, "calls")
|
|
assert (nid["schedule"], nid["on_event"]) in indirect
|
|
# never leaks into the precise `calls` relation
|
|
assert (nid["schedule"], nid["on_event"]) not in calls
|
|
# and it is INFERRED (a referenced value, not an invocation here)
|
|
for e in r["edges"]:
|
|
if e["relation"] == "indirect_call" and e["target"] == nid["on_event"]:
|
|
assert e["confidence"] == "INFERRED"
|
|
|
|
|
|
def test_cross_file_affected_includes_importing_dispatcher(tmp_path):
|
|
r, nid = _extract_dir(tmp_path, {
|
|
"handlers.py": "def on_event(x):\n return x\n",
|
|
"scheduler.py": (
|
|
"from handlers import on_event\n\n\n"
|
|
"def schedule(pool):\n"
|
|
" pool.submit(on_event)\n"
|
|
),
|
|
})
|
|
g = nx.DiGraph()
|
|
for n in r["nodes"]:
|
|
g.add_node(n["id"], **n)
|
|
for e in r["edges"]:
|
|
g.add_edge(e["source"], e["target"], **e)
|
|
affected = {h.node_id for h in affected_nodes(g, nid["on_event"])}
|
|
# editing on_event now flags the cross-module dispatcher — the gap #1565
|
|
# left open (it only saw same-file callbacks).
|
|
assert nid["schedule"] in affected
|
|
|
|
|
|
def test_cross_file_param_shadow_emits_no_indirect_call(tmp_path):
|
|
"""Soundness carries across files: an imported name shadowed by a parameter
|
|
is the local value, so no cross-file indirect edge is manufactured."""
|
|
r, nid = _extract_dir(tmp_path, {
|
|
"handlers.py": "def on_event(x):\n return x\n",
|
|
"scheduler.py": (
|
|
"from handlers import on_event\n\n\n"
|
|
"def schedule(pool, on_event):\n" # on_event is the PARAMETER here
|
|
" pool.submit(on_event)\n"
|
|
),
|
|
})
|
|
indirect = _rels(r, "indirect_call")
|
|
assert (nid["schedule"], nid["on_event"]) not in indirect
|
|
|
|
|
|
# ── Dispatch tables (#1566 slice 1): functions referenced as collection values ─
|
|
# A function listed as a VALUE in a dict/list/set/tuple literal (a route/handler
|
|
# registry) is an indirect dependency. Same INFERRED relation, same guards:
|
|
# callable-target-only, not shadowed, dict keys excluded.
|
|
|
|
def test_module_level_dict_registry_emits_indirect_call(tmp_path):
|
|
r, nid = _extract(tmp_path, (
|
|
"def create(x):\n return x\n\n\n"
|
|
"def delete(x):\n return x\n\n\n"
|
|
'ROUTES = {"create": create, "delete": delete}\n'
|
|
))
|
|
indirect = _rels(r, "indirect_call")
|
|
file_nid = next(n["id"] for n in r["nodes"] if n["label"] == "m.py")
|
|
# the module references both handlers through the table
|
|
assert (file_nid, nid["create"]) in indirect
|
|
assert (file_nid, nid["delete"]) in indirect
|
|
# not leaked into precise `calls`
|
|
assert (file_nid, nid["create"]) not in _rels(r, "calls")
|
|
|
|
|
|
def test_module_level_list_registry_emits_indirect_call(tmp_path):
|
|
r, nid = _extract(tmp_path, (
|
|
"def on_start():\n pass\n\n\n"
|
|
"def on_stop():\n pass\n\n\n"
|
|
"HOOKS = [on_start, on_stop]\n"
|
|
))
|
|
indirect = _rels(r, "indirect_call")
|
|
file_nid = next(n["id"] for n in r["nodes"] if n["label"] == "m.py")
|
|
assert (file_nid, nid["on_start"]) in indirect
|
|
assert (file_nid, nid["on_stop"]) in indirect
|
|
|
|
|
|
def test_function_scoped_dispatch_table_attributes_to_function(tmp_path):
|
|
r, nid = _extract(tmp_path, (
|
|
"def cb(x):\n return x\n\n\n"
|
|
"def build():\n return {\"k\": cb}\n"
|
|
))
|
|
assert (nid["build"], nid["cb"]) in _rels(r, "indirect_call")
|
|
|
|
|
|
def test_dict_keys_are_not_dispatch_targets(tmp_path):
|
|
"""Only VALUES are references; a function used as a dict KEY is not invoked
|
|
through the table and must not produce an edge."""
|
|
r, nid = _extract(tmp_path, (
|
|
"def keyfn():\n pass\n\n\n"
|
|
"def valfn():\n pass\n\n\n"
|
|
"T = {keyfn: valfn}\n"
|
|
))
|
|
indirect = _rels(r, "indirect_call")
|
|
assert all(t != nid["keyfn"] for _s, t in indirect)
|
|
file_nid = next(n["id"] for n in r["nodes"] if n["label"] == "m.py")
|
|
assert (file_nid, nid["valfn"]) in indirect # the value still resolves
|
|
|
|
|
|
def test_non_callable_collection_value_emits_no_indirect_call(tmp_path):
|
|
"""A data value in the table (a number, a string) is not a callable and must
|
|
never become a dispatch target."""
|
|
r, nid = _extract(tmp_path, (
|
|
"def use():\n pass\n\n\n"
|
|
'CONF = {"timeout": 30, "name": use}\n'
|
|
))
|
|
indirect = _rels(r, "indirect_call")
|
|
file_nid = next(n["id"] for n in r["nodes"] if n["label"] == "m.py")
|
|
# only the genuine callable resolves; the int value contributes nothing
|
|
assert (file_nid, nid["use"]) in indirect
|
|
assert len(indirect) == 1
|
|
|
|
|
|
def test_module_level_reassigned_name_shadows_dispatch_value(tmp_path):
|
|
"""If the name is rebound to data at module scope, the table value is that
|
|
data, not the same-named function — no edge."""
|
|
r, nid = _extract(tmp_path, (
|
|
"def handler():\n pass\n\n\n"
|
|
"handler = object()\n"
|
|
'T = {"h": handler}\n'
|
|
))
|
|
indirect = _rels(r, "indirect_call")
|
|
assert all(t != nid["handler"] for _s, t in indirect)
|
|
|
|
|
|
def test_cross_file_dict_registry_emits_indirect_call(tmp_path):
|
|
r, nid = _extract_dir(tmp_path, {
|
|
"handlers.py": "def on_event(x):\n return x\n",
|
|
"registry.py": (
|
|
"from handlers import on_event\n\n\n"
|
|
'ROUTES = {"event": on_event}\n'
|
|
),
|
|
})
|
|
indirect = _rels(r, "indirect_call")
|
|
reg_file = next(n["id"] for n in r["nodes"] if n["label"] == "registry.py")
|
|
assert (reg_file, nid["on_event"]) in indirect
|