MCPcopy Create free account
hub / github.com/Graphify-Labs/graphify / resolve_interface_dispatch

Function resolve_interface_dispatch

graphify/interface_dispatch.py:94–199  ·  view source on GitHub ↗

Link each interface method to every implementation that owns it unambiguously. Purely additive: the existing call edge to the interface method is left in place, since the call site really does name the interface. ``suffixes`` is the language gate: the interface and each implementer must

(
    per_file: list[dict],
    all_nodes: list[dict],
    all_edges: list[dict],
    *,
    suffixes: tuple[str, ...],
    match_arity: bool = False,
)

Source from the content-addressed store, hash-verified

92
93
94def resolve_interface_dispatch(
95 per_file: list[dict],
96 all_nodes: list[dict],
97 all_edges: list[dict],
98 *,
99 suffixes: tuple[str, ...],
100 match_arity: bool = False,
101) -> None:
102 """Link each interface method to every implementation that owns it unambiguously.
103
104 Purely additive: the existing call edge to the interface method is left in
105 place, since the call site really does name the interface. ``suffixes`` is
106 the language gate: the interface and each implementer must both be
107 declared in a file with one of these extensions. ``match_arity`` makes a
108 lone interface method and a lone implementer method of one name pair only
109 when both record the same parameter count.
110 """
111 if not any(
112 declared_in(result, suffixes)
113 for result in per_file
114 if isinstance(result, dict)
115 ) and not any(
116 declared_in(n, suffixes) for n in all_nodes
117 ):
118 return
119
120 node_by_id = {n.get("id"): n for n in all_nodes}
121
122 implementers: dict[str, set[str]] = {}
123 methods_of: dict[str, dict[str, set[str]]] = {}
124 for e in all_edges:
125 rel = e.get("relation")
126 if rel == "implements":
127 implementers.setdefault(e.get("target"), set()).add(e.get("source"))
128 elif rel == "method":
129 owner, method_nid = e.get("source"), e.get("target")
130 mnode = node_by_id.get(method_nid)
131 if mnode is None or not declared_in(mnode, suffixes):
132 continue
133 # A constructor shares its type's name but implements nothing.
134 if (mnode.get("metadata") or {}).get("kind") == "constructor":
135 continue
136 name = method_label(mnode)
137 if name:
138 # A set, so the same method arriving on two `method` edges cannot
139 # look like two same-named methods and trip the guard below.
140 methods_of.setdefault(owner, {}).setdefault(name, set()).add(method_nid)
141
142 if not implementers or not methods_of:
143 return
144
145 # Scoped to this relation: another edge between the two members, whatever it
146 # is, says nothing about whether the dispatch link is already there.
147 existing_pairs = {
148 (e.get("source"), e.get("target"))
149 for e in all_edges
150 if e.get("relation") == DISPATCH_RELATION
151 }

Calls 6

declared_inFunction · 0.85
method_labelFunction · 0.85
_dispatch_confidenceFunction · 0.85
_implementing_pairsFunction · 0.85
getMethod · 0.45
itemsMethod · 0.45

Tested by

no test coverage detected