Python: Support type annotations in call graph#19672
Merged
Conversation
Adds support for tracking instances via type annotations. Also adds a convenience method to the newly added `Annotation` class, `getAnnotatedExpression`, that returns the expression that is annotated with the given type. For return annotations this is any value returned from the annotated function in question. Co-authored-by: Napalys Klicius <napalys@github.com>
Co-authored-by: Napalys Klicius <napalys@github.com>
Also fixes an issue with the return type annotations that caused these to not work properly. Currently, annotated assignments don't work properly, due to the fact that our flow relation doesn't consider flow going to the "type" part of an annotated assignment. This means that in `x : Foo`, we do correctly note that `x` is annotated with `Foo`, but we have no idea what `Foo` is, since it has no incoming flow. To fix this we should probably just extend the flow relation, but this may need to be done with some care, so I have left it as future work.
cf8c8bc to
c6c6a85
Compare
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR adds support for type annotations in the call graph tracking system, enabling better analysis of method calls on annotated variables. The implementation allows the call graph to recognize that variables with type annotations (like x: Foo) may be instances of the annotated type, improving static analysis accuracy.
Key changes:
- Enhanced call graph to track instances through type annotations
- Added
getAnnotatedExpression()method to theAnnotationclass for retrieving annotated expressions - Extended instance tracking to include parameter, return, and variable type annotations
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| type_annotations.py | Test cases demonstrating type annotation tracking for parameters, returns, and variables |
| InlineCallGraphTest.qlref | Reference file for running call graph tests |
| DataFlowDispatch.qll | Core implementation adding annotation-based instance tracking to the call graph |
| Exprs.qll | New getAnnotatedExpression() method in the Annotation class |
| 2025-06-04-call-graph-type-annotations.md | Release notes documenting the new feature |
Napalys
approved these changes
Jul 11, 2025
Contributor
Napalys
left a comment
There was a problem hiding this comment.
Thank you, looks good to me
![]()
Contributor
Author
|
DCA looks uneventful. I'll go ahead with the merge. |
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 support for tracking instances via type annotations. Also adds a convenience method to the newly added
Annotationclass,getAnnotatedExpression, that returns the expression that is annotated with the given type. For return annotations this is any value returned from the annotated function in question.