Python: Track instance attributes through type tracking#21969
Open
Copilot wants to merge 8 commits into
Open
Conversation
Copilot
AI
changed the title
[WIP] Investigate missing alerts when storing connect() result
Document missing PEP249 SQL alerts when connection is stored in a self attribute
Jun 11, 2026
Copilot
AI
changed the title
Document missing PEP249 SQL alerts when connection is stored in a self attribute
Track instance attributes set in Jun 11, 2026
__init__ through type tracking
Copilot stopped work on behalf of
owen-mc due to an error
June 11, 2026 12:13
Copilot stopped work on behalf of
owen-mc due to an error
June 11, 2026 12:14
Use a field level step like JS and Ruby.
6ccc564 to
73bc2d7
Compare
__init__ through type tracking
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves Python type-tracking so that values stored into instance attributes (for example in __init__) can be recovered when those attributes are read in other methods, enabling downstream modeling (Concepts / API graphs) to recognize types such as PEP 249 connections when used outside the constructor.
Changes:
- Extend type-tracking with a no-call-graph “local field” level step to connect attribute writes (
self.attr = ...) to later reads (self.attr) across methods on the same class. - Add/extend Python library tests for PEP249 (
hdbcli) and type-tracking attribute flows to validate the new behavior. - Update experimental inline call-graph expected output to reflect improved type-tracker results through instance attributes.
Show a summary per file
| File | Description |
|---|---|
| python/ql/lib/semmle/python/dataflow/new/internal/TypeTrackingImpl.qll | Adds a new type-tracking level step intended to model flow through instance attributes across methods. |
| python/ql/test/library-tests/frameworks/hdbcli/pep249.py | Adds a regression test for a DB-API connection stored on self._conn and used in other methods. |
| python/ql/test/library-tests/dataflow/typetracking/attribute_tests.py | Updates expectations for tracking reads of attributes assigned in __init__. |
| python/ql/test/experimental/library-tests/CallGraph/code/class_attr_assign.py | Updates inline expectations to reflect type tracking through instance attributes. |
| python/ql/test/experimental/library-tests/CallGraph/InlineCallGraphTest.expected | Updates expected output to reflect the improved type-tracker behavior. |
Copilot's findings
- Files reviewed: 6/6 changed files
- Comments generated: 2
7817cfe to
3bad60c
Compare
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.
Type-tracking-based modeling (Concepts, API graphs) misses values stored in a
selfattribute and read from another method. A typical symptom is a PEP 249 connection assigned toself._connin one method not being recognized as a connection when used later, so downstream modeling such as SQL-execution sinks can be missed.Changes
TypeTrackingImpl.qll: AddlocalFieldStep, a no-call-graph type-tracking level step that models flow throughself.attracross instance methods on the same class.self.attrwith later reads of the same attribute on the same class.__init__, matching the existing instance-insensitive and order-insensitive treatment used for similar field tracking in Ruby and JavaScript.hdbcli/pep249.py: Add regression coverage for a connection stored inself._connand used both directly and via a getter.attribute_tests.py: Update expectations to reflect tracking from values stored onselfinto laterself.fooreads inside instance methods, while leaving externalinstance.fooreads unchanged..expectedfile to reflect the improved type-tracker results.Notes for reviewers
instance.fooare still not modeled by this change.Original prompt