MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / pythonAttrRetryCandidates

Method pythonAttrRetryCandidates

src/resolution/index.ts:1638–1707  ·  view source on GitHub ↗

* The failed python attribute-chain refs that an edit to `touched` makes * resolvable now — what a sync retries after a factory or a base class * changed in another file (`pythonAttrRefResolves` answers each one). * * The candidates are the refs in the files that import a touched file

(touched: string[], ceiling: number = 2000)

Source from the content-addressed store, hash-verified

1636 * resolves `Client` in the touched file.
1637 */
1638 pythonAttrRetryCandidates(touched: string[], ceiling: number = 2000): UnresolvedReference[] | null {
1639 const touchedSet = new Set(touched);
1640 const closure = this.queries.getPythonImporterClosure(touched).filter((f) => !touchedSet.has(f));
1641 // One answer per (calling symbol, name): the same call written twice in
1642 // one function resolves the same way, so it is asked — and counted — once.
1643 const keyOf = (ref: { fromNodeId: string; referenceName: string }) => `${ref.fromNodeId}\0${ref.referenceName}`;
1644 const resolvable = (refs: UnresolvedReference[]) => {
1645 const answers = new Map<string, boolean>();
1646 return refs.filter((ref) => {
1647 if (!ref.filePath || !ref.language) return false;
1648 const key = keyOf(ref);
1649 let ok = answers.get(key);
1650 if (ok === undefined) answers.set(key, (ok = pythonAttrRefResolves(ref as UnresolvedRef, this.context)));
1651 return ok;
1652 });
1653 };
1654 const candidates = this.queries.getFailedPythonAttrRefKeys(closure);
1655 const load = (keys: typeof candidates) => this.queries.getUnresolvedReferencesByIds(keys.map((k) => k.id));
1656 if (new Set(candidates.map(keyOf)).size <= ceiling) return resolvable(load(candidates));
1657
1658 const assigned = new Set<string>();
1659 const defined = new Set<string>();
1660 const subclassing = new Set<string>();
1661 const calling = new Set<string>();
1662 for (const file of touched) {
1663 const source = this.context.readFile(file);
1664 if (source) {
1665 const text = stripCommentsForRegex(source, 'python');
1666 for (const m of text.matchAll(/\b(?:self|cls)\.(\w+)\s*(?::[^=\n]*)?=(?!=)|^[ \t]+(\w+)\s*(?::[^=\n]*)?=(?!=)/gm)) {
1667 assigned.add((m[1] ?? m[2])!);
1668 }
1669 }
1670 for (const n of this.context.getNodesInFile(file)) {
1671 if (n.kind === 'class' || n.kind === 'function' || n.kind === 'method') defined.add(n.name);
1672 }
1673 for (const f of this.queries.getPythonFilesWithEdgesInto(file, ['extends'], ['class'])) subclassing.add(f);
1674 for (const f of this.queries.getPythonFilesWithEdgesInto(file, ['calls', 'instantiates'], ['function', 'method', 'class'])) {
1675 calling.add(f);
1676 }
1677 }
1678
1679 const callees = new Map<string, Map<string, Set<string>>>(); // file → attr → callee names
1680 const assignedCallees = (file: string, attr: string): Set<string> => {
1681 let byAttr = callees.get(file);
1682 if (!byAttr) {
1683 byAttr = new Map();
1684 const source = this.context.readFile(file);
1685 const text = source ? stripCommentsForRegex(source, 'python') : '';
1686 for (const m of text.matchAll(/\b(\w+)\s*(?::[^=\n]*)?=(?!=)\s*(?:await\s+)?(?:[\w.]*\.)?(\w+)\s*\(/g)) {
1687 let set = byAttr.get(m[1]!);
1688 if (!set) byAttr.set(m[1]!, (set = new Set()));
1689 set.add(m[2]!);
1690 }
1691 callees.set(file, byAttr);
1692 }
1693 return byAttr.get(attr) ?? new Set();
1694 };
1695

Callers 1

syncMethod · 0.80

Calls 10

stripCommentsForRegexFunction · 0.90
hasMethod · 0.80
mapMethod · 0.80
readFileMethod · 0.65
getNodesInFileMethod · 0.65
loadFunction · 0.50
addMethod · 0.45

Tested by

no test coverage detected