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.
This commit is contained in:
Taus
2025-03-04 12:55:44 +00:00
parent c22b05a6f4
commit 301ebcb12b
2 changed files with 21 additions and 0 deletions

View File

@@ -4,3 +4,6 @@
| variables_test.py:86:3:86:3 | b | The global variable 'b' is not used. |
| variables_test.py:86:5:86:5 | c | The global variable 'c' is not used. |
| variables_test.py:100:1:100:8 | glob_var | The global variable 'glob_var' is not used. |
| variables_test.py:147:5:147:26 | ForwardParamAnnotation | The global variable 'ForwardParamAnnotation' is not used. |
| variables_test.py:148:5:148:27 | ForwardReturnAnnotation | The global variable 'ForwardReturnAnnotation' is not used. |
| variables_test.py:149:5:149:31 | ForwardAssignmentAnnotation | The global variable 'ForwardAssignmentAnnotation' is not used. |

View File

@@ -137,3 +137,21 @@ def test_dict_unpacking(queryset, field_name, value):
for tag in value.split(','):
queryset = queryset.filter(**{field_name + '__name': tag})
return queryset
from typing import TYPE_CHECKING
if TYPE_CHECKING:
ParamAnnotation = int
ReturnAnnotation = int
AssignmentAnnotation = int
ForwardParamAnnotation = int
ForwardReturnAnnotation = int
ForwardAssignmentAnnotation = int
def test_direct_annotation(x: ParamAnnotation) -> ReturnAnnotation:
if x:
y : AssignmentAnnotation = 1
def test_forward_annotation(x: "ForwardParamAnnotation") -> "ForwardReturnAnnotation":
if x:
y : "ForwardAssignmentAnnotation" = 1