Skip to content

Commit 0547190

Browse files
committed
fix(python): bind ClassName() only to the class own __init__
A construction call now resolves to __init__ only when the called class declares that __init__ itself. An inherited __init__ is no longer followed: class B(A) without its own __init__ keeps the B() call on the Class B, as it was before this branch. Following the bases bound every subclass construction to the base constructor. On a django index (fb113765) that made Model.__init__ a 583-caller hub (every model instantiation), bound IntegerField() to Field.__init__ and HttpResponseForbidden() to HttpResponse.__init__, and degraded 625 constructor call sites versus main (457 fell back to textual resolution, 168 lost their row; CALLS -161). With own __init__ only, 6016 sites move to the class own __init__, 5468 stay on the Class, none fall back to a textual strategy, and total CALLS is +4 versus main. Tests: the same-file and cross-file inherited cases now pin the Class target, and a new same-file subclass test at the LSP and the graph level covers B(A) without __init__ (stays on B, never A.__init__) and C(A) with its own __init__ (binds to C.__init__, never the class C or A.__init__). The strategy inventory comment in the repro suite is updated to match. Refs DeusData#1642 Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent aa02511 commit 0547190

4 files changed

Lines changed: 105 additions & 27 deletions

File tree

‎internal/cbm/lsp/py_lsp.c‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3249,17 +3249,19 @@ 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-
/* 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
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
32563258
* (`f = Foo(); f()`) keeps the class row: that call
32573259
* dispatches to __call__, and a reason-join would invent
32583260
* a Foo.__init__ edge. A builtins.* __init__ is not a
32593261
* project constructor, so the class target stays. */
32603262
const CBMRegisteredFunc *fi = NULL;
32613263
if (qn_short && strcmp(qn_short, fname) == 0) {
3262-
fi = py_lookup_attribute(ctx, qn, "__init__");
3264+
fi = cbm_registry_lookup_method(ctx->registry, qn, "__init__");
32633265
}
32643266
if (fi && fi->qualified_name &&
32653267
strncmp(fi->qualified_name, "builtins.", sizeof("builtins.") - 1) != 0) {

‎tests/repro/repro_lsp_go_py.c‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +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 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.
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.
7173
* lsp_method (py_lsp.c:1731) obj.method() on a NAMED-typed
7274
* receiver (covers self.other())
7375
* lsp_super (py_lsp.c:1693) super().method() resolved on a

‎tests/test_lsp_resolution_probe.c‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1853,6 +1853,49 @@ TEST(lrp_python_usage_instantiation) {
18531853
PASS();
18541854
}
18551855

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);
1896+
PASS();
1897+
}
1898+
18561899
/* ══════════════════════════════════════════════════════════════════════
18571900
* SUITE registration
18581901
* ══════════════════════════════════════════════════════════════════════ */
@@ -1970,4 +2013,5 @@ SUITE(lsp_resolution_probe) {
19702013
RUN_TEST(lrp_ts_usage_import_type_with_decoy);
19712014
RUN_TEST(lrp_tsx_usage_import_type_with_decoy);
19722015
RUN_TEST(lrp_python_usage_instantiation);
2016+
RUN_TEST(lrp_python_subclass_constructor_own_init_only);
19732017
}

‎tests/test_py_lsp.c‎

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -403,26 +403,55 @@ TEST(pylsp_constructor_call_targets_init) {
403403
PASS();
404404
}
405405

406-
/* class B(A) with only A.__init__ resolves construction of B to A.__init__. */
407-
TEST(pylsp_constructor_inherited_init) {
406+
/* Same-file subclasses: only an __init__ the class declares itself is a
407+
* construction target. B(A) inherits A.__init__ but declares none, so B()
408+
* stays on the class B (never A.__init__). C(A) declares its own __init__,
409+
* so C() resolves to C.__init__ (never the inherited A.__init__). */
410+
static int count_build_rows(const CBMFileResult *r, const char *caller_leaf,
411+
const char *callee_qn, const char *strategy) {
412+
int n = 0;
413+
for (int i = 0; i < r->resolved_calls.count; i++) {
414+
const CBMResolvedCall *rc = &r->resolved_calls.items[i];
415+
if (rc->kind != CBM_RESOLVED_INVOCATION || !rc->caller_qn || !rc->callee_qn ||
416+
!rc->strategy)
417+
continue;
418+
const char *dot = strrchr(rc->caller_qn, '.');
419+
const char *leaf = dot ? dot + 1 : rc->caller_qn;
420+
if (strcmp(leaf, caller_leaf) == 0 && strcmp(rc->callee_qn, callee_qn) == 0 &&
421+
strcmp(rc->strategy, strategy) == 0)
422+
n++;
423+
}
424+
return n;
425+
}
426+
427+
TEST(pylsp_constructor_same_file_subclass_own_init_only) {
408428
CBMFileResult *r = extract_py("class A:\n"
409429
" def __init__(self):\n"
410430
" self.ready = True\n"
411431
"class B(A):\n"
412432
" def go(self):\n"
413433
" return 1\n"
414-
"def build():\n"
415-
" return B()\n");
434+
"class C(A):\n"
435+
" def __init__(self):\n"
436+
" self.ready = False\n"
437+
"def build_b():\n"
438+
" return B()\n"
439+
"def build_c():\n"
440+
" return C()\n");
416441
ASSERT_NOT_NULL(r);
417-
int idx = require_resolved(r, "build", "A.__init__");
442+
/* B() keeps the class target; the inherited A.__init__ gets no row. */
443+
ASSERT_EQ(count_build_rows(r, "build_b", "test.main.B", "lsp_constructor"), 1);
444+
ASSERT_EQ(find_resolved(r, "build_b", "__init__"), -1);
445+
/* C() binds to the __init__ C declares itself. */
446+
int idx = require_resolved(r, "build_c", "test.main.C.__init__");
418447
ASSERT_GTE(idx, 0);
419448
if (idx >= 0) {
420449
const CBMResolvedCall *rc = &r->resolved_calls.items[idx];
421450
ASSERT_STR_EQ(rc->strategy, "lsp_constructor_init");
422-
ASSERT(strstr(rc->callee_qn, "B.__init__") == NULL);
423-
ASSERT(strncmp(rc->callee_qn, "builtins.", 9) != 0);
451+
ASSERT_STR_EQ(rc->reason, "C");
424452
}
425-
ASSERT_EQ(find_resolved(r, "build", "B.__init__"), -1);
453+
ASSERT_EQ(find_resolved(r, "build_c", "test.main.A.__init__"), -1);
454+
ASSERT_EQ(count_build_rows(r, "build_c", "test.main.C", "lsp_constructor"), 0);
426455
cbm_free_result(r);
427456
PASS();
428457
}
@@ -1126,10 +1155,11 @@ TEST(pylsp_crossfile_constructor_targets_init) {
11261155
PASS();
11271156
}
11281157

1129-
/* class B(A) where A's __init__ lives in another module. The cross registry
1130-
* records the real base QN; per-file registration would have stored
1131-
* test.main.A and missed the base. */
1132-
TEST(pylsp_crossfile_inherited_init) {
1158+
/* class B(A) where A's __init__ lives in another module and B declares no
1159+
* __init__ of its own. Construction binds only to an __init__ the class
1160+
* declares itself, so B() stays on the class B and the inherited
1161+
* base_mod.A.__init__ gets no row. */
1162+
TEST(pylsp_crossfile_inherited_init_stays_on_class) {
11331163
const char *source = "from base_mod import A\n"
11341164
"class B(A):\n"
11351165
" def go(self):\n"
@@ -1168,12 +1198,12 @@ TEST(pylsp_crossfile_inherited_init) {
11681198
cbm_run_py_lsp_cross_with_registry(&arena, source, (int)strlen(source), "test.main", reg,
11691199
imp_names, imp_qns, 1, NULL, &out, NULL);
11701200

1171-
int idx = find_resolved_arr(&out, "build", "base_mod.A.__init__");
1201+
ASSERT_EQ(find_resolved_arr(&out, "build", "__init__"), -1);
1202+
int idx = find_resolved_arr(&out, "build", "test.main.B");
11721203
ASSERT_GTE(idx, 0);
11731204
if (idx >= 0) {
1174-
ASSERT_STR_EQ(out.items[idx].strategy, "lsp_constructor_init");
1175-
ASSERT_STR_EQ(out.items[idx].reason, "B");
1176-
ASSERT(strstr(out.items[idx].callee_qn, "B.__init__") == NULL);
1205+
ASSERT_STR_EQ(out.items[idx].callee_qn, "test.main.B");
1206+
ASSERT_STR_EQ(out.items[idx].strategy, "lsp_constructor");
11771207
}
11781208
cbm_arena_destroy(&arena);
11791209
PASS();
@@ -5266,7 +5296,7 @@ SUITE(py_lsp) {
52665296
RUN_TEST(pylsp_method_via_self);
52675297
RUN_TEST(pylsp_constructor_call_returns_instance);
52685298
RUN_TEST(pylsp_constructor_call_targets_init);
5269-
RUN_TEST(pylsp_constructor_inherited_init);
5299+
RUN_TEST(pylsp_constructor_same_file_subclass_own_init_only);
52705300
RUN_TEST(pylsp_constructor_without_init_stays_on_class);
52715301
RUN_TEST(pylsp_callable_instance_call_skips_init);
52725302
RUN_TEST(pylsp_constructor_skips_builtin_init);
@@ -5289,7 +5319,7 @@ SUITE(py_lsp) {
52895319
RUN_TEST(pylsp_crossfile_classmethod_on_class_issue228);
52905320
RUN_TEST(pylsp_crossfile_inheritance);
52915321
RUN_TEST(pylsp_crossfile_constructor_targets_init);
5292-
RUN_TEST(pylsp_crossfile_inherited_init);
5322+
RUN_TEST(pylsp_crossfile_inherited_init_stays_on_class);
52935323
RUN_TEST(pylsp_batch_two_files);
52945324
RUN_TEST(pylsp_from_import_alias_equal_module_leaf_targets_imported_member);
52955325
RUN_TEST(pylsp_project_prefixed_direct_alias_same_tail_is_not_from_import_reference);

0 commit comments

Comments
 (0)