@@ -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+
337470static 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 );
0 commit comments