Skip to content

Commit 2455e75

Browse files
committed
fix(python): a TYPE_CHECKING import the else branch rebinds stays unrecorded (DeusData#1277)
The compat shape imports a name under `if TYPE_CHECKING:` and again in the else branch, usually inside try/except ImportError. The name is bound at runtime there, so its TYPE_CHECKING import is no annotation-only import: recording it flagged made every CALLS import map skip the name and lost the calls and bases that bind through the runtime import (airflow mask_secret, OpenLineageRedactor(SecretsMasker)). The imports of the elif/else branches are collected (any depth, iteratively), every TYPE_CHECKING import whose local name they bind is dropped with them, and a star import there drops the whole block: those names keep the behaviour they had before DeusData#1277. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent a45f362 commit 2455e75

1 file changed

Lines changed: 78 additions & 2 deletions

File tree

‎internal/cbm/extract_imports.c‎

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,12 +354,76 @@ static void process_py_import_node(CBMExtractCtx *ctx, TSNode node, bool type_on
354354
}
355355
}
356356

357+
static bool py_is_import_node(TSNode node) {
358+
const char *kind = ts_node_type(node);
359+
return strcmp(kind, "import_statement") == 0 || strcmp(kind, "import_from_statement") == 0 ||
360+
strcmp(kind, "future_import_statement") == 0;
361+
}
362+
363+
static bool py_import_is_wildcard(TSNode import_node) {
364+
uint32_t n = ts_node_child_count(import_node);
365+
for (uint32_t i = 0; i < n; i++) {
366+
if (strcmp(ts_node_type(ts_node_child(import_node, i)), "wildcard_import") == 0) {
367+
return true;
368+
}
369+
}
370+
return false;
371+
}
372+
373+
/* Records, unflagged, every import statement anywhere under `root` (the
374+
* runtime alternative of a TYPE_CHECKING block usually nests them in
375+
* try/except ImportError). An iterative pre-order walk: adversarially deep
376+
* nesting must not exhaust the stack. Returns true when one of them is a star
377+
* import. */
378+
static bool py_record_subtree_imports(CBMExtractCtx *ctx, TSNode root) {
379+
bool star = false;
380+
TSTreeCursor cursor = ts_tree_cursor_new(root);
381+
for (;;) {
382+
TSNode node = ts_tree_cursor_current_node(&cursor);
383+
bool is_import = py_is_import_node(node);
384+
if (is_import) {
385+
process_py_import_node(ctx, node, false);
386+
star = star || py_import_is_wildcard(node);
387+
}
388+
if (!is_import && ts_tree_cursor_goto_first_child(&cursor)) {
389+
continue;
390+
}
391+
while (!ts_tree_cursor_goto_next_sibling(&cursor)) {
392+
if (!ts_tree_cursor_goto_parent(&cursor)) {
393+
ts_tree_cursor_delete(&cursor);
394+
return star;
395+
}
396+
}
397+
}
398+
}
399+
400+
/* Drops the imports [first, mark) whose local name an import at [mark, count)
401+
* binds as well (every one when `star`), then the imports [mark, count). */
402+
static void py_drop_rebound_imports(CBMImportArray *imports, int first, int mark, bool star) {
403+
int keep = first;
404+
for (int i = first; i < mark; i++) {
405+
const CBMImport *imp = &imports->items[i];
406+
bool rebound = star;
407+
for (int j = mark; !rebound && j < imports->count; j++) {
408+
rebound = imp->local_name && imports->items[j].local_name &&
409+
strcmp(imp->local_name, imports->items[j].local_name) == 0;
410+
}
411+
if (!rebound) {
412+
imports->items[keep++] = *imp;
413+
}
414+
}
415+
imports->count = keep;
416+
}
417+
357418
/* A module-level `if TYPE_CHECKING:` holds imports a type checker sees and
358419
* the interpreter never runs (#1277: an annotated field's type is usually
359420
* imported exactly there, to avoid an import cycle). Record the imports of
360-
* its consequence block, flagged type_checking_only; its elif/else branches
421+
* its consequence block, flagged type_checking_only. Its elif/else branches
361422
* are runtime alternatives and stay unrecorded, as every other compound
362-
* statement's imports do. */
423+
* statement's imports do; a name they bind again (the compat shape: the same
424+
* name imported in `else:` under try/except ImportError) is bound at runtime,
425+
* so its TYPE_CHECKING import is no annotation-only import and is dropped as
426+
* well, and a star import there may bind any name. */
363427
static void process_py_type_checking_block(CBMExtractCtx *ctx, TSNode if_node) {
364428
if (!py_is_type_checking_condition(
365429
ctx, ts_node_child_by_field_name(if_node, TS_FIELD("condition")))) {
@@ -369,13 +433,25 @@ static void process_py_type_checking_block(CBMExtractCtx *ctx, TSNode if_node) {
369433
if (ts_node_is_null(body)) {
370434
return;
371435
}
436+
int first = ctx->result->imports.count;
372437
TSTreeCursor cursor = ts_tree_cursor_new(body);
373438
if (ts_tree_cursor_goto_first_child(&cursor)) {
374439
do {
375440
process_py_import_node(ctx, ts_tree_cursor_current_node(&cursor), true);
376441
} while (ts_tree_cursor_goto_next_sibling(&cursor));
377442
}
378443
ts_tree_cursor_delete(&cursor);
444+
int mark = ctx->result->imports.count;
445+
bool star = false;
446+
uint32_t n = ts_node_child_count(if_node);
447+
for (uint32_t i = 0; i < n; i++) {
448+
TSNode alt = ts_node_child(if_node, i);
449+
const char *kind = ts_node_type(alt);
450+
if (strcmp(kind, "elif_clause") == 0 || strcmp(kind, "else_clause") == 0) {
451+
star = py_record_subtree_imports(ctx, alt) || star;
452+
}
453+
}
454+
py_drop_rebound_imports(&ctx->result->imports, first, mark, star);
379455
}
380456

381457
static void parse_python_imports(CBMExtractCtx *ctx) {

0 commit comments

Comments
 (0)