GCI1442 [Team TREE][2025] - Reduce memory footprint of dataclasses#82
Conversation
MP-Aubay
left a comment
There was a problem hiding this comment.
PR seems good (just remove overring of creedengo-rules-specifications)
|
This PR has been automatically marked as stale because it has no activity for 30 days. |
There was a problem hiding this comment.
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))) { |
There was a problem hiding this comment.
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.
| .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))) { |
src/main/java/org/greencodeinitiative/creedengo/python/checks/UsingSlotsOnDataClasses.java
Show resolved
Hide resolved
| && decorator.arguments().arguments().stream() | ||
| .anyMatch(argument -> argument.firstToken().value().equals(SLOTS_ARG))) { | ||
| return; |
There was a problem hiding this comment.
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.
src/it/java/org/greencodeinitiative/creedengo/python/integration/tests/GCIRulesIT.java
Show resolved
Hide resolved
|
Hi @fabienhusseperso, @MP-Aubay I merge this PR even if some best practices are missing. thank you for the work ! |
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