Skip to content

Python: Port simple points-to queries without DCA alert changes - #21519

Merged
yoff merged 22 commits into
mainfrom
tausbn/python-port-no-alert-change
Mar 27, 2026
Merged

Python: Port simple points-to queries without DCA alert changes#21519
yoff merged 22 commits into
mainfrom
tausbn/python-port-no-alert-change

Conversation

@tausbn

Copy link
Copy Markdown
Contributor

A spin-off of #21350, containing only those changes that did not result in alert changes according to DCA. This should hopefully make it easier to review.

tausbn added 15 commits March 20, 2026 13:28
Also extends the list of known built-ins slightly, to add some that were
missing.
Uses a (perhaps) slightly coarser approximation of what modules are
imported, but it's probably fine.
This module (which for convenience currently resides inside
`DataFlowDispatch`, but this may change later) contains convenience
predicates for bridging the gap between the data-flow layer and the old
points-to analysis.
Approximates the behaviour of `Types::isNewStyle` but without depending
on points-to
These could arguably be moved to `Class` itself, but for now I'm
choosing to limit the changes to the `DuckTyping` module (until we
decide on a proper API).
Only trivial test changes.
Only trivial test changes.
Only trivial test changes.
Adds `overridesMethod` and `isPropertyAccessor`.
Primarily used to filter out false positives in cases where our MRO
approximation may be wrong.
Only trivial test changes.
@tausbn
tausbnforce-pushed the tausbn/python-port-no-alert-change branch from 7a255dc to 434b397CompareMarch 20, 2026 13:30
@tausbn
tausbn marked this pull request as ready for review March 23, 2026 13:28
@tausbn
tausbn requested a review from a team as a code ownerMarch 23, 2026 13:28
CopilotAI review requested due to automatic review settings March 23, 2026 13:28

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Ports a set of Python QL queries away from the legacy points-to library (while aiming to avoid DCA-observed alert changes), updating shared libraries and test expectations to match the new resolution approach.

Changes:

  • Replace LegacyPointsTo usage in several Python queries with semmle.python.ApiGraphs and/or DataFlowDispatch.DuckTyping.
  • Introduce a DuckTyping helper module in DataFlowDispatch.qll for structural class/property checks.
  • Update query test .expected files and add a change note for the analysis change.

Reviewed changes

Copilot reviewed 20 out of 20 changed files in this pull request and generated 3 comments.

Show a summary per file
FileDescription
python/ql/src/Variables/SuspiciousUnusedLoopIterationVariable.qlSwitches range/list(range) detection from points-to to API graph call matching.
python/ql/src/Statements/UnnecessaryDelete.qlReplaces points-to detection of sys.exc_info calls with API graph matching.
python/ql/src/Statements/TopLevelPrint.qlRemoves points-to dependency and adds a local predicate to detect “used as module”.
python/ql/src/Statements/SideEffectInAssert.qlReplaces points-to matching of subprocess.* calls with API graph matching.
python/ql/src/Statements/DocStrings.qlReplaces points-to override/property checks with DuckTyping-based checks.
python/ql/src/Imports/DeprecatedModule.qlReplaces points-to check for ImportError with API graph value reachability.
python/ql/src/Functions/DeprecatedSliceMethod.qlUses DuckTyping to exclude overrides/unresolved bases instead of points-to method modeling.
python/ql/src/Expressions/UseofApply.qlMatches apply(...) via API graphs for Python 2 instead of points-to.
python/ql/src/Classes/UselessClass.qlReplaces inheritance checks based on points-to class values with class hierarchy predicates.
python/ql/src/Classes/SuperInOldStyleClass.qlUses DuckTyping::isNewStyle instead of points-to-derived class object inference.
python/ql/src/Classes/SlotsInOldStyleClass.qlUses DuckTyping attribute declaration checks instead of points-to class object inference.
python/ql/src/Classes/ShouldBeContextManager.qlUses DuckTyping context manager/method checks instead of points-to class values.
python/ql/src/Classes/PropertyInOldStyleClass.qlDetects @property functions in old-style classes without points-to.
python/ql/lib/semmle/python/dataflow/new/internal/DataFlowDispatch.qllAdds DuckTyping module with structural predicates and MRO-related helpers.
python/ql/lib/semmle/python/dataflow/new/internal/Builtins.qllExtends builtins list (notably adds exit/quit etc.) and includes apply for Py2.
python/ql/test/query-tests/Functions/general/DeprecatedSliceMethod.expectedUpdates expected result rendering for the updated query entity selection.
python/ql/test/query-tests/Classes/should-be-context-manager/ShouldBeContextManager.expectedUpdates expected result rendering for the updated query entity selection.
python/ql/test/2/query-tests/Classes/new-style/SlotsInOldStyleClass.expectedUpdates expected result rendering for the updated query entity selection.
python/ql/test/2/query-tests/Classes/new-style/PropertyInOldStyleClass.expectedUpdates expected result location/entity rendering for the updated query.
python/ql/src/change-notes/2026-03-13-port-simple-points-to-queries.mdAdds a change note describing the points-to removal work.

Comment threadpython/ql/src/Variables/SuspiciousUnusedLoopIterationVariable.ql Outdated
Comment threadpython/ql/lib/semmle/python/dataflow/new/internal/DataFlowDispatch.qll Outdated
tausbnand others added 2 commits March 23, 2026 15:07
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
yoff
yoff previously approved these changes Mar 23, 2026

@yoffyoff left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good. It is great to get started on this!
Very nice presentation, this was super easy to review :-)

in PropertyInOldStyleClass. This matches the previous behaviour more
closely.
This one also allows easy access to the method being overridden and the
class on which it resides. This let's us simplify DocStrings.ql
accordingly.
For module-level metaclass declarations, we now also check that the
right hand side in a `__metaclass__ = type` assignment is in fact the
built-in `type`.
@tausbn
tausbn requested a review from yoffMarch 23, 2026 15:22
yoff
yoff previously approved these changes Mar 23, 2026

@yoffyoff left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice tweaks, lgtm :-)

By limiting the results to the class that actually defines the `__del__`
method, we eliminate a bunch of FPs where a _subclass_ of such a class
would also get flagged.
@tausbn

Copy link
Copy Markdown
ContributorAuthor

DCA looks nice. Main alert changes are in queries that are not part of any standard suite. Also, overall analysis time is roughly cut in half!

@owen-mcowen-mc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Proxying previous approval.

@yoff
yoff merged commit 08e1150 into mainMar 27, 2026
20 checks passed
@yoff
yoff deleted the tausbn/python-port-no-alert-change branch March 27, 2026 07:44
Sign up for freeto 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.

4 participants

@tausbn@yoff@owen-mc