Skip to content

Python: Add support for PEP 810#21694

Open
tausbn wants to merge 7 commits intomainfrom
tausbn/python-add-support-for-pep-810
Open

Python: Add support for PEP 810#21694
tausbn wants to merge 7 commits intomainfrom
tausbn/python-add-support-for-pep-810

Conversation

@tausbn
Copy link
Copy Markdown
Contributor

@tausbn tausbn commented Apr 10, 2026

Said PEP adds support for annotating imports (import ... and from ... import ...) with a lazy keyword, the semantics of which are not relevant to this PR.

We handle this in the parser in much the same way we do async currently -- import nodes get a new is_lazy field that gets populated when the lazy keyword is present. On the QL side, this determines whether the new isLazy predicate method holds on the relevant nodes.

Should be reviewed commit-by-commit.

tausbn added 6 commits April 10, 2026 13:50
As defined in PEP-810. We implement this in much the same way as how we
handle `async` annotations currently. The relevant nodes get an
`is_lazy` field that defaults to being false.
Otherwise these will disappear every time we regenerate the AST.
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.
@tausbn tausbn marked this pull request as ready for review April 13, 2026 11:22
@tausbn tausbn requested review from a team as code owners April 13, 2026 11:23
Copilot AI review requested due to automatic review settings April 13, 2026 11:23
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds Python extractor + QL support for PEP 810 “lazy imports” by threading a new is_lazy boolean through the parser/DB scheme and exposing it as isLazy() on relevant import AST nodes.

Changes:

  • Extend the tree-sitter grammar and TSG mapping to recognize lazy import ... / lazy from ... import ... and set is_lazy.
  • Add is_lazy to the Python DB scheme for Import and ImportStar, including upgrade/downgrade plumbing.
  • Expose isLazy() in the generated QL AST classes and add extractor/parser tests and a change note.
Show a summary per file
File Description
python/ql/test/3/extractor-tests/lazy-imports/test.ql New extractor test query validating Import.isLazy() output.
python/ql/test/3/extractor-tests/lazy-imports/test.py New Python source for lazy vs non-lazy import extraction tests.
python/ql/test/3/extractor-tests/lazy-imports/test.expected Expected results for the new extractor test.
python/ql/lib/upgrades/279cbb08d387ecd57ac177e87c94cfd5ca62f792/upgrade.properties DB upgrade metadata for adding is_lazy.
python/ql/lib/upgrades/279cbb08d387ecd57ac177e87c94cfd5ca62f792/semmlecode.python.dbscheme Upgrade dbscheme snapshot reflecting the new fields.
python/ql/lib/upgrades/279cbb08d387ecd57ac177e87c94cfd5ca62f792/old.dbscheme Prior dbscheme snapshot for the upgrade.
python/ql/lib/semmlecode.python.dbscheme Adds Import.is_lazy / ImportStar.is_lazy fields and updates @py_bool_parent.
python/ql/lib/semmle/python/AstGenerated.qll Adds isLazy() predicates for Import and ImportStar AST nodes.
python/ql/lib/change-notes/2026-04-10-support-lazy-keyword.md Change note documenting the new syntax support.
python/extractor/tsg-python/tsp/src/tree_sitter/array.h Refactors array delete/swap helpers used by the embedded tree-sitter runtime.
python/extractor/tsg-python/tsp/src/node-types.json Updates node types to include the is_lazy field and lazy token.
python/extractor/tsg-python/tsp/src/grammar.json Generated grammar updates to parse optional lazy on import statements.
python/extractor/tsg-python/tsp/grammar.js Grammar source changes adding optional lazy to import statements and soft-keyword handling.
python/extractor/tsg-python/python.tsg Sets is_lazy attribute on import parse nodes when lazy is present.
python/extractor/tests/parser/lazy_imports_new.py New parser test corpus for lazy imports + soft-keyword behavior.
python/extractor/tests/parser/lazy_imports_new.expected Expected AST dump for the new parser test.
python/extractor/semmle/query_gen.py Ensures generated QL libraries include overlay[local] module; header.
python/extractor/semmle/python/parser/tsg_parser.py Updates placeholder node creation to treat is_lazy like other default-false fields.
python/extractor/semmle/python/parser/dump_ast.py Avoids printing default-false is_lazy in AST dumps.
python/extractor/semmle/python/master.py Adds is_lazy fields to Import and ImportFrom (ImportStar) schema definitions.
python/extractor/semmle/python/ast.py Adds is_lazy slots/constructor params to Import and ImportFrom.
python/downgrades/eb5fc917c79bb23ce2de4a022f3e566d57a91be9/upgrade.properties Downgrade metadata to remove is_lazy, including a py_bools rewrite step.
python/downgrades/eb5fc917c79bb23ce2de4a022f3e566d57a91be9/semmlecode.python.dbscheme Downgrade dbscheme snapshot.
python/downgrades/eb5fc917c79bb23ce2de4a022f3e566d57a91be9/py_bools.ql Downgrade query to drop py_bools rows for Import/ImportStar parents.
python/downgrades/eb5fc917c79bb23ce2de4a022f3e566d57a91be9/old.dbscheme Prior dbscheme snapshot for the downgrade.

Copilot's findings

  • Files reviewed: 25/26 changed files
  • Comments generated: 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants