Skip to content

ISSUE 23 : correction if typeVar exclusion#132

Merged
dedece35 merged 2 commits intomainfrom
ISSUE_23
Feb 17, 2026
Merged

ISSUE 23 : correction if typeVar exclusion#132
dedece35 merged 2 commits intomainfrom
ISSUE_23

Conversation

@dedece35
Copy link
Member

No description provided.

@dedece35 dedece35 linked an issue Feb 17, 2026 that may be closed by this pull request
@dedece35 dedece35 self-assigned this Feb 17, 2026
@dedece35 dedece35 requested a review from Copilot February 17, 2026 22:06
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the AvoidGlobalVariableInFunctionCheck to avoid flagging Python typing constructs (e.g., TypeVar) as “global variables”, aligning the rule behavior with common typing patterns.

Changes:

  • Adjusts global-variable collection to skip module-level assignments that are TypeVar/ParamSpec/NewType/TypeVarTuple constructions.
  • Adds a compliant test case verifying that TypeVar usage inside a function does not raise an issue.
  • Updates the JUnit test to validate both a noncompliant and a compliant sample file.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/main/java/org/greencodeinitiative/creedengo/python/checks/AvoidGlobalVariableInFunctionCheck.java Reworks module-global detection and adds typing-construct exclusion logic.
src/test/java/org/greencodeinitiative/creedengo/python/checks/AvoidGlobalVariableInFunctionCheckTest.java Updates verification to include a “no issue” compliant case (and adjusts visibility).
src/test/resources/checks/avoidGlobalVariableInFunctionCompliant.py Makes the TypeVar sample compliant by removing the “Noncompliant” marker.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 102 to +110
public void visitFileInput(SubscriptionContext ctx) {
FileInput fileInput = (FileInput) ctx.syntaxNode();
fileInput.globalVariables().stream().filter(v -> v.is(Symbol.Kind.OTHER)).forEach(v -> this.globalVariables.add(v.name()));

// Add all module-level assignments except TypeVar and similar typing constructs
StatementList statements = fileInput.statements();
if (statements != null) {
statements.statements().forEach(this::extractGlobalVariablesFromStatement);
}
}
Copy link

Copilot AI Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

visitFileInput only inspects the direct children of fileInput.statements() and extractGlobalVariablesFromStatement only handles ASSIGNMENT_STMT / ANNOTATED_ASSIGNMENT. Module-scope assignments inside compound statements (e.g., if, try, for, with, etc.) will be ignored, so globals defined there won’t be tracked and their usage inside functions will no longer be reported (regression vs the previous symbol-based approach). Consider collecting globals via a recursive traversal/visitor over the full module AST (while still excluding TypeVar-like constructs), or reusing fileInput.globalVariables() and filtering out typing-construct definitions more precisely.

Copilot uses AI. Check for mistakes.
@sonarqubecloud
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
54.8% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@dedece35 dedece35 merged commit 398c855 into main Feb 17, 2026
9 of 10 checks passed
@dedece35 dedece35 deleted the ISSUE_23 branch February 17, 2026 22:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EC4 - False positives with TypeVar

1 participant

Comments