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: False Positive on Global Type Variable #18910

Open
cariad-robert-abel opened this issue Mar 3, 2025 · 1 comment
Open

Python: False Positive on Global Type Variable #18910

cariad-robert-abel opened this issue Mar 3, 2025 · 1 comment

Comments

@cariad-robert-abel
Copy link

cariad-robert-abel commented Mar 3, 2025

A globally defined type variable, usually a short-hand, is reported as being an unused global variable py/unused-global-variable, when it reality it's not, because it's used in a string type annotation.

Our code is closed source, so unfortunately a made up example will have illustrate the false positive:

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    JiraIssue = dict[str, str]

def titlecase_jira_issues(issues: list['JiraIssue']):
    
    for issue in issues:
        issue['title'] = issue['title'].title()

In the snippet above, JiraIssue is reported as unused. We're using the codeql-bundle-v2.20.5/codeql-bundle-linux64.tar.zst.

The closest related issue I could find deals with a similar case involving unused imports, see #2042.

tausbn added a commit that referenced this issue 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.
@mbg
Copy link
Member

mbg commented Mar 4, 2025

Hi @cariad-robert-abel 👋🏻

Thanks for reporting this! I have run this past the relevant engineering team and they confirm that support for string type annotations in Python is currently limited here. We will keep track of this issue internally. Although fixing false positives is not a current product priority and I cannot say how long it may take until this is fixed, the engineering team is working on a potential fix for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants