Skip to content

Commit 8ae04f4

Browse files
authored
Merge pull request DeusData#2424 from mvanhorn/fix/1642-python-constructor-calls-init
fix(python): resolve ClassName() calls to __init__
2 parents fabc2b0 + e87a9ff commit 8ae04f4

7 files changed

Lines changed: 413 additions & 13 deletions

File tree

‎internal/cbm/lsp/py_lsp.c‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3249,7 +3249,28 @@ 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() binds to the __init__ the class declares
3253+
* itself. An inherited __init__ is not followed: B(A)
3254+
* without its own __init__ keeps the call on B, so a base
3255+
* constructor does not collect every subclass
3256+
* construction as its caller. Retarget only when this
3257+
* binding IS the class being called. An instance
3258+
* (`f = Foo(); f()`) keeps the class row: that call
3259+
* dispatches to __call__, and a reason-join would invent
3260+
* a Foo.__init__ edge. A builtins.* __init__ is not a
3261+
* project constructor, so the class target stays. */
3262+
const CBMRegisteredFunc *fi = NULL;
3263+
if (qn_short && strcmp(qn_short, fname) == 0) {
3264+
fi = cbm_registry_lookup_method(ctx->registry, qn, "__init__");
3265+
}
3266+
if (fi && fi->qualified_name &&
3267+
strncmp(fi->qualified_name, "builtins.", sizeof("builtins.") - 1) != 0) {
3268+
py_emit_resolved_call_reason(ctx, fi->qualified_name,
3269+
"lsp_constructor_init", 0.85f, fname,
3270+
call_node);
3271+
} else {
3272+
py_emit_resolved_call(ctx, qn, "lsp_constructor", 0.85f, call_node);
3273+
}
32533274
}
32543275
}
32553276
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: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,12 @@
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() where the class declares no __init__
68+
* of its own (an inherited one is not followed)
69+
* → the Class
70+
* lsp_constructor_init ClassName() where the class declares its own
71+
* __init__ → that __init__. The textual callee
72+
* leaf stays the class name; reason joins it.
6973
* lsp_method (py_lsp.c:1731) obj.method() on a NAMED-typed
7074
* receiver (covers self.other())
7175
* lsp_super (py_lsp.c:1693) super().method() resolved on a
@@ -340,13 +344,24 @@ static const char kPyDirect[] =
340344
"def caller(v):\n"
341345
" return helper(v)\n";
342346

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). */
347+
/* lsp_constructor — ClassName() where the class has no user __init__.
348+
* The edge stays on the Class node. A class that defines __init__ is
349+
* lsp_constructor_init below; strstr would otherwise treat that strategy
350+
* as a hit for "lsp_constructor". */
346351
static const char kPyConstructor[] =
352+
"class Widget:\n"
353+
" def label(self):\n"
354+
" return 1\n"
355+
"def caller():\n"
356+
" return Widget()\n";
357+
358+
/* lsp_constructor_init — ClassName() resolves to the user __init__
359+
* (issue #1642). reason carries the class-name leaf so the pipeline join
360+
* still matches the textual call. */
361+
static const char kPyConstructorInit[] =
347362
"class Widget:\n"
348363
" def __init__(self):\n"
349-
" pass\n"
364+
" self.ready = True\n"
350365
"def caller():\n"
351366
" return Widget()\n";
352367

@@ -538,6 +553,10 @@ TEST(repro_lsp_py_constructor) {
538553
return assert_lsp_strategy("main.py", kPyConstructor, "lsp_constructor");
539554
}
540555

556+
TEST(repro_lsp_py_constructor_init) {
557+
return assert_lsp_strategy("main.py", kPyConstructorInit, "lsp_constructor_init");
558+
}
559+
541560
TEST(repro_lsp_py_method) {
542561
return assert_lsp_strategy("main.py", kPyMethod, "lsp_method");
543562
}
@@ -623,6 +642,7 @@ SUITE(repro_lsp_go_py) {
623642

624643
RUN_TEST(repro_lsp_py_direct);
625644
RUN_TEST(repro_lsp_py_constructor);
645+
RUN_TEST(repro_lsp_py_constructor_init);
626646
RUN_TEST(repro_lsp_py_method);
627647
RUN_TEST(repro_lsp_py_super);
628648
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: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,16 +1837,62 @@ 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);
1853+
PASS();
1854+
}
1855+
1856+
/* Python same-file subclasses in the graph: ClassName() binds only to an
1857+
* __init__ the class declares itself. B(A) declares none, so make_b keeps
1858+
* its CALLS edge on the class B and the inherited A.__init__ gets none.
1859+
* C(A) declares its own __init__, so make_c calls C.__init__, not C and not
1860+
* the inherited A.__init__. */
1861+
TEST(lrp_python_subclass_constructor_own_init_only) {
1862+
static const LRP_File f[] = {{"main.py", "class A:\n"
1863+
" def __init__(self):\n"
1864+
" self.ready = True\n"
1865+
"\n"
1866+
"class B(A):\n"
1867+
" def go(self):\n"
1868+
" return 1\n"
1869+
"\n"
1870+
"class C(A):\n"
1871+
" def __init__(self):\n"
1872+
" self.ready = False\n"
1873+
"\n"
1874+
"def make_b():\n"
1875+
" return B()\n"
1876+
"\n"
1877+
"def make_c():\n"
1878+
" return C()\n"}};
1879+
LRP_Proj lp;
1880+
cbm_store_t *store = lrp_index(&lp, f, 1);
1881+
const char *p = store ? lp.project : NULL;
1882+
int b_class = lrp_exact_edge_by_qn_suffix(store, p, "CALLS", "main.make_b", "main.B");
1883+
int b_base_init =
1884+
lrp_exact_edge_by_qn_suffix(store, p, "CALLS", "main.make_b", "main.A.__init__");
1885+
int c_own_init =
1886+
lrp_exact_edge_by_qn_suffix(store, p, "CALLS", "main.make_c", "main.C.__init__");
1887+
int c_class = lrp_exact_edge_by_qn_suffix(store, p, "CALLS", "main.make_c", "main.C");
1888+
int c_base_init =
1889+
lrp_exact_edge_by_qn_suffix(store, p, "CALLS", "main.make_c", "main.A.__init__");
1890+
lrp_cleanup(&lp, store);
1891+
ASSERT_EQ(b_class, 1);
1892+
ASSERT_EQ(b_base_init, 0);
1893+
ASSERT_EQ(c_own_init, 1);
1894+
ASSERT_EQ(c_class, 0);
1895+
ASSERT_EQ(c_base_init, 0);
18501896
PASS();
18511897
}
18521898

@@ -1967,4 +2013,5 @@ SUITE(lsp_resolution_probe) {
19672013
RUN_TEST(lrp_ts_usage_import_type_with_decoy);
19682014
RUN_TEST(lrp_tsx_usage_import_type_with_decoy);
19692015
RUN_TEST(lrp_python_usage_instantiation);
2016+
RUN_TEST(lrp_python_subclass_constructor_own_init_only);
19702017
}

‎tests/test_pipeline.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21140,8 +21140,9 @@ static int pyn_run_init_collision(int fillers) {
2114021140
const char *project = cbm_pipeline_project_name(p);
2114121141
imports[run] = pyn_edge(s, project, "tests.test_app.py.__file__",
2114221142
"src.rowpkg.layout.Layout", "IMPORTS");
21143+
/* Layout defines __init__, so Layout("demo") targets it (#1642). */
2114321144
calls[run] = pyn_edge(s, project, "tests.test_app.test_build",
21144-
"src.rowpkg.layout.Layout", "CALLS");
21145+
"src.rowpkg.layout.Layout.__init__", "CALLS");
2114521146
method[run] = pyn_edge(s, project, "tests.test_app.test_build",
2114621147
"src.rowpkg.layout.Layout.create", "CALLS");
2114721148
cbm_store_close(s);
@@ -21160,7 +21161,7 @@ static int pyn_run_init_collision(int fillers) {
2116021161
for (int run = 0; run < 2; run++) {
2116121162
ASSERT_EQ(rc[run], 0);
2116221163
ASSERT_TRUE(imports[run]); /* the class stays importable */
21163-
ASSERT_TRUE(calls[run]); /* Layout("demo") */
21164+
ASSERT_TRUE(calls[run]); /* Layout("demo") -> Layout.__init__ */
2116421165
ASSERT_TRUE(method[run]); /* .create() through the constructed type */
2116521166
}
2116621167
PASS();

0 commit comments

Comments
 (0)