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.

fix(python): make iris.cls() callee primary, drop the extra bare call

Thomas Dyar075f40e9d4cc
diff --git a/internal/cbm/extract_calls.c b/internal/cbm/extract_calls.c
index 70dbc3a4f..e508bd47a 100644
--- a/internal/cbm/extract_calls.c
+++ b/internal/cbm/extract_calls.c
@@ -3931,6 +3931,16 @@ CBMInvocationDescriptor handle_calls(CBMExtractCtx *ctx, TSNode node, const CBML
}
}
+ // #1260: iris.cls("Pkg.X").M() names its class, so the qualified
+ // callee replaces the bare one. Keeping both would let the bare
+ // name weak-match an unrelated same-named method.
+ if (ctx->language == CBM_LANG_PYTHON) {
+ const char *iris_callee = python_iris_cls_callee(ctx, node);
+ if (iris_callee) {
+ call.callee_name = iris_callee;
+ call.is_method = false;
+ }
+ }
cbm_calls_push(&ctx->result->calls, ctx->arena, call);
invocation = describe_emitted_primary_call(node, &callee);
@@ -3955,17 +3965,6 @@ CBMInvocationDescriptor handle_calls(CBMExtractCtx *ctx, TSNode node, const CBML
}
}
}
- if (ctx->language == CBM_LANG_PYTHON) {
- const char *iris_callee = python_iris_cls_callee(ctx, node);
- if (iris_callee) {
- CBMCall xcall = {0};
- xcall.callee_name = iris_callee;
- xcall.enclosing_func_qn = call.enclosing_func_qn;
- xcall.site_start_byte = call.site_start_byte;
- xcall.site_end_byte = call.site_end_byte;
- cbm_calls_push(&ctx->result->calls, ctx->arena, xcall);
- }
- }
}
}
diff --git a/tests/test_extraction.c b/tests/test_extraction.c
index 865a248ab..5f9379658 100644
--- a/tests/test_extraction.c
+++ b/tests/test_extraction.c
@@ -3285,6 +3285,28 @@ static int count_calls_in_func(CBMFileResult *r, const char *callee, const char
return n;
}
+/* #1260: calls in `func` whose callee is "Run" or ends in ".Run", whatever
+ * the qualifier. One iris.cls("X").Run() site must emit exactly one. */
+static int count_run_calls_in_func(CBMFileResult *r, const char *func) {
+ int n = 0;
+ size_t flen = strlen(func);
+ for (int i = 0; i < r->calls.count; i++) {
+ const CBMCall *c = &r->calls.items[i];
+ if (!c->callee_name || !c->enclosing_func_qn) {
+ continue;
+ }
+ size_t cl = strlen(c->callee_name);
+ bool is_run = strcmp(c->callee_name, "Run") == 0 ||
+ (cl > 4 && strcmp(c->callee_name + cl - 4, ".Run") == 0);
+ size_t qlen = strlen(c->enclosing_func_qn);
+ if (is_run && qlen > flen && c->enclosing_func_qn[qlen - flen - 1] == '.' &&
+ strcmp(c->enclosing_func_qn + qlen - flen, func) == 0) {
+ n++;
+ }
+ }
+ return n;
+}
+
TEST(python_iris_cls_receiver_is_class_aware) {
CBMFileResult *r = extract("import iris\n"
"def want_a():\n"
@@ -3303,6 +3325,10 @@ TEST(python_iris_cls_receiver_is_class_aware) {
ASSERT_EQ(count_calls_in_func(r, "Pkg.A.Run", "want_a"), 1);
ASSERT_EQ(count_calls_in_func(r, "Pkg.B.Run", "want_b"), 1);
ASSERT_EQ(count_calls_in_func(r, "Pkg.A.Run", "want_b"), 0);
+ /* The class-qualified callee replaces the bare one: no second call per site. */
+ ASSERT_EQ(count_run_calls_in_func(r, "want_a"), 1);
+ ASSERT_EQ(count_run_calls_in_func(r, "want_b"), 1);
+ ASSERT_EQ(count_run_calls_in_func(r, "dynamic"), 1);
/* A non-literal class argument names nothing: no class-qualified callee. */
ASSERT_EQ(count_calls_in_func(r, "name.Run", "dynamic"), 0);
/* An f-string is a Python "string" node but not a class name. */
diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c
index b47ea2d4c..76650461b 100644
--- a/tests/test_pipeline.c
+++ b/tests/test_pipeline.c
@@ -14729,9 +14729,17 @@ TEST(pipeline_python_iris_cls_class_aware_calls) {
write_temp_file(tmp, "A.cls", cls);
snprintf(cls, sizeof(cls), "Class Pkg.B Extends %%RegisteredObject\n%s", run_body);
write_temp_file(tmp, "B.cls", cls);
+ /* A third, unrelated Run: a bare "Run" callee could weak-match it. */
+ write_temp_file(tmp, "util.py",
+ "def Run(x):\n"
+ " return x\n");
write_temp_file(tmp, "caller.py",
"import iris\n"
"\n"
+ "class Helper:\n"
+ " def Run(self, x):\n"
+ " return x\n"
+ "\n"
"def want_a():\n"
" return iris.cls(\"Pkg.A\").Run(\"x\")\n"
"\n"
@@ -14763,6 +14771,10 @@ TEST(pipeline_python_iris_cls_class_aware_calls) {
ASSERT_EQ(iris_cls_calls(s, project, "want_a", NULL), 1);
ASSERT_EQ(iris_cls_calls(s, project, "want_b", "Pkg.B.Run"), 1);
ASSERT_EQ(iris_cls_calls(s, project, "want_b", NULL), 1);
+ ASSERT_EQ(iris_cls_calls(s, project, "want_a", "util.Run"), 0);
+ ASSERT_EQ(iris_cls_calls(s, project, "want_b", "util.Run"), 0);
+ ASSERT_EQ(iris_cls_calls(s, project, "want_a", "Helper.Run"), 0);
+ ASSERT_EQ(iris_cls_calls(s, project, "want_b", "Helper.Run"), 0);
ASSERT_EQ(iris_cls_calls(s, project, "want_nonexistent", NULL), 0);
ASSERT_EQ(iris_cls_calls(s, project, "unrelated_receiver", NULL), 0);
ASSERT_EQ(iris_cls_calls(s, project, "via_classmethodvalue", "Pkg.B.Run"), 1);