* 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)
| 1486 | * resolves `Client` in the touched file. |
| 1487 | */ |
| 1488 | pythonAttrRetryCandidates(touched: string[], ceiling: number = 2000): UnresolvedReference[] | null { |
| 1489 | const touchedSet = new Set(touched); |
| 1490 | const closure = this.queries.getPythonImporterClosure(touched).filter((f) => !touchedSet.has(f)); |
| 1491 | // One answer per (calling symbol, name): the same call written twice in |
| 1492 | // one function resolves the same way, so it is asked — and counted — once. |
| 1493 | const keyOf = (ref: { fromNodeId: string; referenceName: string }) => `${ref.fromNodeId}\0${ref.referenceName}`; |
| 1494 | const resolvable = (refs: UnresolvedReference[]) => { |
| 1495 | const answers = new Map<string, boolean>(); |
| 1496 | return refs.filter((ref) => { |
| 1497 | if (!ref.filePath || !ref.language) return false; |
| 1498 | const key = keyOf(ref); |
| 1499 | let ok = answers.get(key); |
| 1500 | if (ok === undefined) answers.set(key, (ok = pythonAttrRefResolves(ref as UnresolvedRef, this.context))); |
| 1501 | return ok; |
| 1502 | }); |
| 1503 | }; |
| 1504 | const candidates = this.queries.getFailedPythonAttrRefKeys(closure); |
| 1505 | const load = (keys: typeof candidates) => this.queries.getUnresolvedReferencesByIds(keys.map((k) => k.id)); |
| 1506 | if (new Set(candidates.map(keyOf)).size <= ceiling) return resolvable(load(candidates)); |
| 1507 | |
| 1508 | const assigned = new Set<string>(); |
| 1509 | const defined = new Set<string>(); |
| 1510 | const subclassing = new Set<string>(); |
| 1511 | const calling = new Set<string>(); |
| 1512 | for (const file of touched) { |
| 1513 | const source = this.context.readFile(file); |
| 1514 | if (source) { |
| 1515 | const text = stripCommentsForRegex(source, 'python'); |
| 1516 | for (const m of text.matchAll(/\b(?:self|cls)\.(\w+)\s*(?::[^=\n]*)?=(?!=)|^[ \t]+(\w+)\s*(?::[^=\n]*)?=(?!=)/gm)) { |
| 1517 | assigned.add((m[1] ?? m[2])!); |
| 1518 | } |
| 1519 | } |
| 1520 | for (const n of this.context.getNodesInFile(file)) { |
| 1521 | if (n.kind === 'class' || n.kind === 'function' || n.kind === 'method') defined.add(n.name); |
| 1522 | } |
| 1523 | for (const f of this.queries.getPythonFilesWithEdgesInto(file, ['extends'], ['class'])) subclassing.add(f); |
| 1524 | for (const f of this.queries.getPythonFilesWithEdgesInto(file, ['calls', 'instantiates'], ['function', 'method', 'class'])) { |
| 1525 | calling.add(f); |
| 1526 | } |
| 1527 | } |
| 1528 | |
| 1529 | const callees = new Map<string, Map<string, Set<string>>>(); // file → attr → callee names |
| 1530 | const assignedCallees = (file: string, attr: string): Set<string> => { |
| 1531 | let byAttr = callees.get(file); |
| 1532 | if (!byAttr) { |
| 1533 | byAttr = new Map(); |
| 1534 | const source = this.context.readFile(file); |
| 1535 | const text = source ? stripCommentsForRegex(source, 'python') : ''; |
| 1536 | for (const m of text.matchAll(/\b(\w+)\s*(?::[^=\n]*)?=(?!=)\s*(?:await\s+)?(?:[\w.]*\.)?(\w+)\s*\(/g)) { |
| 1537 | let set = byAttr.get(m[1]!); |
| 1538 | if (!set) byAttr.set(m[1]!, (set = new Set())); |
| 1539 | set.add(m[2]!); |
| 1540 | } |
| 1541 | callees.set(file, byAttr); |
| 1542 | } |
| 1543 | return byAttr.get(attr) ?? new Set(); |
| 1544 | }; |
| 1545 |
no test coverage detected