-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python: Add support for forward references in unused var query #18921
base: main
Are you sure you want to change the base?
Python: Add support for forward references in unused var query #18921
Conversation
41ddad0
to
0fc4806
Compare
Adds two test cases having to do with type annotations. The first one demonstrates that type annotations (even if they are never executed by the Python interpreter) count as uses for the purposes of the unused variable query. The second one demonstrates that this is _not_ the case if all such uses are inside strings (i.e. forward declarations), as we do not currently inspect the content of these strings.
0fc4806
to
301ebcb
Compare
Fixes the false positive reported in #18910 Adds a new `Annotation` class (subclass of `Expr`) which encompasses all possible kinds of annotations in Python. Using this, we look for string literals which are part of an annotation, and which have the same content as the name of a (potentially) unused global variable, and in that case we do not produce an alert. In future, we may want to support inspecting such string literals more deeply (e.g. to support stuff like "list[unused_var]"), but I think for now this level of support is sufficient.
This way we also get annotations that appear in `Lambda`s
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Overview
This PR improves the unused variable query by adding support for forward references, helping to reduce false positives when string literals in annotations match unused global variable names.
- Added test functions for both direct and forward annotations in the test file.
- Updated change notes to document the new behavior concerning forward references.
Reviewed Changes
File | Description |
---|---|
python/ql/test/query-tests/Variables/unused/variables_test.py | Adds tests for both direct and forward annotations to ensure unused globals used only in annotations aren’t flagged. |
python/ql/src/change-notes/2025-03-04-fix-forward-annotation-fp-in-unused-global-var-query.md | Documents the change that forward reference annotations now prevent false positives in the unused global variable query. |
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
Comments suppressed due to low confidence (2)
python/ql/test/query-tests/Variables/unused/variables_test.py:151
- [nitpick] Consider adding explicit assertions within test_direct_annotation to verify that the query behaves as expected regarding unused variables in annotations.
def test_direct_annotation(x: ParamAnnotation) -> ReturnAnnotation:
python/ql/test/query-tests/Variables/unused/variables_test.py:155
- [nitpick] Consider documenting the expected outcome of test_forward_annotation, such as adding inline comments or assertions to clarify that forward-referenced annotations should not be flagged.
def test_forward_annotation(x: "ForwardParamAnnotation") -> "ForwardReturnAnnotation":
Tip: Copilot only keeps its highest confidence comments to reduce noise and keep you focused. Learn more
Fixes the false positive reported in #18910
Adds a new
Annotation
class (subclass ofExpr
) which encompasses all possible kinds of annotations in Python.Using this, we look for string literals which are part of an annotation, and which have the same content as the name of a (potentially) unused global variable, and in that case we do not produce an alert.
In future, we may want to support inspecting such string literals more deeply (e.g. to support stuff like "list[unused_var]"), but I think for now this level of support is sufficient.