Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

test(python): annotations resolve through TYPE_CHECKING imports (#1277)

Martin Vogel257aea137950
diff --git a/internal/cbm/cbm.h b/internal/cbm/cbm.h
index 86586dd15..9c736d67b 100644
--- a/internal/cbm/cbm.h
+++ b/internal/cbm/cbm.h
@@ -398,6 +398,8 @@ typedef struct {
const char *member; // ES named import: the exported name the local binds (`get` in
// `import { get as navigate } from "axios"`); NULL otherwise
bool is_default; // ES default import (`import X from "Y"`), JS/TS only (#1916)
+ bool type_checking_only; // Python: inside a module-level `if TYPE_CHECKING:` block --
+ // binds the name for annotations, never at runtime (#1277)
CBMImportKind kind; // CBM_IMPORT_KIND_DEFAULT unless the syntax names a member kind
} CBMImport;
diff --git a/tests/test_extraction.c b/tests/test_extraction.c
index 5f46a416d..fcaa3655f 100644
--- a/tests/test_extraction.c
+++ b/tests/test_extraction.c
@@ -1506,6 +1506,50 @@ TEST(python_annotated_instance_fields_exported_issue1277) {
PASS();
}
+/* #1277: the import an annotated field's type comes from usually sits under
+ * `if TYPE_CHECKING:`. It is recorded, flagged annotation-only; a runtime
+ * import is not flagged; an else branch (a runtime alternative) and an
+ * ordinary `if` stay unrecorded as before. */
+static const CBMImport *py_find_import(const CBMFileResult *r, const char *local) {
+ for (int i = 0; i < r->imports.count; i++) {
+ if (r->imports.items[i].local_name && strcmp(r->imports.items[i].local_name, local) == 0)
+ return &r->imports.items[i];
+ }
+ return NULL;
+}
+
+TEST(python_type_checking_imports_flagged_issue1277) {
+ const char *src = "from typing import TYPE_CHECKING\n"
+ "import typing\n"
+ "if TYPE_CHECKING:\n"
+ " from trainer import Trainer\n"
+ " import contracts\n"
+ "else:\n"
+ " from fallback import Runtime\n"
+ "if typing.TYPE_CHECKING:\n"
+ " from engine import Engine as Eng\n"
+ "if FLAG:\n"
+ " from other import Conditional\n";
+ CBMFileResult *r = extract(src, CBM_LANG_PYTHON, "t", "loop.py");
+ ASSERT_NOT_NULL(r);
+ const CBMImport *tc = py_find_import(r, "TYPE_CHECKING");
+ const CBMImport *trainer = py_find_import(r, "Trainer");
+ const CBMImport *contracts = py_find_import(r, "contracts");
+ const CBMImport *eng = py_find_import(r, "Eng");
+ ASSERT_NOT_NULL(tc);
+ ASSERT_FALSE(tc->type_checking_only);
+ ASSERT_NOT_NULL(trainer);
+ ASSERT_TRUE(trainer->type_checking_only);
+ ASSERT_NOT_NULL(contracts);
+ ASSERT_TRUE(contracts->type_checking_only);
+ ASSERT_NOT_NULL(eng);
+ ASSERT_TRUE(eng->type_checking_only);
+ ASSERT_NULL(py_find_import(r, "Runtime"));
+ ASSERT_NULL(py_find_import(r, "Conditional"));
+ cbm_free_result(r);
+ PASS();
+}
+
TEST(python_function) {
CBMFileResult *r = extract(
"def greet(name):\n return f\"Hello {name}\"\n\ndef main():\n greet(\"World\")\n",
@@ -12625,6 +12669,7 @@ SUITE(extraction) {
RUN_TEST(python_function);
RUN_TEST(python_class);
RUN_TEST(python_annotated_instance_fields_exported_issue1277);
+ RUN_TEST(python_type_checking_imports_flagged_issue1277);
RUN_TEST(js_function);
RUN_TEST(js_class);
RUN_TEST(ts_function);
diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c
index 4f8c656ad..c8578430e 100644
--- a/tests/test_pipeline.c
+++ b/tests/test_pipeline.c
@@ -13666,8 +13666,17 @@ TEST(pipeline_python_init_import_probe_failure_falls_back) {
/* #1277: typed instance fields of a Python class imported from another file.
* Counts CALLS edges caller -> callee (QN suffixes) whose properties name an
* LSP strategy. */
+static int count_calls_with(cbm_store_t *s, const char *project, const char *caller_suffix,
+ const char *callee_suffix, const char *fragment);
+
static int count_lsp_calls(cbm_store_t *s, const char *project, const char *caller_suffix,
const char *callee_suffix) {
+ return count_calls_with(s, project, caller_suffix, callee_suffix, "lsp");
+}
+
+/* As count_lsp_calls, with any properties fragment (NULL = every CALLS edge). */
+static int count_calls_with(cbm_store_t *s, const char *project, const char *caller_suffix,
+ const char *callee_suffix, const char *fragment) {
cbm_edge_t *edges = NULL;
int edge_count = 0;
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
size_t ts = strlen(callee_suffix);
if (sl >= cs && tl >= ts && strcmp(src.qualified_name + sl - cs, caller_suffix) == 0 &&
strcmp(tgt.qualified_name + tl - ts, callee_suffix) == 0 &&
- edges[i].properties_json && strstr(edges[i].properties_json, "lsp")) {
+ (!fragment ||
+ (edges[i].properties_json && strstr(edges[i].properties_json, fragment)))) {
hits++;
}
}
@@ -13802,6 +13812,110 @@ TEST(python_crossfile_typed_field_calls_issue1277_parallel) {
return check_python_crossfile_typed_field_calls_issue1277(true);
}
+/* #1277, second half: the reporter's chain with both annotation imports
+ * under `if TYPE_CHECKING:` (the usual shape when the import would cycle).
+ * trainer.strategies is typed through a TYPE_CHECKING import of Contract, and
+ * run()'s parameter through a typing.TYPE_CHECKING import of Trainer; the call
+ * resolves by the LSP exactly as with runtime imports. A runtime call through
+ * a TYPE_CHECKING-only name cannot happen: it gets no CALLS edge at all. */
+static int check_python_type_checking_typed_field_issue1277(bool parallel) {
+ const char *contracts = "class Contract:\n"
+ " def process_batch(self) -> None:\n"
+ " ...\n";
+ const char *trainer = "from __future__ import annotations\n"
+ "from typing import TYPE_CHECKING\n\n"
+ "if TYPE_CHECKING:\n"
+ " from contracts import Contract\n\n"
+ "class Trainer:\n"
+ " def __init__(self, strategies: Contract) -> None:\n"
+ " self.strategies = strategies\n";
+ const char *loop = "from __future__ import annotations\n"
+ "import typing\n\n"
+ "if typing.TYPE_CHECKING:\n"
+ " from trainer import Trainer\n\n"
+ "def run(trainer: Trainer) -> None:\n"
+ " trainer.strategies.process_batch()\n\n"
+ "def bad():\n"
+ " return Trainer()\n";
+ if (setup_usages_repo("contracts.py", contracts, "trainer.py", trainer) != 0) {
+ FAIL("failed to create temp dir");
+ }
+ char path[512];
+ snprintf(path, sizeof(path), "%s/loop.py", g_usages_tmpdir);
+ if (th_write_file(path, loop) != 0) {
+ teardown_usages_repo();
+ FAIL("failed to write loop.py");
+ }
+ if (parallel) {
+ for (int i = 0; i < 50; i++) {
+ snprintf(path, sizeof(path), "%s/tc_pad_%02d.py", g_usages_tmpdir, i);
+ if (th_write_file(path, "tc_padding = 0\n") != 0) {
+ teardown_usages_repo();
+ FAIL("failed to write parallel-selection fixture");
+ }
+ }
+ }
+
+ const char *old_workers = getenv("CBM_WORKERS");
+ char *saved_workers = old_workers ? strdup(old_workers) : NULL;
+ const char *old_single = getenv("CBM_INDEX_SINGLE_THREAD");
+ char *saved_single = old_single ? strdup(old_single) : NULL;
+ if ((old_workers && !saved_workers) || (old_single && !saved_single)) {
+ free(saved_workers);
+ free(saved_single);
+ teardown_usages_repo();
+ FAIL("failed to save pipeline environment");
+ }
+ int pin_workers_rc = parallel ? cbm_setenv("CBM_WORKERS", "4", 1) : 0;
+ int pin_single_rc = parallel ? cbm_unsetenv("CBM_INDEX_SINGLE_THREAD") : 0;
+
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/test_1277_tc.db", g_usages_tmpdir);
+ cbm_pipeline_t *p = pin_workers_rc == 0 && pin_single_rc == 0
+ ? cbm_pipeline_new(g_usages_tmpdir, db_path, CBM_MODE_FULL)
+ : NULL;
+ int run_rc = p ? cbm_pipeline_run(p) : -1;
+ cbm_store_t *s = run_rc == 0 ? cbm_store_open_path(db_path) : NULL;
+ bool store_opened = s != NULL;
+ int via_field = -1, runtime_import = -1, runtime_lsp = -1;
+ if (s) {
+ const char *project = cbm_pipeline_project_name(p);
+ via_field = count_lsp_calls(s, project, "loop.run", "contracts.Contract.process_batch");
+ runtime_import = count_calls_with(s, project, "loop.bad", "trainer.Trainer",
+ "\"strategy\":\"import_map");
+ runtime_lsp = count_lsp_calls(s, project, "loop.bad", "trainer.Trainer");
+ cbm_store_close(s);
+ }
+
+ cbm_pipeline_free(p);
+ int restore_workers_rc =
+ saved_workers ? cbm_setenv("CBM_WORKERS", saved_workers, 1) : cbm_unsetenv("CBM_WORKERS");
+ int restore_single_rc = saved_single ? cbm_setenv("CBM_INDEX_SINGLE_THREAD", saved_single, 1)
+ : cbm_unsetenv("CBM_INDEX_SINGLE_THREAD");
+ free(saved_workers);
+ free(saved_single);
+ teardown_usages_repo();
+
+ ASSERT_EQ(pin_workers_rc, 0);
+ ASSERT_EQ(pin_single_rc, 0);
+ ASSERT_EQ(restore_workers_rc, 0);
+ ASSERT_EQ(restore_single_rc, 0);
+ ASSERT_EQ(run_rc, 0);
+ ASSERT_TRUE(store_opened);
+ ASSERT_EQ(via_field, 1); /* annotation through TYPE_CHECKING imports */
+ ASSERT_EQ(runtime_import, 0); /* control: no CALLS import map binds the name */
+ ASSERT_EQ(runtime_lsp, 0); /* control: the LSP binds it for annotations only */
+ PASS();
+}
+
+TEST(python_type_checking_typed_field_issue1277) {
+ return check_python_type_checking_typed_field_issue1277(false);
+}
+
+TEST(python_type_checking_typed_field_issue1277_parallel) {
+ return check_python_type_checking_typed_field_issue1277(true);
+}
+
/* A base class spelled through a root-binding import (`import pkg.mod.nodes`
* binds `pkg`; `class X(pkg.mod.nodes.Context)`) is the same class as the
* from-imported spelling. The Python LSP's inheritance view never resolved the
@@ -24798,6 +24912,8 @@ SUITE(pipeline) {
RUN_TEST(override_python_through_intermediate_class);
RUN_TEST(python_crossfile_typed_field_calls_issue1277);
RUN_TEST(python_crossfile_typed_field_calls_issue1277_parallel);
+ RUN_TEST(python_type_checking_typed_field_issue1277);
+ RUN_TEST(python_type_checking_typed_field_issue1277_parallel);
RUN_TEST(python_dotted_base_inheritance);
RUN_TEST(python_dotted_base_inheritance_parallel);
RUN_TEST(python_typed_field_external_import_stays_unresolved);