Skip to content

Commit 904ecca

Browse files
committed
fix(python): resolve ClassName() calls to __init__
A Python construction call resolved to the Class node, so a user __init__ never received a caller from ClassName(...) and the dead-code view reported live constructors as unreferenced. When the called name is the class itself, resolve it to the __init__ found on the class or, depth-first, its declared bases (the lookup super().__init__() already uses), with a new lsp_constructor_init strategy that joins the textual call site through its reason. Classes with no user __init__ keep the Class target (lsp_constructor), as does a builtins.* __init__. Calls through an instance (f = Foo(); f()) are unchanged, since they dispatch to __call__. This changes the documented Class-target contract for classes that define __init__; the three tests that pinned it are updated. Refs #1642 Signed-off-by: Matt Van Horn <[email protected]>
1 parent 13ce2c0 commit 904ecca

6 files changed

Lines changed: 332 additions & 11 deletions

File tree

‎internal/cbm/lsp/py_lsp.c‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2599,7 +2599,26 @@ static void py_emit_call_for(PyLSPContext *ctx, TSNode call_node) {
25992599
py_emit_resolved_call_reason(ctx, qn, "lsp_import_alias", 0.85f, fname,
26002600
call_node);
26012601
} else if (qn && cbm_registry_lookup_type(ctx->registry, qn)) {
2602-
py_emit_resolved_call(ctx, qn, "lsp_constructor", 0.85f, call_node);
2602+
/* ClassName() runs a user __init__ found on the class or,
2603+
* depth-first, a declared base (the same walk
2604+
* super().__init__ uses, not C3 MRO). Retarget only when
2605+
* this binding IS the class being called. An instance
2606+
* (`f = Foo(); f()`) keeps the class row: that call
2607+
* dispatches to __call__, and a reason-join would invent
2608+
* a Foo.__init__ edge. A builtins.* __init__ is not a
2609+
* project constructor, so the class target stays. */
2610+
const CBMRegisteredFunc *fi = NULL;
2611+
if (qn_short && strcmp(qn_short, fname) == 0) {
2612+
fi = py_lookup_attribute(ctx, qn, "__init__");
2613+
}
2614+
if (fi && fi->qualified_name &&
2615+
strncmp(fi->qualified_name, "builtins.", sizeof("builtins.") - 1) != 0) {
2616+
py_emit_resolved_call_reason(ctx, fi->qualified_name,
2617+
"lsp_constructor_init", 0.85f, fname,
2618+
call_node);
2619+
} else {
2620+
py_emit_resolved_call(ctx, qn, "lsp_constructor", 0.85f, call_node);
2621+
}
26032622
}
26042623
}
26052624
return;

‎src/pipeline/lsp_resolve.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ static inline bool cbm_pipeline_invocation_reason_join_strategy(const char *stra
327327
strcmp(strategy, "lsp_method_ref_ctor_synth") == 0 ||
328328
strcmp(strategy, "lsp_dict_dispatch") == 0 ||
329329
strcmp(strategy, "lsp_import_alias") == 0 ||
330+
strcmp(strategy, "lsp_constructor_init") == 0 ||
330331
strcmp(strategy, "php_method_dynamic") == 0);
331332
}
332333

‎tests/repro/repro_lsp_go_py.c‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@
6464
* PYTHON STRATEGY INVENTORY — every literal "lsp_..." emitted by py_lsp.c
6565
* (grep '"lsp_' internal/cbm/lsp/py_lsp.c), with its keying site:
6666
* lsp_direct (py_lsp.c:1631) module-local f()
67-
* lsp_constructor (py_lsp.c:1624) ClassName() where the name is a
68-
* NAMED type in scope
67+
* lsp_constructor ClassName() with no user __init__ → the Class
68+
* lsp_constructor_init ClassName() with a user __init__ (on the class
69+
* or a declared base) → that __init__. The textual
70+
* callee leaf stays the class name; reason joins it.
6971
* lsp_method (py_lsp.c:1731) obj.method() on a NAMED-typed
7072
* receiver (covers self.other())
7173
* lsp_super (py_lsp.c:1693) super().method() resolved on a
@@ -340,13 +342,24 @@ static const char kPyDirect[] =
340342
"def caller(v):\n"
341343
" return helper(v)\n";
342344

343-
/* lsp_constructor — ClassName() where the name is a NAMED type in scope
344-
* (py_lsp.c:1620-1624: cbm_scope_lookup yields a NAMED type → emit constructor
345-
* edge to the class QN). */
345+
/* lsp_constructor — ClassName() where the class has no user __init__.
346+
* The edge stays on the Class node. A class that defines __init__ is
347+
* lsp_constructor_init below; strstr would otherwise treat that strategy
348+
* as a hit for "lsp_constructor". */
346349
static const char kPyConstructor[] =
350+
"class Widget:\n"
351+
" def label(self):\n"
352+
" return 1\n"
353+
"def caller():\n"
354+
" return Widget()\n";
355+
356+
/* lsp_constructor_init — ClassName() resolves to the user __init__
357+
* (issue #1642). reason carries the class-name leaf so the pipeline join
358+
* still matches the textual call. */
359+
static const char kPyConstructorInit[] =
347360
"class Widget:\n"
348361
" def __init__(self):\n"
349-
" pass\n"
362+
" self.ready = True\n"
350363
"def caller():\n"
351364
" return Widget()\n";
352365

@@ -538,6 +551,10 @@ TEST(repro_lsp_py_constructor) {
538551
return assert_lsp_strategy("main.py", kPyConstructor, "lsp_constructor");
539552
}
540553

554+
TEST(repro_lsp_py_constructor_init) {
555+
return assert_lsp_strategy("main.py", kPyConstructorInit, "lsp_constructor_init");
556+
}
557+
541558
TEST(repro_lsp_py_method) {
542559
return assert_lsp_strategy("main.py", kPyMethod, "lsp_method");
543560
}
@@ -623,6 +640,7 @@ SUITE(repro_lsp_go_py) {
623640

624641
RUN_TEST(repro_lsp_py_direct);
625642
RUN_TEST(repro_lsp_py_constructor);
643+
RUN_TEST(repro_lsp_py_constructor_init);
626644
RUN_TEST(repro_lsp_py_method);
627645
RUN_TEST(repro_lsp_py_super);
628646
RUN_TEST(repro_lsp_py_super_init);

‎tests/test_edge_structural.c‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -825,14 +825,31 @@ TEST(es_decorates_attribute_csharp) {
825825
* resolves in the registry. pass_usages.c handles this.
826826
* ══════════════════════════════════════════════════════════════════ */
827827

828-
/* Python constructor syntax is a CALLS edge to the materialized Class node. */
828+
/* Python ClassName() with a user __init__ is a CALLS edge to that method.
829+
* The Class node no longer receives the instantiation edge. A class with no
830+
* user __init__ still targets the Class (lsp_constructor). */
829831
TEST(es_usage_crossfile_python) {
830832
static const ES_LangFile f[] = {
831833
{"models.py",
832834
"class User:\n def __init__(self, name):\n self.name = name\n"},
833835
{"main.py",
834836
"from .models import User\n\n\ndef create_user(name):\n return User(name)\n"}};
835-
ASSERT_EQ(es_exact_edge_by_name(f, 2, "CALLS", "create_user", "User"), 1);
837+
ASSERT_EQ(es_exact_edge_by_name(f, 2, "CALLS", "create_user", "__init__"), 1);
838+
ASSERT_EQ(es_exact_edge_by_name(f, 2, "CALLS", "create_user", "User"), 0);
839+
PASS();
840+
}
841+
842+
/* Issue #1642 repro: build() -> Embedder.__init__, not the Embedder class. */
843+
TEST(es_calls_python_constructor_init) {
844+
static const ES_LangFile f[] = {
845+
{"main.py", "class Embedder:\n"
846+
" def __init__(self, model=\"x\"):\n"
847+
" self.model = model\n"
848+
"\n"
849+
"def build():\n"
850+
" return Embedder(model=\"y\")\n"}};
851+
ASSERT_EQ(es_exact_edge_by_name(f, 1, "CALLS", "build", "__init__"), 1);
852+
ASSERT_EQ(es_exact_edge_by_name(f, 1, "CALLS", "build", "Embedder"), 0);
836853
PASS();
837854
}
838855

@@ -981,6 +998,7 @@ SUITE(edge_structural) {
981998
/* ── FAMILY 7: USAGE cross-file ─────────────────────────── */
982999
/* Uncertain: depends on USAGE extraction for each language. */
9831000
RUN_TEST(es_usage_crossfile_python);
1001+
RUN_TEST(es_calls_python_constructor_init);
9841002
RUN_TEST(es_usage_crossfile_typescript);
9851003
RUN_TEST(es_usage_crossfile_go);
9861004

‎tests/test_lsp_resolution_probe.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,16 +1837,19 @@ TEST(lrp_tsx_usage_import_type_with_decoy) {
18371837
PASS();
18381838
}
18391839

1840-
/* Python: constructor syntax is an exact call to the materialized Class node. */
1840+
/* Python: ClassName() with a user __init__ is an exact CALLS edge to that
1841+
* method. The Class node no longer receives the instantiation edge. */
18411842
TEST(lrp_python_usage_instantiation) {
18421843
static const LRP_File f[] = {
18431844
{"model.py", "class User:\n def __init__(self, name):\n self.name = name\n"},
18441845
{"main.py", "from .model import User\n\n\ndef create(name):\n return User(name)\n"}};
18451846
LRP_Proj lp;
18461847
cbm_store_t *store = lrp_index(&lp, f, 2);
1847-
int exact = store ? lrp_exact_calls_by_name(store, lp.project, "create", "User") : -1;
1848+
int exact = store ? lrp_exact_calls_by_name(store, lp.project, "create", "__init__") : -1;
1849+
int class_edge = store ? lrp_exact_calls_by_name(store, lp.project, "create", "User") : -1;
18481850
lrp_cleanup(&lp, store);
18491851
ASSERT_EQ(exact, 1);
1852+
ASSERT_EQ(class_edge, 0);
18501853
PASS();
18511854
}
18521855

0 commit comments

Comments
 (0)