Skip to content

Commit a45f362

Browse files
committed
test(python): a name the else branch rebinds is no TYPE_CHECKING-only import (DeusData#1277)
The airflow 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, so its TYPE_CHECKING import is not annotation-only and must stay unrecorded, as the runtime import itself does. A star import in an elif or else branch may bind any name. A pure annotation import stays flagged. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 3a46d69 commit a45f362

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

‎tests/test_extraction.c‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,38 @@ TEST(python_type_checking_imports_flagged_issue1277) {
15501550
PASS();
15511551
}
15521552

1553+
/* A name the alternative of an `if TYPE_CHECKING:` binds again (the airflow
1554+
* compat shape, usually inside try/except ImportError) is bound at runtime, so
1555+
* its TYPE_CHECKING import is not annotation-only: it stays unrecorded, as the
1556+
* runtime import itself does. A star import in an alternative may bind any
1557+
* name, so it covers every name of the block. */
1558+
TEST(python_type_checking_rebound_in_else_unrecorded_issue1277) {
1559+
const char *src = "from typing import TYPE_CHECKING\n"
1560+
"if TYPE_CHECKING:\n"
1561+
" from airflow.sdk.execution_time.secrets_masker import mask_secret\n"
1562+
" from airflow.sdk.types import OnlyTyped\n"
1563+
"else:\n"
1564+
" try:\n"
1565+
" from airflow.sdk.log import mask_secret\n"
1566+
" except ImportError:\n"
1567+
" mask_secret = None\n"
1568+
"if TYPE_CHECKING:\n"
1569+
" from compat.types import Starred\n"
1570+
"elif SOMETHING:\n"
1571+
" from compat import *\n";
1572+
CBMFileResult *r = extract(src, CBM_LANG_PYTHON, "t", "hook.py");
1573+
ASSERT_NOT_NULL(r);
1574+
const CBMImport *typed = py_find_import(r, "OnlyTyped");
1575+
ASSERT_NOT_NULL(typed);
1576+
ASSERT_TRUE(typed->type_checking_only); /* only the type checker binds it */
1577+
ASSERT_NULL(py_find_import(r, "mask_secret")); /* the else branch binds it at runtime */
1578+
ASSERT_NULL(py_find_import(r, "Starred")); /* the elif star import may bind it */
1579+
ASSERT_NULL(py_find_import(r, "compat")); /* alternatives stay unrecorded */
1580+
ASSERT_NOT_NULL(py_find_import(r, "TYPE_CHECKING"));
1581+
cbm_free_result(r);
1582+
PASS();
1583+
}
1584+
15531585
TEST(python_function) {
15541586
CBMFileResult *r = extract(
15551587
"def greet(name):\n return f\"Hello {name}\"\n\ndef main():\n greet(\"World\")\n",
@@ -12670,6 +12702,7 @@ SUITE(extraction) {
1267012702
RUN_TEST(python_class);
1267112703
RUN_TEST(python_annotated_instance_fields_exported_issue1277);
1267212704
RUN_TEST(python_type_checking_imports_flagged_issue1277);
12705+
RUN_TEST(python_type_checking_rebound_in_else_unrecorded_issue1277);
1267312706
RUN_TEST(js_function);
1267412707
RUN_TEST(js_class);
1267512708
RUN_TEST(ts_function);

0 commit comments

Comments
 (0)