Skip to content

Commit 1b64c50

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 DeusData#1642 Signed-off-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
1 parent 6c0ce23 commit 1b64c50

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
@@ -3249,7 +3249,26 @@ static void py_emit_call_for(PyLSPContext *ctx, TSNode call_node) {
32493249
py_emit_resolved_call_reason(ctx, qn, "lsp_import_alias", 0.85f, fname,
32503250
call_node);
32513251
} else if (qn && cbm_registry_lookup_type(ctx->registry, qn)) {
3252-
py_emit_resolved_call(ctx, qn, "lsp_constructor", 0.85f, call_node);
3252+
/* ClassName() runs a user __init__ found on the class or,
3253+
* depth-first, a declared base (the same walk
3254+
* super().__init__ uses, not C3 MRO). Retarget only when
3255+
* this binding IS the class being called. An instance
3256+
* (`f = Foo(); f()`) keeps the class row: that call
3257+
* dispatches to __call__, and a reason-join would invent
3258+
* a Foo.__init__ edge. A builtins.* __init__ is not a
3259+
* project constructor, so the class target stays. */
3260+
const CBMRegisteredFunc *fi = NULL;
3261+
if (qn_short && strcmp(qn_short, fname) == 0) {
3262+
fi = py_lookup_attribute(ctx, qn, "__init__");
3263+
}
3264+
if (fi && fi->qualified_name &&
3265+
strncmp(fi->qualified_name, "builtins.", sizeof("builtins.") - 1) != 0) {
3266+
py_emit_resolved_call_reason(ctx, fi->qualified_name,
3267+
"lsp_constructor_init", 0.85f, fname,
3268+
call_node);
3269+
} else {
3270+
py_emit_resolved_call(ctx, qn, "lsp_constructor", 0.85f, call_node);
3271+
}
32533272
}
32543273
}
32553274
return;

‎src/pipeline/lsp_resolve.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ static inline bool cbm_pipeline_invocation_reason_join_strategy(const char *stra
388388
strcmp(strategy, "lsp_method_ref_ctor_synth") == 0 ||
389389
strcmp(strategy, "lsp_dict_dispatch") == 0 ||
390390
strcmp(strategy, "lsp_import_alias") == 0 ||
391+
strcmp(strategy, "lsp_constructor_init") == 0 ||
391392
strcmp(strategy, "php_method_dynamic") == 0);
392393
}
393394

‎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
@@ -826,14 +826,31 @@ TEST(es_decorates_attribute_csharp) {
826826
* resolves in the registry. pass_usages.c handles this.
827827
* ══════════════════════════════════════════════════════════════════ */
828828

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

@@ -982,6 +999,7 @@ SUITE(edge_structural) {
982999
/* ── FAMILY 7: USAGE cross-file ─────────────────────────── */
9831000
/* Uncertain: depends on USAGE extraction for each language. */
9841001
RUN_TEST(es_usage_crossfile_python);
1002+
RUN_TEST(es_calls_python_constructor_init);
9851003
RUN_TEST(es_usage_crossfile_typescript);
9861004
RUN_TEST(es_usage_crossfile_go);
9871005

‎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)