Skip to content

Commit 301ebcb

Browse files
committed
Python: Extend test cases for "unused global var" query
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.
1 parent c22b05a commit 301ebcb

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

python/ql/test/query-tests/Variables/unused/UnusedModuleVariable.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
| variables_test.py:86:3:86:3 | b | The global variable 'b' is not used. |
55
| variables_test.py:86:5:86:5 | c | The global variable 'c' is not used. |
66
| variables_test.py:100:1:100:8 | glob_var | The global variable 'glob_var' is not used. |
7+
| variables_test.py:147:5:147:26 | ForwardParamAnnotation | The global variable 'ForwardParamAnnotation' is not used. |
8+
| variables_test.py:148:5:148:27 | ForwardReturnAnnotation | The global variable 'ForwardReturnAnnotation' is not used. |
9+
| variables_test.py:149:5:149:31 | ForwardAssignmentAnnotation | The global variable 'ForwardAssignmentAnnotation' is not used. |

python/ql/test/query-tests/Variables/unused/variables_test.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,21 @@ def test_dict_unpacking(queryset, field_name, value):
137137
for tag in value.split(','):
138138
queryset = queryset.filter(**{field_name + '__name': tag})
139139
return queryset
140+
141+
from typing import TYPE_CHECKING
142+
143+
if TYPE_CHECKING:
144+
ParamAnnotation = int
145+
ReturnAnnotation = int
146+
AssignmentAnnotation = int
147+
ForwardParamAnnotation = int
148+
ForwardReturnAnnotation = int
149+
ForwardAssignmentAnnotation = int
150+
151+
def test_direct_annotation(x: ParamAnnotation) -> ReturnAnnotation:
152+
if x:
153+
y : AssignmentAnnotation = 1
154+
155+
def test_forward_annotation(x: "ForwardParamAnnotation") -> "ForwardReturnAnnotation":
156+
if x:
157+
y : "ForwardAssignmentAnnotation" = 1

0 commit comments

Comments
 (0)