Skip to content
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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

tausbn
Copy link
Contributor

@tausbn tausbn commented Mar 4, 2025

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.

@github-actions github-actions bot added the Python label Mar 4, 2025
@tausbn tausbn force-pushed the tausbn/python-fix-unused-global-variable-in-forward-annotation-fp branch from 41ddad0 to 0fc4806 Compare March 4, 2025 13:51
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.
@tausbn tausbn force-pushed the tausbn/python-fix-unused-global-variable-in-forward-annotation-fp branch from 0fc4806 to 301ebcb Compare March 4, 2025 13:52
tausbn added 2 commits March 4, 2025 14:41
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.
@tausbn tausbn changed the title Python: Extend test cases for "unused global var" query Python: Add support for forward references in unused var query Mar 4, 2025
@tausbn tausbn marked this pull request as ready for review March 4, 2025 15:50
@Copilot Copilot bot review requested due to automatic review settings March 4, 2025 15:50
@tausbn tausbn requested a review from a team as a code owner March 4, 2025 15:50
This way we also get annotations that appear in `Lambda`s

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

Sign up for free to 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.

1 participant