Skip to content

Commit 8d1815c

Browse files
committed
Merge remote-tracking branch 'origin/main' into fix/data-flows-test-file-scope
Signed-off-by: Martin Vogel <[email protected]>
2 parents 6f00af3 + 0f6cc06 commit 8d1815c

7 files changed

Lines changed: 1417 additions & 52 deletions

File tree

‎internal/cbm/extract_imports.c‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,97 @@ static bool py_ns_delete(PyNamespaceBuilder *b, TSNode node, int depth) {
877877
return ok;
878878
}
879879

880+
static bool py_ns_statement(PyNamespaceBuilder *b, TSNode node);
881+
882+
/* `if TYPE_CHECKING:` / `if <module>.TYPE_CHECKING:` */
883+
static bool py_ns_is_type_checking_if(PyNamespaceBuilder *b, TSNode node) {
884+
TSNode cond = ts_node_child_by_field_name(node, TS_FIELD("condition"));
885+
if (ts_node_is_null(cond))
886+
return false;
887+
TSNode name = cond;
888+
if (strcmp(ts_node_type(cond), "attribute") == 0)
889+
name = ts_node_child_by_field_name(cond, TS_FIELD("attribute"));
890+
const char *text = NULL;
891+
size_t len = 0;
892+
return !ts_node_is_null(name) && strcmp(ts_node_type(name), "identifier") == 0 &&
893+
py_ns_slice(b, name, &text, &len) && len == strlen("TYPE_CHECKING") &&
894+
memcmp(text, "TYPE_CHECKING", len) == 0;
895+
}
896+
897+
/* An import in an elif/else branch of a TYPE_CHECKING block rebinds its names
898+
* at runtime from wherever that branch says (the compat shape: `else: try:
899+
* from runtime.place import name`). A static reader cannot tell which source
900+
* the module ends up exporting, so every name such an import binds becomes a
901+
* SHADOW (reason UNSUPPORTED: the re-export is withdrawn, as for a later
902+
* assignment); a star import, or an import form the builder does not read,
903+
* may bind any name, so the whole namespace becomes unknown. An iterative
904+
* walk of the branch: deep nesting must not exhaust the stack. */
905+
static bool py_ns_alternative_imports(PyNamespaceBuilder *b, TSNode root) {
906+
TSTreeCursor cursor = ts_tree_cursor_new(root);
907+
bool done = false;
908+
while (!done && !b->failure) {
909+
TSNode node = ts_tree_cursor_current_node(&cursor);
910+
const char *kind = ts_node_type(node);
911+
bool from = strcmp(kind, "import_from_statement") == 0;
912+
bool is_import = from || strcmp(kind, "import_statement") == 0;
913+
if (is_import) {
914+
int start = b->facts.count;
915+
bool star = !py_ns_import(b, node, from);
916+
for (int i = start; !star && i < b->facts.count; i++) {
917+
star = b->facts.items[i].kind == CBM_PY_NS_IMPORT_STAR;
918+
b->facts.items[i] = (CBMPyNamespaceFact){.kind = CBM_PY_NS_SHADOW,
919+
.local_name = b->facts.items[i].local_name,
920+
.reason = CBM_PY_NS_REASON_UNSUPPORTED};
921+
}
922+
if (star && !b->failure) {
923+
b->facts.count = start;
924+
py_ns_unknown(b, CBM_PY_NS_REASON_COMPOUND);
925+
}
926+
}
927+
if (!is_import && ts_tree_cursor_goto_first_child(&cursor))
928+
continue;
929+
while (!ts_tree_cursor_goto_next_sibling(&cursor)) {
930+
if (!ts_tree_cursor_goto_parent(&cursor)) {
931+
done = true;
932+
break;
933+
}
934+
}
935+
}
936+
ts_tree_cursor_delete(&cursor);
937+
return !b->failure;
938+
}
939+
940+
/* The type checker view of a module-level `if TYPE_CHECKING:`: its
941+
* consequence runs, its elif/else branches never do. Packages that load
942+
* their public names lazily at runtime (`else: sys.modules[...] =
943+
* _LazyModule(...)`) publish them exactly there, so a static reader of the
944+
* namespace must take this branch to see them; every other compound
945+
* statement stays a whole-namespace unknown. A name an elif/else branch
946+
* imports again is withdrawn (py_ns_alternative_imports). */
947+
static bool py_ns_type_checking_block(PyNamespaceBuilder *b, TSNode node) {
948+
TSNode body = ts_node_child_by_field_name(node, TS_FIELD("consequence"));
949+
if (ts_node_is_null(body))
950+
return py_ns_unknown(b, CBM_PY_NS_REASON_COMPOUND);
951+
uint32_t count = ts_node_named_child_count(body);
952+
for (uint32_t i = 0; i < count && !b->failure; i++) {
953+
TSNode stmt = ts_node_named_child(body, i);
954+
int start = b->facts.count;
955+
if (!py_ns_statement(b, stmt) && !b->failure) {
956+
b->facts.count = start;
957+
if (!py_ns_unknown(b, CBM_PY_NS_REASON_UNSUPPORTED))
958+
return false;
959+
}
960+
}
961+
uint32_t children = ts_node_child_count(node);
962+
for (uint32_t i = 0; i < children && !b->failure; i++) {
963+
TSNode alt = ts_node_child(node, i);
964+
const char *kind = ts_node_type(alt);
965+
if (strcmp(kind, "elif_clause") == 0 || strcmp(kind, "else_clause") == 0)
966+
py_ns_alternative_imports(b, alt);
967+
}
968+
return !b->failure;
969+
}
970+
880971
static bool py_ns_statement(PyNamespaceBuilder *b, TSNode node) {
881972
const char *kind = ts_node_type(node);
882973
if (strcmp(kind, "comment") == 0 || strcmp(kind, "pass_statement") == 0)
@@ -922,6 +1013,8 @@ static bool py_ns_statement(PyNamespaceBuilder *b, TSNode node) {
9221013
return py_ns_assignment(b, expr);
9231014
return py_ns_safe_expr(b, expr, 0) || py_ns_unknown(b, CBM_PY_NS_REASON_EFFECT);
9241015
}
1016+
if (strcmp(kind, "if_statement") == 0 && py_ns_is_type_checking_if(b, node))
1017+
return py_ns_type_checking_block(b, node);
9251018
if (strcmp(kind, "if_statement") == 0 || strcmp(kind, "try_statement") == 0 ||
9261019
strcmp(kind, "for_statement") == 0 || strcmp(kind, "while_statement") == 0 ||
9271020
strcmp(kind, "with_statement") == 0 || strcmp(kind, "match_statement") == 0)

0 commit comments

Comments
 (0)