Python: Add QL support for lazy imports

Adds a new `isLazy` predicate to the relevant classes, and adds the
relevant dbscheme (and up/downgrade) changes. On upgrades we do nothing,
and on downgrades we remove the `is_lazy` bits.
This commit is contained in:
Taus
2026-04-10 14:25:08 +00:00
parent fe94828fe4
commit 1ddfed6b6b
10 changed files with 5188 additions and 1 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,12 @@
class BoolParent extends @py_bool_parent {
string toString() { result = "BoolParent" }
}
// Drop py_bools rows for Import and ImportStar parents,
// since the old schema does not include them in @py_bool_parent.
from BoolParent parent, int idx
where
py_bools(parent, idx) and
not parent instanceof @py_Import and
not parent instanceof @py_ImportStar
select parent, idx

View File

@@ -0,0 +1,3 @@
description: Remove is_lazy field from Import and ImportStar (downgrade PEP 810 lazy imports)
compatibility: backwards
py_bools.rel: run py_bools.qlo

View File

@@ -334,9 +334,11 @@ IfExp.field('body', expr, 'if-true expression')
IfExp.field('orelse', expr, 'if-false expression')
Import.field('names', alias_list, 'alias list')
Import.field('is_lazy', bool_, 'lazy')
ImportFrom.set_name('ImportStar')
ImportFrom.field('module', expr)
ImportFrom.field('is_lazy', bool_, 'lazy')
ImportMember.field('module', expr)
ImportMember.field('name', string)

View File

@@ -698,6 +698,9 @@ class Import_ extends @py_Import, Stmt {
/** Gets an alias of this import statement. */
Alias getAName() { result = this.getNames().getAnItem() }
/** Whether the lazy property of this import statement is true. */
predicate isLazy() { py_bools(this, 2) }
override string toString() { result = "Import" }
}
@@ -720,6 +723,9 @@ class ImportStar_ extends @py_ImportStar, Stmt {
/** Gets the module of this import * statement. */
Expr getModule() { py_exprs(result, _, this, 1) }
/** Whether the lazy property of this import * statement is true. */
predicate isLazy() { py_bools(this, 2) }
override string toString() { result = "ImportStar" }
}

View File

@@ -517,6 +517,7 @@ py_extracted_version(int module : @py_Module ref,
/* <Field> Import.location = 0, location */
/* <Field> Import.names = 1, alias_list */
/* <Field> Import.is_lazy = 2, bool */
/* <Field> ImportExpr.location = 0, location */
/* <Field> ImportExpr.parenthesised = 1, bool */
@@ -526,6 +527,7 @@ py_extracted_version(int module : @py_Module ref,
/* <Field> ImportStar.location = 0, location */
/* <Field> ImportStar.module = 1, expr */
/* <Field> ImportStar.is_lazy = 2, bool */
/* <Field> ImportMember.location = 0, location */
/* <Field> ImportMember.parenthesised = 1, bool */
@@ -1127,7 +1129,7 @@ case @py_unaryop.kind of
@py_ast_node = @py_Class | @py_Function | @py_Module | @py_StringPart | @py_comprehension | @py_dict_item | @py_expr | @py_pattern | @py_stmt | @py_type_parameter;
@py_bool_parent = @py_For | @py_Function | @py_Print | @py_With | @py_expr | @py_pattern;
@py_bool_parent = @py_For | @py_Function | @py_Import | @py_ImportStar | @py_Print | @py_With | @py_expr | @py_pattern;
@py_dict_item_list_parent = @py_Call | @py_ClassExpr | @py_Dict;

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,2 @@
description: Add is_lazy field to Import and ImportStar for PEP 810 lazy imports
compatibility: backwards