@@ -13666,8 +13666,17 @@ TEST(pipeline_python_init_import_probe_failure_falls_back) {
1366613666/* #1277: typed instance fields of a Python class imported from another file.
1366713667 * Counts CALLS edges caller -> callee (QN suffixes) whose properties name an
1366813668 * LSP strategy. */
13669+ static int count_calls_with(cbm_store_t *s, const char *project, const char *caller_suffix,
13670+ const char *callee_suffix, const char *fragment);
13671+
1366913672static int count_lsp_calls(cbm_store_t *s, const char *project, const char *caller_suffix,
1367013673 const char *callee_suffix) {
13674+ return count_calls_with(s, project, caller_suffix, callee_suffix, "lsp");
13675+ }
13676+
13677+ /* As count_lsp_calls, with any properties fragment (NULL = every CALLS edge). */
13678+ static int count_calls_with(cbm_store_t *s, const char *project, const char *caller_suffix,
13679+ const char *callee_suffix, const char *fragment) {
1367113680 cbm_edge_t *edges = NULL;
1367213681 int edge_count = 0;
1367313682 cbm_store_find_edges_by_type(s, project, "CALLS", &edges, &edge_count);
@@ -13683,7 +13692,8 @@ static int count_lsp_calls(cbm_store_t *s, const char *project, const char *call
1368313692 size_t ts = strlen(callee_suffix);
1368413693 if (sl >= cs && tl >= ts && strcmp(src.qualified_name + sl - cs, caller_suffix) == 0 &&
1368513694 strcmp(tgt.qualified_name + tl - ts, callee_suffix) == 0 &&
13686- edges[i].properties_json && strstr(edges[i].properties_json, "lsp")) {
13695+ (!fragment ||
13696+ (edges[i].properties_json && strstr(edges[i].properties_json, fragment)))) {
1368713697 hits++;
1368813698 }
1368913699 }
@@ -13802,6 +13812,110 @@ TEST(python_crossfile_typed_field_calls_issue1277_parallel) {
1380213812 return check_python_crossfile_typed_field_calls_issue1277(true);
1380313813}
1380413814
13815+ /* #1277, second half: the reporter's chain with both annotation imports
13816+ * under `if TYPE_CHECKING:` (the usual shape when the import would cycle).
13817+ * trainer.strategies is typed through a TYPE_CHECKING import of Contract, and
13818+ * run()'s parameter through a typing.TYPE_CHECKING import of Trainer; the call
13819+ * resolves by the LSP exactly as with runtime imports. A runtime call through
13820+ * a TYPE_CHECKING-only name cannot happen: it gets no CALLS edge at all. */
13821+ static int check_python_type_checking_typed_field_issue1277(bool parallel) {
13822+ const char *contracts = "class Contract:\n"
13823+ " def process_batch(self) -> None:\n"
13824+ " ...\n";
13825+ const char *trainer = "from __future__ import annotations\n"
13826+ "from typing import TYPE_CHECKING\n\n"
13827+ "if TYPE_CHECKING:\n"
13828+ " from contracts import Contract\n\n"
13829+ "class Trainer:\n"
13830+ " def __init__(self, strategies: Contract) -> None:\n"
13831+ " self.strategies = strategies\n";
13832+ const char *loop = "from __future__ import annotations\n"
13833+ "import typing\n\n"
13834+ "if typing.TYPE_CHECKING:\n"
13835+ " from trainer import Trainer\n\n"
13836+ "def run(trainer: Trainer) -> None:\n"
13837+ " trainer.strategies.process_batch()\n\n"
13838+ "def bad():\n"
13839+ " return Trainer()\n";
13840+ if (setup_usages_repo("contracts.py", contracts, "trainer.py", trainer) != 0) {
13841+ FAIL("failed to create temp dir");
13842+ }
13843+ char path[512];
13844+ snprintf(path, sizeof(path), "%s/loop.py", g_usages_tmpdir);
13845+ if (th_write_file(path, loop) != 0) {
13846+ teardown_usages_repo();
13847+ FAIL("failed to write loop.py");
13848+ }
13849+ if (parallel) {
13850+ for (int i = 0; i < 50; i++) {
13851+ snprintf(path, sizeof(path), "%s/tc_pad_%02d.py", g_usages_tmpdir, i);
13852+ if (th_write_file(path, "tc_padding = 0\n") != 0) {
13853+ teardown_usages_repo();
13854+ FAIL("failed to write parallel-selection fixture");
13855+ }
13856+ }
13857+ }
13858+
13859+ const char *old_workers = getenv("CBM_WORKERS");
13860+ char *saved_workers = old_workers ? strdup(old_workers) : NULL;
13861+ const char *old_single = getenv("CBM_INDEX_SINGLE_THREAD");
13862+ char *saved_single = old_single ? strdup(old_single) : NULL;
13863+ if ((old_workers && !saved_workers) || (old_single && !saved_single)) {
13864+ free(saved_workers);
13865+ free(saved_single);
13866+ teardown_usages_repo();
13867+ FAIL("failed to save pipeline environment");
13868+ }
13869+ int pin_workers_rc = parallel ? cbm_setenv("CBM_WORKERS", "4", 1) : 0;
13870+ int pin_single_rc = parallel ? cbm_unsetenv("CBM_INDEX_SINGLE_THREAD") : 0;
13871+
13872+ char db_path[512];
13873+ snprintf(db_path, sizeof(db_path), "%s/test_1277_tc.db", g_usages_tmpdir);
13874+ cbm_pipeline_t *p = pin_workers_rc == 0 && pin_single_rc == 0
13875+ ? cbm_pipeline_new(g_usages_tmpdir, db_path, CBM_MODE_FULL)
13876+ : NULL;
13877+ int run_rc = p ? cbm_pipeline_run(p) : -1;
13878+ cbm_store_t *s = run_rc == 0 ? cbm_store_open_path(db_path) : NULL;
13879+ bool store_opened = s != NULL;
13880+ int via_field = -1, runtime_import = -1, runtime_lsp = -1;
13881+ if (s) {
13882+ const char *project = cbm_pipeline_project_name(p);
13883+ via_field = count_lsp_calls(s, project, "loop.run", "contracts.Contract.process_batch");
13884+ runtime_import = count_calls_with(s, project, "loop.bad", "trainer.Trainer",
13885+ "\"strategy\":\"import_map");
13886+ runtime_lsp = count_lsp_calls(s, project, "loop.bad", "trainer.Trainer");
13887+ cbm_store_close(s);
13888+ }
13889+
13890+ cbm_pipeline_free(p);
13891+ int restore_workers_rc =
13892+ saved_workers ? cbm_setenv("CBM_WORKERS", saved_workers, 1) : cbm_unsetenv("CBM_WORKERS");
13893+ int restore_single_rc = saved_single ? cbm_setenv("CBM_INDEX_SINGLE_THREAD", saved_single, 1)
13894+ : cbm_unsetenv("CBM_INDEX_SINGLE_THREAD");
13895+ free(saved_workers);
13896+ free(saved_single);
13897+ teardown_usages_repo();
13898+
13899+ ASSERT_EQ(pin_workers_rc, 0);
13900+ ASSERT_EQ(pin_single_rc, 0);
13901+ ASSERT_EQ(restore_workers_rc, 0);
13902+ ASSERT_EQ(restore_single_rc, 0);
13903+ ASSERT_EQ(run_rc, 0);
13904+ ASSERT_TRUE(store_opened);
13905+ ASSERT_EQ(via_field, 1); /* annotation through TYPE_CHECKING imports */
13906+ ASSERT_EQ(runtime_import, 0); /* control: no CALLS import map binds the name */
13907+ ASSERT_EQ(runtime_lsp, 0); /* control: the LSP binds it for annotations only */
13908+ PASS();
13909+ }
13910+
13911+ TEST(python_type_checking_typed_field_issue1277) {
13912+ return check_python_type_checking_typed_field_issue1277(false);
13913+ }
13914+
13915+ TEST(python_type_checking_typed_field_issue1277_parallel) {
13916+ return check_python_type_checking_typed_field_issue1277(true);
13917+ }
13918+
1380513919/* A base class spelled through a root-binding import (`import pkg.mod.nodes`
1380613920 * binds `pkg`; `class X(pkg.mod.nodes.Context)`) is the same class as the
1380713921 * from-imported spelling. The Python LSP's inheritance view never resolved the
@@ -24798,6 +24912,8 @@ SUITE(pipeline) {
2479824912 RUN_TEST(override_python_through_intermediate_class);
2479924913 RUN_TEST(python_crossfile_typed_field_calls_issue1277);
2480024914 RUN_TEST(python_crossfile_typed_field_calls_issue1277_parallel);
24915+ RUN_TEST(python_type_checking_typed_field_issue1277);
24916+ RUN_TEST(python_type_checking_typed_field_issue1277_parallel);
2480124917 RUN_TEST(python_dotted_base_inheritance);
2480224918 RUN_TEST(python_dotted_base_inheritance_parallel);
2480324919 RUN_TEST(python_typed_field_external_import_stays_unresolved);
0 commit comments