Skip to content

Commit

Permalink
Merge pull request #20 from green-code-initiative/ISSUE_17
Browse files Browse the repository at this point in the history
EC7 - correction setter problem on constructor method
  • Loading branch information
dedece35 authored Feb 1, 2024
2 parents ca535c6 + 631365e commit 17a116c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

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

### Deleted

## [1.4.2] - 2024-01-11
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public class AvoidGettersAndSetters extends PythonSubscriptionCheck {
public void initialize(Context context) {
context.registerSyntaxNodeConsumer(Tree.Kind.FUNCDEF, ctx -> {
FunctionDef functionDef = (FunctionDef) ctx.syntaxNode();

if (isConstructorMethod(functionDef)) {
return; // Ignore constructors
}

StatementList statementList = functionDef.body();
List<Statement> statements = statementList.statements();
if (functionDef.parent().parent().is(Tree.Kind.CLASSDEF)) {
Expand All @@ -53,6 +58,10 @@ public void initialize(Context context) {
});
}

private boolean isConstructorMethod(FunctionDef functionDef) {
return functionDef.name() != null && "__init__".equals(functionDef.name().name());
}

public void checkAllSetters(List<Statement> statements, FunctionDef functionDef, SubscriptionContext ctx) {
if (statements.size() == 1 && statements.get(0).is(Tree.Kind.ASSIGNMENT_STMT)) {
AssignmentStatement assignmentStatement = (AssignmentStatement) statements.get(0);
Expand Down
4 changes: 4 additions & 0 deletions src/test/resources/checks/avoidGettersAndSettersCompliant.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from datetime import date

class Something:
def __init__(self, value):
self.value = value

class Client():

def __init__(self, age, weight):
Expand Down

0 comments on commit 17a116c

Please sign in to comment.