Repository navigation
Conversation
Adds a simple recursive scheme for inferring expression contexts. This means we no longer need to do this in the extractor, but the present commit does not attempt to remove that code. Overall this is an improvement to the readability of the code. Instead of writing `py_expr_contexts(_, 2, foo)`, we can write `foo.isDeleteContext()`. I validated this by running the newly added `ExprContextParity` query on `python/cpython`, where it had zero results (meaning all of the inferred contexts match exactly to the ones generated by the extractor). Finally, the `UselessClass` query used `ctx instanceof AugAssign`, which never did anything in the first place since `AugAssign` isn't a valid context type. I therefore refactored it to (presumably) capture the original intent of also matching augmented assignments.
Contributor
There was a problem hiding this comment.
🔵 Needs a closer look
The migration changes core name-binding and control-flow classification, and correctness across both parser paths still needs human validation.
2 open issues
What changed in this PR
Moves Python expression-context classification into QL while retaining extractor-generated contexts for comparison.
Changes:
- Adds public
Exprcontext predicates and migrates library and query consumers. - Adds parity tests covering legacy and modern Python syntax.
- Updates
py/useless-classto recognize augmented-assignment state changes.
| File | Description |
|---|---|
python/ql/test/query-tests/Classes/useless/test.py |
Covers augmented attribute and subscript mutations. |
python/ql/test/library-tests/expr-context-parity/new_parser.py |
Adds modern-syntax parity fixtures. |
python/ql/test/library-tests/expr-context-parity/legacy.py |
Adds legacy-parser context fixtures. |
python/ql/test/library-tests/expr-context-parity/ExprContextParity.ql |
Compares inferred and extracted contexts. |
python/ql/test/library-tests/expr-context-parity/ExprContextParity.expected |
Establishes an empty parity-result baseline. |
python/ql/src/Classes/UselessClass.ql |
Recognizes augmented assignments as state changes. |
python/ql/src/Classes/OverwritingAttributeInSuperClass.ql |
Uses the new store-context predicate. |
python/ql/src/Classes/ClassAttributes.qll |
Migrates the monkey-patching store check. |
python/ql/src/analysis/RatioOfDefinitions.ql |
Uses inferred load contexts. |
python/ql/lib/semmle/python/SelfAttribute.qll |
Migrates attribute read and store classification. |
python/ql/lib/semmle/python/Flow.qll |
Uses inferred contexts for control-flow classification. |
python/ql/lib/semmle/python/Exprs.qll |
Implements context inference and public predicates. |
python/ql/lib/semmle/python/controlflow/internal/Cfg.qll |
Migrates internal control-flow context checks. |
python/ql/lib/change-notes/2026-10-23-inferred-expression-contexts.md |
Documents the new library API. |
Review effort: Max
Limits `inferredNonLoadExpressionContext`, and extends the new parser tests to also contain the legacy test cases.
tausbn
marked this pull request as ready for review
October 23, 2026 15:41
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Adds a simple recursive scheme for inferring expression contexts in QL. This means we no longer need to do this in the extractor, but the present PR does not attempt to remove that code.
Overall this is an improvement to the readability of the code. Instead of writing
py_expr_contexts(_, 2, foo), we can writefoo.isDeleteContext().I validated this by running the newly added
ExprContextParityquery onpython/cpython, where it had zero results (meaning all of the inferred contexts match exactly to the ones generated by the extractor).Finally, the
UselessClassquery usedctx instanceof AugAssign, which never did anything in the first place sinceAugAssignisn't a valid context type. I therefore refactored it to (presumably) capture the original intent of also matching augmented assignments.