Skip to content

GCI1442 [Team TREE][2025] - Reduce memory footprint of dataclasses#82

Merged
dedece35 merged 6 commits intogreen-code-initiative:mainfrom
fabienhusseperso:1442-PYTHON
Feb 14, 2026
Merged

GCI1442 [Team TREE][2025] - Reduce memory footprint of dataclasses#82
dedece35 merged 6 commits intogreen-code-initiative:mainfrom
fabienhusseperso:1442-PYTHON

Conversation

@fabienhusseperso
Copy link
Copy Markdown
Contributor

@fabienhusseperso fabienhusseperso commented May 21, 2025

Added rule GCI1442.

Since python 3.10 dataclass instances can easily enable a memory optimization
By default, when we define a class in Python, it uses a dict to store its attributes and methods.
This means that every instance of the class has an extensible dictionary to store its data, this flexibility comes at the cost of memory usage.
When we use slots, Python creates a tuple to store the attributes of the class instead of a dictionary, reducing memory usage.

Reference :
green-code-initiative/creedengo-challenge#32
green-code-initiative/creedengo-rules-specifications#409

Copy link
Copy Markdown

@MP-Aubay MP-Aubay left a comment

Choose a reason for hiding this comment

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

PR seems good (just remove overring of creedengo-rules-specifications)

@github-actions
Copy link
Copy Markdown

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!

Copy link
Copy Markdown
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

Adds a new Python analyzer rule (GCI1442) to encourage using @dataclass(slots=True) (Python ≥ 3.10) to reduce dataclass instance memory footprint, and wires it into the plugin’s rule registration, profiles, and tests.

Changes:

  • Implement new rule UsingSlotsOnDataClasses (GCI1442) with Python-version gating (≥ 3.10).
  • Add unit + integration tests and Python sample files (compliant/noncompliant) for the new rule.
  • Register the rule in the rule repository and include it in the default “creedengo way” profile + changelog.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
src/main/java/org/greencodeinitiative/creedengo/python/checks/UsingSlotsOnDataClasses.java New rule implementation detecting missing slots=True on @dataclass decorators (Python ≥ 3.10).
src/main/java/org/greencodeinitiative/creedengo/python/PythonRuleRepository.java Registers the new rule class in the plugin repository.
src/main/resources/org/greencodeinitiative/creedengo/python/creedengo_way_profile.json Enables GCI1442 in the default profile.
src/test/java/org/greencodeinitiative/creedengo/python/checks/UsingSlotsOnDataClassesTest.java Unit tests for compliant/noncompliant samples and version gating behavior.
src/test/resources/checks/usingSlotsOnDataClassesNonCompliant.py Noncompliant examples expected to raise issues.
src/test/resources/checks/usingSlotsOnDataClassesCompliant.py Compliant example using @dataclass(slots=True).
src/test/resources/checks/usingSlotsOnDataClassesCompliantV39.py Sample used to validate “no issue” behavior on Python 3.9.
src/it/test-projects/creedengo-python-plugin-test-project/src/usingSlotsOnDataClassesNonCompliant.py Integration-test noncompliant examples.
src/it/test-projects/creedengo-python-plugin-test-project/src/usingSlotsOnDataClassesCompliant.py Integration-test compliant example.
src/it/java/org/greencodeinitiative/creedengo/python/integration/tests/GCIRulesIT.java Adds IT assertions for GCI1442.
CHANGELOG.md Notes addition of the new rule.

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

if (decorator.arguments() != null
&& decorator.arguments().arguments() != null
&& decorator.arguments().arguments().stream()
.anyMatch(argument -> argument.firstToken().value().equals(SLOTS_ARG))) {
Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

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

The check currently treats any @dataclass(..., slots=...) as compliant, regardless of the value. This means @dataclass(slots=False) (or slots=None) would incorrectly bypass the issue. Update the argument inspection to verify that the slots keyword argument is explicitly set to True before returning; otherwise, raise the issue.

Suggested change
.anyMatch(argument -> argument.firstToken().value().equals(SLOTS_ARG))) {
.anyMatch(argument -> argument.keyword() != null
&& SLOTS_ARG.equals(argument.keyword().name())
&& argument.value() != null
&& argument.value().is(Tree.Kind.TRUE))) {

Copilot uses AI. Check for mistakes.
Comment on lines +50 to +52
&& decorator.arguments().arguments().stream()
.anyMatch(argument -> argument.firstToken().value().equals(SLOTS_ARG))) {
return;
Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

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

There is no test covering the case where slots is present but disabled (e.g., @dataclass(slots=False)), which would currently be treated as compliant. Add a noncompliant test case asserting an issue is raised when slots is explicitly set to a non-True value.

Copilot uses AI. Check for mistakes.
@dedece35 dedece35 merged commit 30a754c into green-code-initiative:main Feb 14, 2026
1 check failed
@dedece35
Copy link
Copy Markdown
Member

Hi @fabienhusseperso, @MP-Aubay

I merge this PR even if some best practices are missing.
I'm going to make some corrections in main branch and next I will create a new release.

thank you for the work !

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.

4 participants