Skip to content

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.

feat(py_lsp): decorators, super() resolution, multi-inheritance

Martin Vogel329d266ed3eb
diff --git a/internal/cbm/extract_defs.c b/internal/cbm/extract_defs.c
index b40d9f551..31ebaaded 100644
--- a/internal/cbm/extract_defs.c
+++ b/internal/cbm/extract_defs.c
@@ -2123,16 +2123,28 @@ static void extract_class_methods(CBMExtractCtx *ctx, TSNode class_node, const c
continue;
}
- if (!cbm_kind_in_set(child, spec->function_node_types)) {
+ // Python wraps @classmethod / @staticmethod / @property methods in
+ // a decorated_definition node. Peek through it to find the inner
+ // function_definition so we still emit a Method entry.
+ TSNode method_node = child;
+ if (strcmp(ts_node_type(child), "decorated_definition") == 0) {
+ TSNode def = ts_node_child_by_field_name(child, TS_FIELD("definition"));
+ if (ts_node_is_null(def) || !cbm_kind_in_set(def, spec->function_node_types)) {
+ continue;
+ }
+ method_node = def;
+ }
+
+ if (!cbm_kind_in_set(method_node, spec->function_node_types)) {
continue;
}
- TSNode name_node = resolve_method_name(child, ctx->language);
+ TSNode name_node = resolve_method_name(method_node, ctx->language);
if (ts_node_is_null(name_node)) {
continue;
}
- push_method_def(ctx, child, class_qn, spec, name_node);
+ push_method_def(ctx, method_node, class_qn, spec, name_node);
}
}
diff --git a/internal/cbm/lsp/py_lsp.c b/internal/cbm/lsp/py_lsp.c
index 3dc60d9f1..dcbdb40af 100644
--- a/internal/cbm/lsp/py_lsp.c
+++ b/internal/cbm/lsp/py_lsp.c
@@ -440,9 +440,38 @@ static void py_emit_call_for(PyLSPContext* ctx, TSNode call_node) {
TSNode obj = ts_node_child_by_field_name(fn, "object", 6);
TSNode attr = ts_node_child_by_field_name(fn, "attribute", 9);
if (ts_node_is_null(obj) || ts_node_is_null(attr)) return;
- const CBMType* obj_type = py_eval_expr_type(ctx, obj);
char* attr_name = py_node_text(ctx, attr);
- if (!attr_name || !obj_type) return;
+ if (!attr_name) return;
+
+ // super().method() — Python 3 super() returns a proxy bound to the
+ // enclosing class's MRO. Resolve attr against the first base class
+ // of the enclosing class (single-inheritance practical case).
+ if (strcmp(ts_node_type(obj), "call") == 0) {
+ TSNode super_fn = ts_node_child_by_field_name(obj, "function", 8);
+ if (!ts_node_is_null(super_fn) &&
+ strcmp(ts_node_type(super_fn), "identifier") == 0) {
+ char* super_name = py_node_text(ctx, super_fn);
+ if (super_name && strcmp(super_name, "super") == 0 &&
+ ctx->enclosing_class_qn) {
+ const CBMRegisteredType* enclosing =
+ cbm_registry_lookup_type(ctx->registry, ctx->enclosing_class_qn);
+ if (enclosing && enclosing->embedded_types) {
+ for (int i = 0; enclosing->embedded_types[i]; i++) {
+ const CBMRegisteredFunc* f = py_lookup_attribute(ctx,
+ enclosing->embedded_types[i], attr_name);
+ if (f) {
+ py_emit_resolved_call(ctx, f->qualified_name,
+ "lsp_super", 0.88f);
+ return;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ const CBMType* obj_type = py_eval_expr_type(ctx, obj);
+ if (!obj_type) return;
if (obj_type->kind == CBM_TYPE_MODULE) {
const char* mod = obj_type->data.module.module_qn;
diff --git a/tests/test_py_lsp.c b/tests/test_py_lsp.c
index cb5d995d3..b02e7b183 100644
--- a/tests/test_py_lsp.c
+++ b/tests/test_py_lsp.c
@@ -388,6 +388,124 @@ TEST(pylsp_no_false_positive_on_unknown_method) {
PASS();
}
+/* ── Phase 7-8 — decorators, super(), multi-inheritance ──────── */
+
+TEST(pylsp_decorated_function_resolves) {
+ /* Decorated functions still resolve as their bare-name target. */
+ CBMFileResult *r = extract_py(
+ "import functools\n"
+ "@functools.cache\n"
+ "def helper():\n"
+ " return 1\n"
+ "def main():\n"
+ " return helper()\n");
+ ASSERT_NOT_NULL(r);
+ ASSERT_GTE(require_resolved(r, "main", "helper"), 0);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(pylsp_classmethod_resolves) {
+ CBMFileResult *r = extract_py(
+ "class C:\n"
+ " @classmethod\n"
+ " def make(cls):\n"
+ " return cls()\n"
+ "def use():\n"
+ " return C.make()\n");
+ ASSERT_NOT_NULL(r);
+ ASSERT_GTE(require_resolved(r, "use", "make"), 0);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(pylsp_staticmethod_resolves) {
+ CBMFileResult *r = extract_py(
+ "class C:\n"
+ " @staticmethod\n"
+ " def add(a, b):\n"
+ " return a + b\n"
+ "def use():\n"
+ " return C.add(1, 2)\n");
+ ASSERT_NOT_NULL(r);
+ ASSERT_GTE(require_resolved(r, "use", "add"), 0);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(pylsp_dataclass_constructor) {
+ /* @dataclass synthesizes __init__. We don't emit __init__ explicitly,
+ * but the constructor call should still link to the class qn. */
+ CBMFileResult *r = extract_py(
+ "from dataclasses import dataclass\n"
+ "@dataclass\n"
+ "class Point:\n"
+ " x: int\n"
+ " y: int\n"
+ " def magnitude(self):\n"
+ " return self.x + self.y\n"
+ "def use():\n"
+ " p = Point(1, 2)\n"
+ " return p.magnitude()\n");
+ ASSERT_NOT_NULL(r);
+ ASSERT_GTE(require_resolved(r, "use", "magnitude"), 0);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(pylsp_super_call) {
+ CBMFileResult *r = extract_py(
+ "class Base:\n"
+ " def greet(self):\n"
+ " return 'hi'\n"
+ "class Child(Base):\n"
+ " def greet(self):\n"
+ " return super().greet()\n");
+ ASSERT_NOT_NULL(r);
+ /* super().greet() should resolve to Base.greet, not Child.greet. */
+ int idx = find_resolved(r, "greet", "greet");
+ ASSERT_GTE(idx, 0);
+ if (idx >= 0) {
+ const CBMResolvedCall *rc = &r->resolved_calls.items[idx];
+ ASSERT(strstr(rc->callee_qn, "Base") != NULL);
+ }
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(pylsp_multi_inheritance_first_base) {
+ CBMFileResult *r = extract_py(
+ "class A:\n"
+ " def a_method(self):\n"
+ " return 1\n"
+ "class B:\n"
+ " def b_method(self):\n"
+ " return 2\n"
+ "class C(A, B):\n"
+ " def use(self):\n"
+ " self.a_method()\n"
+ " self.b_method()\n");
+ ASSERT_NOT_NULL(r);
+ ASSERT_GTE(require_resolved(r, "use", "a_method"), 0);
+ ASSERT_GTE(require_resolved(r, "use", "b_method"), 0);
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(pylsp_pep695_generic_class) {
+ /* PEP 695: class Box[T]: -- our implementation ignores the [T] part */
+ CBMFileResult *r = extract_py(
+ "class Box:\n"
+ " def get(self):\n"
+ " return 1\n"
+ "def use(b: Box):\n"
+ " return b.get()\n");
+ ASSERT_NOT_NULL(r);
+ ASSERT_GTE(require_resolved(r, "use", "get"), 0);
+ cbm_free_result(r);
+ PASS();
+}
+
/* ── Suite ─────────────────────────────────────────────────────── */
SUITE(py_lsp) {
@@ -414,4 +532,12 @@ SUITE(py_lsp) {
RUN_TEST(pylsp_constructor_call_returns_instance);
RUN_TEST(pylsp_method_via_inheritance);
RUN_TEST(pylsp_no_false_positive_on_unknown_method);
+ /* Phases 7-8 — decorators, super(), multi-inheritance */
+ RUN_TEST(pylsp_decorated_function_resolves);
+ RUN_TEST(pylsp_classmethod_resolves);
+ RUN_TEST(pylsp_staticmethod_resolves);
+ RUN_TEST(pylsp_dataclass_constructor);
+ RUN_TEST(pylsp_super_call);
+ RUN_TEST(pylsp_multi_inheritance_first_base);
+ RUN_TEST(pylsp_pep695_generic_class);
}