Skip to content

Commit 257aea1

Browse files
committed
test(python): annotations resolve through TYPE_CHECKING imports (DeusData#1277)
The second half of DeusData#1277: the reporter's typed-field chain with both annotation imports under `if TYPE_CHECKING:` (the usual shape when the import would cycle), through `typing.TYPE_CHECKING` too. The call resolves by the LSP as with runtime imports, and a runtime call through a TYPE_CHECKING-only name gets no CALLS edge at all. The extraction test reads the new CBMImport.type_checking_only flag, declared here and not yet set. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 1346ac4 commit 257aea1

3 files changed

Lines changed: 164 additions & 1 deletion

File tree

‎internal/cbm/cbm.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,8 @@ typedef struct {
398398
const char *member; // ES named import: the exported name the local binds (`get` in
399399
// `import { get as navigate } from "axios"`); NULL otherwise
400400
bool is_default; // ES default import (`import X from "Y"`), JS/TS only (#1916)
401+
bool type_checking_only; // Python: inside a module-level `if TYPE_CHECKING:` block --
402+
// binds the name for annotations, never at runtime (#1277)
401403
CBMImportKind kind; // CBM_IMPORT_KIND_DEFAULT unless the syntax names a member kind
402404
} CBMImport;
403405

‎tests/test_extraction.c‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,6 +1506,50 @@ TEST(python_annotated_instance_fields_exported_issue1277) {
15061506
PASS();
15071507
}
15081508

1509+
/* #1277: the import an annotated field's type comes from usually sits under
1510+
* `if TYPE_CHECKING:`. It is recorded, flagged annotation-only; a runtime
1511+
* import is not flagged; an else branch (a runtime alternative) and an
1512+
* ordinary `if` stay unrecorded as before. */
1513+
static const CBMImport *py_find_import(const CBMFileResult *r, const char *local) {
1514+
for (int i = 0; i < r->imports.count; i++) {
1515+
if (r->imports.items[i].local_name && strcmp(r->imports.items[i].local_name, local) == 0)
1516+
return &r->imports.items[i];
1517+
}
1518+
return NULL;
1519+
}
1520+
1521+
TEST(python_type_checking_imports_flagged_issue1277) {
1522+
const char *src = "from typing import TYPE_CHECKING\n"
1523+
"import typing\n"
1524+
"if TYPE_CHECKING:\n"
1525+
" from trainer import Trainer\n"
1526+
" import contracts\n"
1527+
"else:\n"
1528+
" from fallback import Runtime\n"
1529+
"if typing.TYPE_CHECKING:\n"
1530+
" from engine import Engine as Eng\n"
1531+
"if FLAG:\n"
1532+
" from other import Conditional\n";
1533+
CBMFileResult *r = extract(src, CBM_LANG_PYTHON, "t", "loop.py");
1534+
ASSERT_NOT_NULL(r);
1535+
const CBMImport *tc = py_find_import(r, "TYPE_CHECKING");
1536+
const CBMImport *trainer = py_find_import(r, "Trainer");
1537+
const CBMImport *contracts = py_find_import(r, "contracts");
1538+
const CBMImport *eng = py_find_import(r, "Eng");
1539+
ASSERT_NOT_NULL(tc);
1540+
ASSERT_FALSE(tc->type_checking_only);
1541+
ASSERT_NOT_NULL(trainer);
1542+
ASSERT_TRUE(trainer->type_checking_only);
1543+
ASSERT_NOT_NULL(contracts);
1544+
ASSERT_TRUE(contracts->type_checking_only);
1545+
ASSERT_NOT_NULL(eng);
1546+
ASSERT_TRUE(eng->type_checking_only);
1547+
ASSERT_NULL(py_find_import(r, "Runtime"));
1548+
ASSERT_NULL(py_find_import(r, "Conditional"));
1549+
cbm_free_result(r);
1550+
PASS();
1551+
}
1552+
15091553
TEST(python_function) {
15101554
CBMFileResult *r = extract(
15111555
"def greet(name):\n return f\"Hello {name}\"\n\ndef main():\n greet(\"World\")\n",
@@ -12625,6 +12669,7 @@ SUITE(extraction) {
1262512669
RUN_TEST(python_function);
1262612670
RUN_TEST(python_class);
1262712671
RUN_TEST(python_annotated_instance_fields_exported_issue1277);
12672+
RUN_TEST(python_type_checking_imports_flagged_issue1277);
1262812673
RUN_TEST(js_function);
1262912674
RUN_TEST(js_class);
1263012675
RUN_TEST(ts_function);

‎tests/test_pipeline.c‎

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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+
1366913672
static 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

Comments
 (0)