Skip to content

Commit e6b47a3

Browse files
authored
Merge pull request DeusData#2700 from DeusData/fix/py-type-checking-imports-v2
fix(python): resolve annotations through TYPE_CHECKING imports (DeusData#1277)
2 parents 4527fc7 + 3ead775 commit e6b47a3

11 files changed

Lines changed: 511 additions & 27 deletions

File tree

‎internal/cbm/cbm.c‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,31 @@ void cbm_imports_push(CBMImportArray *arr, CBMArena *a, CBMImport imp) {
402402
arr->items[arr->count++] = imp;
403403
}
404404

405+
bool cbm_imports_local_span_binds_runtime_name(const CBMImportArray *imports, const char *name,
406+
size_t len) {
407+
if (!imports || !name) {
408+
return true;
409+
}
410+
bool seen = false;
411+
for (int i = 0; i < imports->count; i++) {
412+
const CBMImport *imp = &imports->items[i];
413+
if (!imp->local_name || strncmp(imp->local_name, name, len) != 0 ||
414+
imp->local_name[len] != '\0') {
415+
continue;
416+
}
417+
if (cbm_import_binds_runtime_name(imp)) {
418+
return true;
419+
}
420+
seen = true;
421+
}
422+
return !seen;
423+
}
424+
425+
bool cbm_imports_local_binds_runtime_name(const CBMImportArray *imports, const char *local_name) {
426+
return !local_name ||
427+
cbm_imports_local_span_binds_runtime_name(imports, local_name, strlen(local_name));
428+
}
429+
405430
void cbm_usages_push(CBMUsageArray *arr, CBMArena *a, CBMUsage usage) {
406431
GROW_ARRAY(arr, a);
407432
arr->items[arr->count++] = usage;

‎internal/cbm/cbm.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,20 @@ 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

406+
/* True when an import binds its local name at runtime. A Python import under
407+
* `if TYPE_CHECKING:` exists for annotations only: a call through that name
408+
* cannot happen, so no CALLS import map may bind it (an import_map edge
409+
* through it would be fabricated by construction). THE one rule every CALLS
410+
* import-map builder applies. */
411+
static inline bool cbm_import_binds_runtime_name(const CBMImport *imp) {
412+
return imp && !imp->type_checking_only;
413+
}
414+
404415
/* Content-only ordered Python namespace facts; no resolved target identities. */
405416
#define CBM_PY_NAMESPACE_FACTS_VERSION 1u
406417

@@ -1306,6 +1317,14 @@ void cbm_py_namespace_test_copy_fail_after(int successful_allocations);
13061317
void cbm_defs_push(CBMDefArray *arr, CBMArena *a, CBMDefinition def);
13071318
void cbm_calls_push(CBMCallArray *arr, CBMArena *a, CBMCall call);
13081319
void cbm_imports_push(CBMImportArray *arr, CBMArena *a, CBMImport imp);
1320+
/* True unless every import of `imports` that binds `local_name` is
1321+
* annotation-only (cbm_import_binds_runtime_name), so also true when no import
1322+
* names it: the rule for the builders that key IMPORTS edges by local name. */
1323+
bool cbm_imports_local_binds_runtime_name(const CBMImportArray *imports, const char *local_name);
1324+
/* The same for the `len` bytes at `name` (not NUL-terminated): lets a caller
1325+
* decide before it copies the name. */
1326+
bool cbm_imports_local_span_binds_runtime_name(const CBMImportArray *imports, const char *name,
1327+
size_t len);
13091328
void cbm_usages_push(CBMUsageArray *arr, CBMArena *a, CBMUsage usage);
13101329
void cbm_throws_push(CBMThrowArray *arr, CBMArena *a, CBMThrow thr);
13111330
void cbm_rw_push(CBMRWArray *arr, CBMArena *a, CBMReadWrite rw);

‎internal/cbm/extract_imports.c‎

Lines changed: 137 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,139 @@ static void process_py_import_from(CBMExtractCtx *ctx, TSNode node) {
334334
}
335335
}
336336

337+
/* `TYPE_CHECKING` or `<module>.TYPE_CHECKING` (typing.TYPE_CHECKING, t.TYPE_CHECKING). */
338+
static bool py_is_type_checking_condition(CBMExtractCtx *ctx, TSNode cond) {
339+
if (ts_node_is_null(cond)) {
340+
return false;
341+
}
342+
TSNode name = cond;
343+
if (strcmp(ts_node_type(cond), "attribute") == 0) {
344+
name = ts_node_child_by_field_name(cond, TS_FIELD("attribute"));
345+
}
346+
if (ts_node_is_null(name) || strcmp(ts_node_type(name), "identifier") != 0) {
347+
return false;
348+
}
349+
const char *text = cbm_node_text(ctx->arena, name, ctx->source);
350+
return text && strcmp(text, "TYPE_CHECKING") == 0;
351+
}
352+
353+
/* One import statement; `type_only` flags every import it records. */
354+
static void process_py_import_node(CBMExtractCtx *ctx, TSNode node, bool type_only) {
355+
const char *kind = ts_node_type(node);
356+
int before = ctx->result->imports.count;
357+
if (strcmp(kind, "import_statement") == 0) {
358+
process_py_import_stmt(ctx, node);
359+
} else if (strcmp(kind, "import_from_statement") == 0 ||
360+
strcmp(kind, "future_import_statement") == 0) {
361+
// `from __future__ import annotations` is a distinct node type in
362+
// tree-sitter-python but has the same shape (module + name list).
363+
process_py_import_from(ctx, node);
364+
}
365+
for (int i = before; type_only && i < ctx->result->imports.count; i++) {
366+
ctx->result->imports.items[i].type_checking_only = true;
367+
}
368+
}
369+
370+
static bool py_is_import_node(TSNode node) {
371+
const char *kind = ts_node_type(node);
372+
return strcmp(kind, "import_statement") == 0 || strcmp(kind, "import_from_statement") == 0 ||
373+
strcmp(kind, "future_import_statement") == 0;
374+
}
375+
376+
static bool py_import_is_wildcard(TSNode import_node) {
377+
uint32_t n = ts_node_child_count(import_node);
378+
for (uint32_t i = 0; i < n; i++) {
379+
if (strcmp(ts_node_type(ts_node_child(import_node, i)), "wildcard_import") == 0) {
380+
return true;
381+
}
382+
}
383+
return false;
384+
}
385+
386+
/* Records, unflagged, every import statement anywhere under `root` (the
387+
* runtime alternative of a TYPE_CHECKING block usually nests them in
388+
* try/except ImportError). An iterative pre-order walk: adversarially deep
389+
* nesting must not exhaust the stack. Returns true when one of them is a star
390+
* import. */
391+
static bool py_record_subtree_imports(CBMExtractCtx *ctx, TSNode root) {
392+
bool star = false;
393+
TSTreeCursor cursor = ts_tree_cursor_new(root);
394+
for (;;) {
395+
TSNode node = ts_tree_cursor_current_node(&cursor);
396+
bool is_import = py_is_import_node(node);
397+
if (is_import) {
398+
process_py_import_node(ctx, node, false);
399+
star = star || py_import_is_wildcard(node);
400+
}
401+
if (!is_import && ts_tree_cursor_goto_first_child(&cursor)) {
402+
continue;
403+
}
404+
while (!ts_tree_cursor_goto_next_sibling(&cursor)) {
405+
if (!ts_tree_cursor_goto_parent(&cursor)) {
406+
ts_tree_cursor_delete(&cursor);
407+
return star;
408+
}
409+
}
410+
}
411+
}
412+
413+
/* Drops the imports [first, mark) whose local name an import at [mark, count)
414+
* binds as well (every one when `star`), then the imports [mark, count). */
415+
static void py_drop_rebound_imports(CBMImportArray *imports, int first, int mark, bool star) {
416+
int keep = first;
417+
for (int i = first; i < mark; i++) {
418+
const CBMImport *imp = &imports->items[i];
419+
bool rebound = star;
420+
for (int j = mark; !rebound && j < imports->count; j++) {
421+
rebound = imp->local_name && imports->items[j].local_name &&
422+
strcmp(imp->local_name, imports->items[j].local_name) == 0;
423+
}
424+
if (!rebound) {
425+
imports->items[keep++] = *imp;
426+
}
427+
}
428+
imports->count = keep;
429+
}
430+
431+
/* A module-level `if TYPE_CHECKING:` holds imports a type checker sees and
432+
* the interpreter never runs (#1277: an annotated field's type is usually
433+
* imported exactly there, to avoid an import cycle). Record the imports of
434+
* its consequence block, flagged type_checking_only. Its elif/else branches
435+
* are runtime alternatives and stay unrecorded, as every other compound
436+
* statement's imports do; a name they bind again (the compat shape: the same
437+
* name imported in `else:` under try/except ImportError) is bound at runtime,
438+
* so its TYPE_CHECKING import is no annotation-only import and is dropped as
439+
* well, and a star import there may bind any name. */
440+
static void process_py_type_checking_block(CBMExtractCtx *ctx, TSNode if_node) {
441+
if (!py_is_type_checking_condition(
442+
ctx, ts_node_child_by_field_name(if_node, TS_FIELD("condition")))) {
443+
return;
444+
}
445+
TSNode body = ts_node_child_by_field_name(if_node, TS_FIELD("consequence"));
446+
if (ts_node_is_null(body)) {
447+
return;
448+
}
449+
int first = ctx->result->imports.count;
450+
TSTreeCursor cursor = ts_tree_cursor_new(body);
451+
if (ts_tree_cursor_goto_first_child(&cursor)) {
452+
do {
453+
process_py_import_node(ctx, ts_tree_cursor_current_node(&cursor), true);
454+
} while (ts_tree_cursor_goto_next_sibling(&cursor));
455+
}
456+
ts_tree_cursor_delete(&cursor);
457+
int mark = ctx->result->imports.count;
458+
bool star = false;
459+
uint32_t n = ts_node_child_count(if_node);
460+
for (uint32_t i = 0; i < n; i++) {
461+
TSNode alt = ts_node_child(if_node, i);
462+
const char *kind = ts_node_type(alt);
463+
if (strcmp(kind, "elif_clause") == 0 || strcmp(kind, "else_clause") == 0) {
464+
star = py_record_subtree_imports(ctx, alt) || star;
465+
}
466+
}
467+
py_drop_rebound_imports(&ctx->result->imports, first, mark, star);
468+
}
469+
337470
static void parse_python_imports(CBMExtractCtx *ctx) {
338471
TSTreeCursor cursor = ts_tree_cursor_new(ctx->root);
339472
if (!ts_tree_cursor_goto_first_child(&cursor)) {
@@ -342,15 +475,10 @@ static void parse_python_imports(CBMExtractCtx *ctx) {
342475
}
343476
do {
344477
TSNode node = ts_tree_cursor_current_node(&cursor);
345-
const char *kind = ts_node_type(node);
346-
347-
if (strcmp(kind, "import_statement") == 0) {
348-
process_py_import_stmt(ctx, node);
349-
} else if (strcmp(kind, "import_from_statement") == 0 ||
350-
strcmp(kind, "future_import_statement") == 0) {
351-
// `from __future__ import annotations` is a distinct node type in
352-
// tree-sitter-python but has the same shape (module + name list).
353-
process_py_import_from(ctx, node);
478+
if (strcmp(ts_node_type(node), "if_statement") == 0) {
479+
process_py_type_checking_block(ctx, node);
480+
} else {
481+
process_py_import_node(ctx, node, false);
354482
}
355483
} while (ts_tree_cursor_goto_next_sibling(&cursor));
356484
ts_tree_cursor_delete(&cursor);

‎internal/cbm/lsp/py_lsp.c‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4426,6 +4426,11 @@ static const CBMType *py_resolve_annotation(PyLSPContext *ctx, const char *ann)
44264426
const CBMType *t = cbm_scope_lookup(ctx->current_scope, ann);
44274427
if (!cbm_type_is_unknown(t))
44284428
return t;
4429+
if (ctx->annotation_scope) {
4430+
t = cbm_scope_lookup(ctx->annotation_scope, ann);
4431+
if (!cbm_type_is_unknown(t))
4432+
return t;
4433+
}
44294434
if (ctx->module_qn) {
44304435
const char *qn = cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->module_qn, ann);
44314436
const CBMRegisteredType *rt = cbm_registry_lookup_type(ctx->registry, qn);
@@ -5508,6 +5513,27 @@ static void py_replay_import_statement(PyLSPContext *ctx, TSNode stmt, unsigned
55085513
}
55095514
}
55105515

5516+
/* The consequence block of a module-level `if TYPE_CHECKING:` /
5517+
* `if <module>.TYPE_CHECKING:` statement, else a null node. Mirrors the
5518+
* extraction's py_is_type_checking_condition (extract_imports.c). */
5519+
static TSNode py_type_checking_body(PyLSPContext *ctx, TSNode stmt) {
5520+
TSNode none = {0};
5521+
if (strcmp(ts_node_type(stmt), "if_statement") != 0)
5522+
return none;
5523+
TSNode cond = ts_node_child_by_field_name(stmt, "condition", 9);
5524+
if (ts_node_is_null(cond))
5525+
return none;
5526+
TSNode name = cond;
5527+
if (strcmp(ts_node_type(cond), "attribute") == 0)
5528+
name = ts_node_child_by_field_name(cond, "attribute", 9);
5529+
if (ts_node_is_null(name) || strcmp(ts_node_type(name), "identifier") != 0)
5530+
return none;
5531+
char *text = py_node_text(ctx, name);
5532+
if (!text || strcmp(text, "TYPE_CHECKING") != 0)
5533+
return none;
5534+
return ts_node_child_by_field_name(stmt, "consequence", 11);
5535+
}
5536+
55115537
static bool py_expression_is_annotation_only_assignment(TSNode statement) {
55125538
if (ts_node_named_child_count(statement) != 1)
55135539
return false;
@@ -5592,6 +5618,29 @@ void py_lsp_process_file(PyLSPContext *ctx, TSNode root) {
55925618
* Invalidate only syntactic binding targets at the control-flow
55935619
* join; a later unconditional statement may restore exact proof. */
55945620
py_invalidate_possible_bindings(ctx, c, 0);
5621+
/* `if TYPE_CHECKING:` holds the imports annotations are written
5622+
* against (#1277). Bind them into the annotation-only frame: an
5623+
* annotation resolves through them, a runtime call never does (the
5624+
* interpreter never runs the block). Extraction recorded them
5625+
* flagged type_checking_only, so they reach this walk through the
5626+
* LSP import map only, never a CALLS import map. */
5627+
TSNode tc_body = py_type_checking_body(ctx, c);
5628+
if (!ts_node_is_null(tc_body)) {
5629+
if (!ctx->annotation_scope)
5630+
ctx->annotation_scope = cbm_scope_push(ctx->arena, NULL);
5631+
CBMScope *runtime_scope = ctx->current_scope;
5632+
PyKids tk = py_kids(ctx->arena, tc_body);
5633+
for (uint32_t t = 0; ctx->annotation_scope && t < tk.n && !ctx->eval_failure; t++) {
5634+
TSNode stmt = py_kid(&tk, t);
5635+
const char *sk = ts_node_type(stmt);
5636+
if (strcmp(sk, "import_statement") == 0 ||
5637+
strcmp(sk, "import_from_statement") == 0) {
5638+
ctx->current_scope = ctx->annotation_scope;
5639+
py_replay_import_statement(ctx, stmt, consumed_imports);
5640+
ctx->current_scope = runtime_scope;
5641+
}
5642+
}
5643+
}
55955644
}
55965645
}
55975646
// Pass 2: top-level calls (rare) and nested definitions.

‎internal/cbm/lsp/py_lsp.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ typedef struct {
4949
* cbm_registry_*_for_update, never into anything behind ->fallback. */
5050
CBMTypeRegistry *registry_head;
5151
CBMScope *current_scope;
52+
// Names a module-level `if TYPE_CHECKING:` imports (#1277): consulted by
53+
// annotation resolution only, after the ordinary scope chain misses, so a
54+
// runtime call through such a name never resolves. NULL when none.
55+
CBMScope *annotation_scope;
5256

5357
// Import map: local_name -> module_qn (arena-allocated, NULL-terminated).
5458
// Built from CBMFileResult.imports.

‎src/pipeline/pass_calls.c‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,24 @@ static const char *itoa_log(int val) {
9595

9696
/* Build per-file import map from cached extraction result or graph buffer edges.
9797
* Returns parallel arrays of (local_name, module_qn) pairs. Caller frees. */
98-
/* Parse "local_name":"value" from JSON properties string. Returns strdup'd key or NULL. */
99-
static char *extract_local_name_from_json(const char *props_json) {
98+
/* The "local_name":"value" of a JSON properties string, as a span of it;
99+
* false when absent or empty. */
100+
static bool local_name_span_from_json(const char *props_json, const char **out, size_t *len) {
100101
if (!props_json) {
101-
return NULL;
102+
return false;
102103
}
103104
const char *start = strstr(props_json, "\"local_name\":\"");
104105
if (!start) {
105-
return NULL;
106+
return false;
106107
}
107108
start += strlen("\"local_name\":\"");
108109
const char *end = strchr(start, '"');
109110
if (!end || end <= start) {
110-
return NULL;
111+
return false;
111112
}
112-
return cbm_strndup(start, end - start);
113+
*out = start;
114+
*len = (size_t)(end - start);
115+
return true;
113116
}
114117

115118
/* The graph node whose QN is cbm_pipeline_fqn_compute(project, rel_path, name),
@@ -141,7 +144,8 @@ static int build_import_map(cbm_pipeline_ctx_t *ctx, const char *rel_path,
141144

142145
for (int i = 0; i < result->imports.count; i++) {
143146
const CBMImport *imp = &result->imports.items[i];
144-
if (!imp->local_name || !imp->local_name[0] || !imp->module_path) {
147+
if (!imp->local_name || !imp->local_name[0] || !imp->module_path ||
148+
!cbm_import_binds_runtime_name(imp)) {
145149
continue;
146150
}
147151
const cbm_gbuf_node_t *target =
@@ -192,7 +196,16 @@ static int build_import_map(cbm_pipeline_ctx_t *ctx, const char *rel_path,
192196
if (!target) {
193197
continue;
194198
}
195-
char *key = extract_local_name_from_json(e->properties_json);
199+
const char *name = NULL;
200+
size_t name_len = 0;
201+
/* An annotation-only import (#1277) binds no CALLS name: skipped before
202+
* the key is copied. */
203+
if (!local_name_span_from_json(e->properties_json, &name, &name_len) ||
204+
!cbm_imports_local_span_binds_runtime_name(result ? &result->imports : NULL, name,
205+
name_len)) {
206+
continue;
207+
}
208+
char *key = cbm_strndup(name, name_len);
196209
if (key) {
197210
keys[count] = key;
198211
vals[count] = target->qualified_name;

0 commit comments

Comments
 (0)