Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
Merge origin/main into fix/py-namespace-reexports-v2 (pick up #2680, #2690, #2695, #2696)
Martin Vogel7b5e680f3369
diff --cc internal/cbm/lsp/py_lsp.cindex b2947d917,6be9ad565..4130b4699--- a/internal/cbm/lsp/py_lsp.c+++ b/internal/cbm/lsp/py_lsp.c@@@ -1273,85 -1229,138 +1273,217 @@@ static const CBMRegisteredFunc *py_lookreturn py_lookup_attribute_depth(ctx, type_qn, member_name, 0);}+static PyModuleMember py_module_member_exact(PyLSPContext *ctx, const char *module_qn,+ const char *name) {+ PyModuleMember m = {0};+ m.func = cbm_registry_lookup_symbol(ctx->registry, module_qn, name);+ if (m.func)+ return m;+ const char *qn = cbm_arena_sprintf(ctx->arena, "%s.%s", module_qn, name);+ m.type = qn ? cbm_registry_lookup_type(ctx->registry, qn) : NULL;+ if (m.type)+ return m;+ /* Re-export: `module.name` is bound by the module's imports (the export+ * table the cross registry materialized once, before its seal), keyed by+ * the module symbol scope (a package without its `.__init__`). */+ size_t scope_len = cbm_fqn_symbol_scope_len(module_qn);+ const char *scope =+ module_qn[scope_len] ? cbm_arena_strndup(ctx->arena, module_qn, scope_len) : module_qn;+ const char *target = scope ? cbm_registry_lookup_export(ctx->registry, scope, name) : NULL;+ if (target) {+ m.func = cbm_registry_lookup_func(ctx->registry, target);+ if (!m.func)+ m.type = cbm_registry_lookup_type(ctx->registry, target);+ }+ return m;+}++/* `prefix.module_qn`, or NULL when module_qn already starts with prefix. */+static const char *py_qualify_module(PyLSPContext *ctx, const char *prefix, size_t prefix_len,+ const char *module_qn) {+ if (prefix_len == 0 ||+ (strncmp(module_qn, prefix, prefix_len) == 0 && module_qn[prefix_len] == '.'))+ return NULL;+ return cbm_arena_sprintf(ctx->arena, "%.*s.%s", (int)prefix_len, prefix, module_qn);+}++/* An in-project `import sibling` / `import pkg` binds the module by its+ * source spelling ("pkg"), while its defs are registered project-qualified.+ * Try the spelling as written, then qualified by the importing file's+ * package, then by the project root (the file module's first segment). */+static PyModuleMember py_module_member(PyLSPContext *ctx, const char *module_qn, const char *name) {+ PyModuleMember none = {0};+ if (!ctx || !ctx->registry || !module_qn || !name)+ return none;+ PyModuleMember m = py_module_member_exact(ctx, module_qn, name);+ if (m.func || m.type || !ctx->file_module_qn)+ return m;+ const char *file_mod = ctx->file_module_qn;+ const char *last_dot = strrchr(file_mod, '.');+ const char *first_dot = strchr(file_mod, '.');+ if (last_dot && last_dot > file_mod) {+ const char *qual =+ py_qualify_module(ctx, file_mod, (size_t)(last_dot - file_mod), module_qn);+ m = qual ? py_module_member_exact(ctx, qual, name) : none;+ if (m.func || m.type)+ return m;+ }+ if (first_dot && first_dot != last_dot) {+ const char *qual =+ py_qualify_module(ctx, file_mod, (size_t)(first_dot - file_mod), module_qn);+ m = qual ? py_module_member_exact(ctx, qual, name) : none;+ }+ return m;+}++/* Emit a call resolved through a module namespace. A re-export may bind the+ * definition under another name (`from .x import Orig as Renamed`): that is+ * an import alias, whose join reads the written name from `reason`. */+static void py_emit_module_member_call(PyLSPContext *ctx, const char *callee_qn,+ const char *strategy, float confidence,+ const char *written_name, TSNode site) {+ const char *dot = strrchr(callee_qn, '.');+ const char *short_name = dot ? dot + 1 : callee_qn;+ if (strcmp(short_name, written_name) != 0) {+ py_emit_resolved_call_reason(ctx, callee_qn, "lsp_import_alias", confidence, written_name,+ site);+ } else {+ py_emit_resolved_call(ctx, callee_qn, strategy, confidence, site);+ }+}++ /* ── super(): the C3 method resolution order ──────────────────────── */++ /* Longest linearization followed. A longer one (or more bases than this) keeps+ * the base-by-base walk of the caller. */+ #define PY_C3_MAX 64++ /* The linearizations computed during one super() lookup: every class of a+ * diamond is linearized once, however many paths reach it. */+ typedef struct {+ const char *qn[PY_C3_MAX];+ const char **mro[PY_C3_MAX];+ int len[PY_C3_MAX];+ int count;+ } py_c3_cache_t;++ static bool py_c3_in_tail(const char *qn, const char *const *seq, int head, int len) {+ for (int k = head + 1; k < len; k++) {+ if (strcmp(seq[k], qn) == 0)+ return true;+ }+ return false;+ }++ /* The C3 head to take next: the first sequence head that is in no sequence's+ * tail. NULL when every head is blocked (Python rejects such a class). */+ static const char *py_c3_pick(const char **const *seqs, const int *lens, const int *heads,+ int nseq) {+ for (int s = 0; s < nseq; s++) {+ if (heads[s] >= lens[s])+ continue;+ const char *cand = seqs[s][heads[s]];+ bool blocked = false;+ for (int t = 0; t < nseq && !blocked; t++) {+ blocked = heads[t] < lens[t] && py_c3_in_tail(cand, seqs[t], heads[t], lens[t]);+ }+ if (!blocked)+ return cand;+ }+ return NULL;+ }++ /* Python's method resolution order of `type_qn`: the class, then the C3 merge+ * of its bases' linearizations and of the base list. A common base therefore+ * follows every class deriving from it. Sets *out to an arena array and returns+ * its length, or -1 when there is no C3 order, the hierarchy is deeper than+ * CBM_LSP_MAX_LOOKUP_DEPTH, or the order is longer than PY_C3_MAX. A base the+ * registry does not know (external, unresolved) linearizes to itself. */+ static int py_c3_linearize(PyLSPContext *ctx, py_c3_cache_t *cache, const char *type_qn,+ const char ***out, int depth) {+ for (int c = 0; c < cache->count; c++) {+ if (strcmp(cache->qn[c], type_qn) == 0) {+ *out = cache->mro[c];+ return cache->len[c];+ }+ }+ if (depth > CBM_LSP_MAX_LOOKUP_DEPTH)+ return -1;+ const CBMRegisteredType *rt = cbm_registry_lookup_type(ctx->registry, type_qn);+ if (rt && rt->alias_of)+ return py_c3_linearize(ctx, cache, rt->alias_of, out, depth + 1);+ const char **bases = rt ? rt->embedded_types : NULL;+ int nb = 0;+ while (bases && bases[nb])+ nb++;+ if (nb >= PY_C3_MAX)+ return -1;+ const char **seqs[PY_C3_MAX];+ int lens[PY_C3_MAX];+ int heads[PY_C3_MAX];+ for (int b = 0; b < nb; b++) {+ lens[b] = py_c3_linearize(ctx, cache, bases[b], &seqs[b], depth + 1);+ if (lens[b] < 0)+ return -1;+ heads[b] = 0;+ }+ seqs[nb] = bases;+ lens[nb] = nb;+ heads[nb] = 0;+ const char **mro = (const char **)cbm_arena_alloc(ctx->arena, PY_C3_MAX * sizeof(char *));+ if (!mro)+ return -1;+ int n = 0;+ mro[n++] = type_qn;+ for (;;) {+ bool pending = false;+ for (int s = 0; s <= nb && !pending; s++) {+ pending = heads[s] < lens[s];+ }+ if (!pending)+ break;+ const char *pick = py_c3_pick((const char **const *)seqs, lens, heads, nb + 1);+ if (!pick || n >= PY_C3_MAX)+ return -1;+ mro[n++] = pick;+ for (int s = 0; s <= nb; s++) {+ if (heads[s] < lens[s] && strcmp(seqs[s][heads[s]], pick) == 0)+ heads[s]++;+ }+ }+ if (cache->count < PY_C3_MAX) {+ cache->qn[cache->count] = type_qn;+ cache->mro[cache->count] = mro;+ cache->len[cache->count] = n;+ cache->count++;+ }+ *out = mro;+ return n;+ }++ /* The method super() reaches from inside `class_qn`: the first class AFTER it in+ * its C3 order that declares `member` itself. *have_mro is false when the+ * hierarchy has no C3 order; the caller then keeps its base-by-base walk. */+ static const CBMRegisteredFunc *py_lookup_super_attribute(PyLSPContext *ctx, const char *class_qn,+ const char *member, bool *have_mro) {+ *have_mro = false;+ if (!ctx || !class_qn || !member)+ return NULL;+ py_c3_cache_t cache;+ cache.count = 0;+ const char **mro = NULL;+ int n = py_c3_linearize(ctx, &cache, class_qn, &mro, 0);+ if (n < 0)+ return NULL;+ *have_mro = true;+ for (int i = 1; i < n; i++) {+ const CBMRegisteredFunc *f = cbm_registry_lookup_method(ctx->registry, mro[i], member);+ if (f)+ return f;+ }+ return NULL;+ }+/* Per-file field overlay: look up a (class_qn, field_name) recorded during resolve* against the sealed shared registry. Linear scan — the overlay only holds fields* discovered in THIS file's own classes, so it is small. */