mirror of
https://github.com/github/codeql.git
synced 2026-04-17 13:04:02 +02:00
Merge pull request #21694 from github/tausbn/python-add-support-for-pep-810
Python: Add support for PEP 810
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
category: minorAnalysis
|
||||
---
|
||||
|
||||
- The Python extractor now supports the new `lazy import ...` and `lazy from ... import ...` (as defined in [PEP-810](https://peps.python.org/pep-0810/)) that will be part of Python 3.15.
|
||||
@@ -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" }
|
||||
}
|
||||
|
||||
|
||||
@@ -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
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,2 @@
|
||||
description: Add is_lazy field to Import and ImportStar for PEP 810 lazy imports
|
||||
compatibility: backwards
|
||||
@@ -0,0 +1,9 @@
|
||||
| 2 | Import | lazy |
|
||||
| 3 | Import | lazy |
|
||||
| 4 | Import | lazy |
|
||||
| 5 | Import | lazy |
|
||||
| 6 | Import | lazy |
|
||||
| 7 | from l import * | lazy |
|
||||
| 10 | Import | normal |
|
||||
| 11 | Import | normal |
|
||||
| 12 | from w import * | normal |
|
||||
12
python/ql/test/3/extractor-tests/lazy-imports/test.py
Normal file
12
python/ql/test/3/extractor-tests/lazy-imports/test.py
Normal file
@@ -0,0 +1,12 @@
|
||||
# Lazy imports (PEP 810)
|
||||
lazy import a
|
||||
lazy from b import c
|
||||
lazy from d import e as f
|
||||
lazy import g.h as i
|
||||
lazy from ..j import k
|
||||
lazy from l import *
|
||||
|
||||
# Non-lazy imports
|
||||
import x
|
||||
from y import z
|
||||
from w import *
|
||||
11
python/ql/test/3/extractor-tests/lazy-imports/test.ql
Normal file
11
python/ql/test/3/extractor-tests/lazy-imports/test.ql
Normal file
@@ -0,0 +1,11 @@
|
||||
import python
|
||||
|
||||
string lazy(Stmt s) {
|
||||
if s.(Import).isLazy() or s.(ImportStar).isLazy() then result = "lazy" else result = "normal"
|
||||
}
|
||||
|
||||
from Stmt s
|
||||
where
|
||||
s.getLocation().getFile().getShortName() = "test.py" and
|
||||
(s instanceof Import or s instanceof ImportStar)
|
||||
select s.getLocation().getStartLine(), s.toString(), lazy(s)
|
||||
Reference in New Issue
Block a user