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): a name the else branch rebinds is no TYPE_CHECKING-only import (#1277)

Martin Vogela45f36251e79
diff --git a/tests/test_extraction.c b/tests/test_extraction.c
index fcaa3655f..5d0165252 100644
--- a/tests/test_extraction.c
+++ b/tests/test_extraction.c
@@ -1550,6 +1550,38 @@ TEST(python_type_checking_imports_flagged_issue1277) {
PASS();
}
+/* A name the alternative of an `if TYPE_CHECKING:` binds again (the airflow
+ * compat shape, usually inside try/except ImportError) is bound at runtime, so
+ * its TYPE_CHECKING import is not annotation-only: it stays unrecorded, as the
+ * runtime import itself does. A star import in an alternative may bind any
+ * name, so it covers every name of the block. */
+TEST(python_type_checking_rebound_in_else_unrecorded_issue1277) {
+ const char *src = "from typing import TYPE_CHECKING\n"
+ "if TYPE_CHECKING:\n"
+ " from airflow.sdk.execution_time.secrets_masker import mask_secret\n"
+ " from airflow.sdk.types import OnlyTyped\n"
+ "else:\n"
+ " try:\n"
+ " from airflow.sdk.log import mask_secret\n"
+ " except ImportError:\n"
+ " mask_secret = None\n"
+ "if TYPE_CHECKING:\n"
+ " from compat.types import Starred\n"
+ "elif SOMETHING:\n"
+ " from compat import *\n";
+ CBMFileResult *r = extract(src, CBM_LANG_PYTHON, "t", "hook.py");
+ ASSERT_NOT_NULL(r);
+ const CBMImport *typed = py_find_import(r, "OnlyTyped");
+ ASSERT_NOT_NULL(typed);
+ ASSERT_TRUE(typed->type_checking_only); /* only the type checker binds it */
+ ASSERT_NULL(py_find_import(r, "mask_secret")); /* the else branch binds it at runtime */
+ ASSERT_NULL(py_find_import(r, "Starred")); /* the elif star import may bind it */
+ ASSERT_NULL(py_find_import(r, "compat")); /* alternatives stay unrecorded */
+ ASSERT_NOT_NULL(py_find_import(r, "TYPE_CHECKING"));
+ 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",
@@ -12670,6 +12702,7 @@ SUITE(extraction) {
RUN_TEST(python_class);
RUN_TEST(python_annotated_instance_fields_exported_issue1277);
RUN_TEST(python_type_checking_imports_flagged_issue1277);
+ RUN_TEST(python_type_checking_rebound_in_else_unrecorded_issue1277);
RUN_TEST(js_function);
RUN_TEST(js_class);
RUN_TEST(ts_function);