Skip to content

Commit 4123268

Browse files
authored
Merge pull request DeusData#2691 from DeusData/fix/py-nested-class-qn-v3
fix(python): walk a class nested in a class under its outer class
2 parents 27c3303 + c5b6f22 commit 4123268

2 files changed

Lines changed: 96 additions & 1 deletion

File tree

‎internal/cbm/lsp/py_lsp.c‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4949,9 +4949,15 @@ static void py_process_class_in(PyLSPContext *ctx, TSNode class_node, const char
49494949
strcmp(ts_node_type(def), "function_definition") == 0 &&
49504950
!py_is_init_method(ctx, def)) {
49514951
py_process_function(ctx, def, ctx->enclosing_class_qn);
4952+
} else if (!ts_node_is_null(def) &&
4953+
strcmp(ts_node_type(def), "class_definition") == 0) {
4954+
py_process_class_in(ctx, def, ctx->enclosing_class_qn);
49524955
}
49534956
} else if (strcmp(ck, "class_definition") == 0) {
4954-
py_process_class(ctx, c);
4957+
/* A class nested in a class is qualified by its outer class
4958+
* (proj.m.Outer.Inner), the QN extraction gives it; under the
4959+
* module its methods' calls never joined their callers. */
4960+
py_process_class_in(ctx, c, ctx->enclosing_class_qn);
49554961
}
49564962
}
49574963
}

‎tests/test_pipeline.c‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16209,6 +16209,93 @@ TEST(pipeline_python_decorated_nested_class_extracted_parallel) {
1620916209
return pyn_run_decorated_nested(52);
1621016210
}
1621116211

16212+
/* A class nested in a class is walked by the Python LSP under its outer
16213+
* class (m.Outer.Inner): a typed-parameter call, a self call and a
16214+
* constructor call in its methods resolve, and so does a decorated nested
16215+
* class. Walked under the module (m.Inner) none of the LSP rows joined the
16216+
* method nodes, so the calls fell to name guesses or got no edge. */
16217+
static const char pyn_nested_class_py[] = "class Helper:\n"
16218+
" def go(self):\n"
16219+
" return 1\n"
16220+
"\n"
16221+
"def deco(cls):\n"
16222+
" return cls\n"
16223+
"\n"
16224+
"class Outer:\n"
16225+
" class Inner:\n"
16226+
" def run(self, h: Helper):\n"
16227+
" return h.go()\n"
16228+
"\n"
16229+
" def again(self):\n"
16230+
" return self.run(Helper())\n"
16231+
"\n"
16232+
" @deco\n"
16233+
" class Decorated:\n"
16234+
" def run(self, h: Helper):\n"
16235+
" return h.go()\n";
16236+
16237+
static int pyn_run_nested_class(int fillers) {
16238+
char tmp[256];
16239+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_py_nestcls_XXXXXX");
16240+
if (!cbm_mkdtemp(tmp)) {
16241+
FAIL("tmpdir");
16242+
}
16243+
write_temp_file(tmp, "sn.py", pyn_nested_class_py);
16244+
for (int i = 0; i < fillers; i++) {
16245+
char name[64];
16246+
char body[128];
16247+
snprintf(name, sizeof(name), "filler%d.py", i);
16248+
snprintf(body, sizeof(body), "def filler%d():\n return %d\n", i, i);
16249+
write_temp_file(tmp, name, body);
16250+
}
16251+
char *old_workers = getenv("CBM_WORKERS");
16252+
char *saved = old_workers ? strdup(old_workers) : NULL;
16253+
if (fillers > 0) {
16254+
cbm_setenv("CBM_WORKERS", "4", 1);
16255+
}
16256+
char db_path[512];
16257+
snprintf(db_path, sizeof(db_path), "%s/nestcls.db", tmp);
16258+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
16259+
int rc = p ? cbm_pipeline_run(p) : -1;
16260+
cbm_store_t *s = rc == 0 ? cbm_store_open_path(db_path) : NULL;
16261+
const char *project = p ? cbm_pipeline_project_name(p) : NULL;
16262+
bool typed = s && pyn_edge_with(s, project, "sn.Outer.Inner.run", "sn.Helper.go", "CALLS",
16263+
"\"strategy\":\"lsp_");
16264+
bool self_call = s && pyn_edge_with(s, project, "sn.Outer.Inner.again", "sn.Outer.Inner.run",
16265+
"CALLS", "\"strategy\":\"lsp_");
16266+
bool ctor = s && pyn_edge_with(s, project, "sn.Outer.Inner.again", "sn.Helper", "CALLS",
16267+
"\"strategy\":\"lsp_");
16268+
bool decorated = s && pyn_edge_with(s, project, "sn.Outer.Decorated.run", "sn.Helper.go",
16269+
"CALLS", "\"strategy\":\"lsp_");
16270+
if (s) {
16271+
cbm_store_close(s);
16272+
}
16273+
if (p) {
16274+
cbm_pipeline_free(p);
16275+
}
16276+
if (saved) {
16277+
cbm_setenv("CBM_WORKERS", saved, 1);
16278+
free(saved);
16279+
} else {
16280+
cbm_unsetenv("CBM_WORKERS");
16281+
}
16282+
th_rmtree(tmp);
16283+
ASSERT_EQ(rc, 0);
16284+
ASSERT_TRUE(typed);
16285+
ASSERT_TRUE(self_call);
16286+
ASSERT_TRUE(ctor);
16287+
ASSERT_TRUE(decorated);
16288+
PASS();
16289+
}
16290+
16291+
TEST(pipeline_python_nested_class_methods_resolve) {
16292+
return pyn_run_nested_class(0);
16293+
}
16294+
16295+
TEST(pipeline_python_nested_class_methods_resolve_parallel) {
16296+
return pyn_run_nested_class(52);
16297+
}
16298+
1621216299
TEST(pipeline_python_project) {
1621316300
/* Port of TestPipelinePythonProject */
1621416301
const char *files[] = {"main.py", "utils.py"};
@@ -25738,6 +25825,8 @@ SUITE(pipeline) {
2573825825
RUN_TEST(pipeline_python_nested_helper_keeps_tests_parallel_issue1548);
2573925826
RUN_TEST(pipeline_python_decorated_nested_class_extracted);
2574025827
RUN_TEST(pipeline_python_decorated_nested_class_extracted_parallel);
25828+
RUN_TEST(pipeline_python_nested_class_methods_resolve);
25829+
RUN_TEST(pipeline_python_nested_class_methods_resolve_parallel);
2574125830
RUN_TEST(pipeline_header_include_target_is_independent_of_registration_order);
2574225831
RUN_TEST(pipeline_imports_multi_symbol_edges);
2574325832
RUN_TEST(pipeline_go_cross_package_call);

0 commit comments

Comments
 (0)