Skip to content

Commit 17a116c

Browse files
authored
Merge pull request #20 from green-code-initiative/ISSUE_17
EC7 - correction setter problem on constructor method
2 parents ca535c6 + 631365e commit 17a116c

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Changed
1515

16+
- [#17](https://github.com/green-code-initiative/ecoCode-python/issues/17) EC7 - correction setter problem on constructor method
17+
1618
### Deleted
1719

1820
## [1.4.2] - 2024-01-11

src/main/java/fr/greencodeinitiative/python/checks/AvoidGettersAndSetters.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public class AvoidGettersAndSetters extends PythonSubscriptionCheck {
4444
public void initialize(Context context) {
4545
context.registerSyntaxNodeConsumer(Tree.Kind.FUNCDEF, ctx -> {
4646
FunctionDef functionDef = (FunctionDef) ctx.syntaxNode();
47+
48+
if (isConstructorMethod(functionDef)) {
49+
return; // Ignore constructors
50+
}
51+
4752
StatementList statementList = functionDef.body();
4853
List<Statement> statements = statementList.statements();
4954
if (functionDef.parent().parent().is(Tree.Kind.CLASSDEF)) {
@@ -53,6 +58,10 @@ public void initialize(Context context) {
5358
});
5459
}
5560

61+
private boolean isConstructorMethod(FunctionDef functionDef) {
62+
return functionDef.name() != null && "__init__".equals(functionDef.name().name());
63+
}
64+
5665
public void checkAllSetters(List<Statement> statements, FunctionDef functionDef, SubscriptionContext ctx) {
5766
if (statements.size() == 1 && statements.get(0).is(Tree.Kind.ASSIGNMENT_STMT)) {
5867
AssignmentStatement assignmentStatement = (AssignmentStatement) statements.get(0);

src/test/resources/checks/avoidGettersAndSettersCompliant.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
from datetime import date
22

3+
class Something:
4+
def __init__(self, value):
5+
self.value = value
6+
37
class Client():
48

59
def __init__(self, age, weight):

0 commit comments

Comments
 (0)