-
Notifications
You must be signed in to change notification settings - Fork 22
GCI1442 [Team TREE][2025] - Reduce memory footprint of dataclasses #82
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
Open
fabienhusseperso
wants to merge
5
commits into
green-code-initiative:main
Choose a base branch
from
fabienhusseperso:1442-PYTHON
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e6b970c
:sparkles: feat: add rule to check using slots on data classes
fabienhusseperso d083e3b
:sparkles: feat: add it tests, update rule id and changelog
fabienhusseperso 0f08288
:shower: fix: py test file
fabienhusseperso fcca0b8
:heavy_check_mark: feat: add python version check
fabienhusseperso f06defc
:bug: fix: rules specifications version
fabienhusseperso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
...est-projects/creedengo-python-plugin-test-project/src/usingSlotsOnDataClassesCompliant.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| @dataclass(slots=True) | ||
| class MyClass: | ||
| a: int | ||
| b: int | ||
| c: int |
17 changes: 17 additions & 0 deletions
17
...-projects/creedengo-python-plugin-test-project/src/usingSlotsOnDataClassesNonCompliant.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| @dataclass | ||
| class MyClass1: | ||
| a: int | ||
| b: int | ||
| c: int | ||
|
|
||
| @dataclass() | ||
| class MyClass2: | ||
| a: int | ||
| b: int | ||
| c: int | ||
|
|
||
| @dataclass(frozen=True) | ||
| class MyClass3: | ||
| a: int | ||
| b: int | ||
| c: int |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/main/java/org/greencodeinitiative/creedengo/python/checks/UsingSlotsOnDataClasses.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| * creedengo - Python language - Provides rules to reduce the environmental footprint of your Python programs | ||
| * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org) | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package org.greencodeinitiative.creedengo.python.checks; | ||
|
|
||
| import org.sonar.check.Rule; | ||
| import org.sonar.plugins.python.api.ProjectPythonVersion; | ||
| import org.sonar.plugins.python.api.PythonSubscriptionCheck; | ||
| import org.sonar.plugins.python.api.SubscriptionContext; | ||
| import org.sonar.plugins.python.api.tree.*; | ||
|
|
||
| @Rule(key = "GCI1442") | ||
| public class UsingSlotsOnDataClasses extends PythonSubscriptionCheck { | ||
|
|
||
| private static final String DECORATOR_DATA_CLASS = "dataclass"; | ||
| private static final String SLOTS_ARG = "slots"; | ||
|
|
||
| public static final String DESCRIPTION = "Reduce memory footprint by using @dataclass(slots=True)"; | ||
|
|
||
| @Override | ||
| public void initialize(Context context) { | ||
| if(ProjectPythonVersion.currentVersions().stream().anyMatch(version -> version.compare(3, 10) >= 0)) { | ||
| context.registerSyntaxNodeConsumer(Tree.Kind.DECORATOR, this::isUsingSlots); | ||
| } | ||
| } | ||
|
|
||
| private void isUsingSlots(SubscriptionContext ctx) { | ||
| Decorator decorator = ((Decorator) ctx.syntaxNode()); | ||
|
|
||
| if (!isDecoratorDataClass(decorator)) { | ||
| return; | ||
| } | ||
|
|
||
| if (decorator.arguments() != null | ||
| && decorator.arguments().arguments() != null | ||
| && decorator.arguments().arguments().stream() | ||
| .anyMatch(argument -> argument.firstToken().value().equals(SLOTS_ARG))) { | ||
| return; | ||
| } | ||
|
|
||
| ctx.addIssue(decorator, DESCRIPTION); | ||
| } | ||
|
|
||
| private boolean isDecoratorDataClass(Decorator decorator) { | ||
| Name name = null; | ||
| // Manage decorator detected as simple expression | ||
| if (decorator.expression().is(Tree.Kind.NAME)) { | ||
| name = (Name) decorator.expression(); | ||
| // manage decorator detected as callable expression | ||
| } else if(decorator.expression().is(Tree.Kind.CALL_EXPR)) { | ||
| CallExpression callExpression = (CallExpression) decorator.expression(); | ||
| if (callExpression.callee().is(Tree.Kind.NAME)) { | ||
| name = (Name) callExpression.callee(); | ||
| } | ||
| } | ||
| return name != null && UsingSlotsOnDataClasses.DECORATOR_DATA_CLASS.equals(name.name()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
| "GCI74", | ||
| "GCI89", | ||
| "GCI203", | ||
| "GCI404" | ||
| "GCI404", | ||
| "GCI1442" | ||
| ] | ||
| } | ||
42 changes: 42 additions & 0 deletions
42
...est/java/org/greencodeinitiative/creedengo/python/checks/UsingSlotsOnDataClassesTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * creedengo - Python language - Provides rules to reduce the environmental footprint of your Python programs | ||
| * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org) | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package org.greencodeinitiative.creedengo.python.checks; | ||
|
|
||
| import org.junit.Test; | ||
| import org.sonar.plugins.python.api.ProjectPythonVersion; | ||
| import org.sonar.plugins.python.api.PythonVersionUtils; | ||
| import org.sonar.python.checks.utils.PythonCheckVerifier; | ||
|
|
||
| import java.util.Set; | ||
|
|
||
| public class UsingSlotsOnDataClassesTest { | ||
|
|
||
| @Test | ||
| public void test_using_slots_on_data_classes() { | ||
| ProjectPythonVersion.setCurrentVersions(PythonVersionUtils.allVersions()); | ||
| PythonCheckVerifier.verify("src/test/resources/checks/usingSlotsOnDataClassesNonCompliant.py", new UsingSlotsOnDataClasses()); | ||
| PythonCheckVerifier.verifyNoIssue("src/test/resources/checks/usingSlotsOnDataClassesCompliant.py", new UsingSlotsOnDataClasses()); | ||
| } | ||
|
|
||
| @Test | ||
| public void test_using_slots_on_data_classes_python_less_than_310() { | ||
| ProjectPythonVersion.setCurrentVersions(Set.of(PythonVersionUtils.Version.V_39)); | ||
| PythonCheckVerifier.verifyNoIssue("src/test/resources/checks/usingSlotsOnDataClassesCompliantV39.py", new UsingSlotsOnDataClasses()); | ||
| } | ||
|
|
||
| } |
6 changes: 6 additions & 0 deletions
6
src/test/resources/checks/usingSlotsOnDataClassesCompliant.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # COMPLIANT | ||
| @dataclass(slots=True) | ||
| class MyClass: | ||
| a: int | ||
| b: int | ||
| c: int |
17 changes: 17 additions & 0 deletions
17
src/test/resources/checks/usingSlotsOnDataClassesCompliantV39.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| @dataclass | ||
| class MyClass: | ||
| a: int | ||
| b: int | ||
| c: int | ||
|
|
||
| @dataclass() | ||
| class MyClass: | ||
| a: int | ||
| b: int | ||
| c: int | ||
|
|
||
| @dataclass(frozen=True) | ||
| class MyClass: | ||
| a: int | ||
| b: int | ||
| c: int |
17 changes: 17 additions & 0 deletions
17
src/test/resources/checks/usingSlotsOnDataClassesNonCompliant.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| @dataclass # Noncompliant {{Reduce memory footprint by using @dataclass(slots=True)}} | ||
| class MyClass: | ||
| a: int | ||
| b: int | ||
| c: int | ||
|
|
||
| @dataclass() # Noncompliant {{Reduce memory footprint by using @dataclass(slots=True)}} | ||
| class MyClass: | ||
| a: int | ||
| b: int | ||
| c: int | ||
|
|
||
| @dataclass(frozen=True) # Noncompliant {{Reduce memory footprint by using @dataclass(slots=True)}} | ||
| class MyClass: | ||
| a: int | ||
| b: int | ||
| c: int |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not be commited
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes thank you. Done