Skip to content

Conversation

@cleophass
Copy link
Contributor

Verified that the rule does not exist.
Confirmed that the rule is not listed in Rules.MD, so a new ID GCI107 was created.
Added a corresponding Python unit test.
Updated the CHANGELOG accordingly.

@github-actions
Copy link

github-actions bot commented Jul 4, 2025

This PR has been automatically marked as stale because it has no activity for 30 days.
Please add a comment if you want to keep the issue open. Thank you for your contributions!

@github-actions github-actions bot added the stale label Jul 4, 2025
@@ -0,0 +1,102 @@
/*
Copy link
Member

Choose a reason for hiding this comment

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

delete this class and use UtilsAST already available on "main"

@github-actions github-actions bot removed the stale label Aug 1, 2025

### Added

- [#86](https://github.com/green-code-initiative/creedengo-python/pull/86) Add rule GCI107 Torch from numpy, the rule isn't finished yet
Copy link
Member

Choose a reason for hiding this comment

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

  • use GCI109 rule id in all your PR
  • please add an IT
  • add a new PR with GCI109 in rules-spec projet

@dedece35 dedece35 requested a review from Copilot August 27, 2025 21:25
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

This PR introduces a new static analysis rule GCI107 that detects inefficient patterns when creating PyTorch tensors from NumPy arrays. The rule enforces using torch.from_numpy() instead of torch.tensor() for better memory efficiency and performance.

  • Implements rule GCI107 to encourage memory-efficient tensor creation from NumPy arrays
  • Adds comprehensive test coverage for various import patterns and usage scenarios
  • Creates utility functions to support AST analysis for the new rule

Reviewed Changes

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

Show a summary per file
File Description
src/main/java/org/greencodeinitiative/creedengo/python/checks/UseTorchFromNumpy.java Main rule implementation that detects torch.tensor() calls with NumPy arrays
src/main/java/org/greencodeinitiative/creedengo/python/checks/Utils.java Utility class providing helper methods for AST analysis
src/test/java/org/greencodeinitiative/creedengo/python/checks/UseTorchFromNumpyTest.java Unit test class for the new rule
src/test/resources/checks/useTorchFromNumpy.py Test file with various scenarios for compliant and non-compliant code
src/main/java/org/greencodeinitiative/creedengo/python/PythonRuleRepository.java Registers the new rule in the repository
CHANGELOG.md Documents the addition of the new rule

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

private void visitCallExpression(SubscriptionContext ctx) {
var callExpression = (CallExpression) ctx.syntaxNode();

if (!TORCH_TENSOR_FUNCTION.equals(Utils.getQualifiedName(callExpression)) && !TORCH_TENSOR_FUNCTION.equals(callExpression.callee().firstToken().value()+"."+callExpression.calleeSymbol().name())) {
Copy link

Copilot AI Aug 27, 2025

Choose a reason for hiding this comment

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

The logic is inverted - this condition will return early when it should continue processing. It should use OR (||) instead of AND (&&) to check if NEITHER condition matches torch.tensor.

Suggested change
if (!TORCH_TENSOR_FUNCTION.equals(Utils.getQualifiedName(callExpression)) && !TORCH_TENSOR_FUNCTION.equals(callExpression.callee().firstToken().value()+"."+callExpression.calleeSymbol().name())) {
if (!TORCH_TENSOR_FUNCTION.equals(Utils.getQualifiedName(callExpression)) || !TORCH_TENSOR_FUNCTION.equals(callExpression.callee().firstToken().value()+"."+callExpression.calleeSymbol().name())) {

Copilot uses AI. Check for mistakes.
private void visitCallExpression(SubscriptionContext ctx) {
var callExpression = (CallExpression) ctx.syntaxNode();

if (!TORCH_TENSOR_FUNCTION.equals(Utils.getQualifiedName(callExpression)) && !TORCH_TENSOR_FUNCTION.equals(callExpression.callee().firstToken().value()+"."+callExpression.calleeSymbol().name())) {
Copy link

Copilot AI Aug 27, 2025

Choose a reason for hiding this comment

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

Potential NullPointerException when callExpression.calleeSymbol() returns null. The code should check for null before calling .name().

Suggested change
if (!TORCH_TENSOR_FUNCTION.equals(Utils.getQualifiedName(callExpression)) && !TORCH_TENSOR_FUNCTION.equals(callExpression.callee().firstToken().value()+"."+callExpression.calleeSymbol().name())) {
var calleeSymbol = callExpression.calleeSymbol();
if (!TORCH_TENSOR_FUNCTION.equals(Utils.getQualifiedName(callExpression)) &&
!(calleeSymbol != null && TORCH_TENSOR_FUNCTION.equals(callExpression.callee().firstToken().value() + "." + calleeSymbol.name()))) {

Copilot uses AI. Check for mistakes.

### Added

- [#86](https://github.com/green-code-initiative/creedengo-python/pull/86) Add rule GCI107 Torch from numpy, the rule isn't finished yet
Copy link

Copilot AI Aug 27, 2025

Choose a reason for hiding this comment

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

The changelog entry states 'the rule isn't finished yet' which appears inconsistent with a completed implementation. This should be updated to reflect the actual state.

Suggested change
- [#86](https://github.com/green-code-initiative/creedengo-python/pull/86) Add rule GCI107 Torch from numpy, the rule isn't finished yet
- [#86](https://github.com/green-code-initiative/creedengo-python/pull/86) Add rule GCI107 Torch from numpy. This rule checks for efficient use of Torch tensors created from NumPy arrays.

Copilot uses AI. Check for mistakes.
@dedece35 dedece35 assigned dedece35 and cleophass and unassigned dedece35 Sep 1, 2025
@dedece35 dedece35 marked this pull request as draft September 1, 2025 20:26
@github-actions
Copy link

github-actions bot commented Oct 2, 2025

This PR has been automatically marked as stale because it has no activity for 30 days.
Please add a comment if you want to keep the issue open. Thank you for your contributions!

@github-actions github-actions bot added the stale label Oct 2, 2025
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.

3 participants