From 1505bf856d15129f36f90c7a03846bc638bf7239 Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Thu, 1 Feb 2024 22:23:20 +0100 Subject: [PATCH 1/4] [ISSUE 17] EC7 - correction setter problem on constructor method --- .../python/checks/AvoidGettersAndSetters.java | 9 +++++++++ .../resources/checks/avoidGettersAndSettersCompliant.py | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/src/main/java/fr/greencodeinitiative/python/checks/AvoidGettersAndSetters.java b/src/main/java/fr/greencodeinitiative/python/checks/AvoidGettersAndSetters.java index a88f4df..04c8783 100644 --- a/src/main/java/fr/greencodeinitiative/python/checks/AvoidGettersAndSetters.java +++ b/src/main/java/fr/greencodeinitiative/python/checks/AvoidGettersAndSetters.java @@ -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 statements = statementList.statements(); if (functionDef.parent().parent().is(Tree.Kind.CLASSDEF)) { @@ -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 statements, FunctionDef functionDef, SubscriptionContext ctx) { if (statements.size() == 1 && statements.get(0).is(Tree.Kind.ASSIGNMENT_STMT)) { AssignmentStatement assignmentStatement = (AssignmentStatement) statements.get(0); diff --git a/src/test/resources/checks/avoidGettersAndSettersCompliant.py b/src/test/resources/checks/avoidGettersAndSettersCompliant.py index 13fa802..f0af415 100644 --- a/src/test/resources/checks/avoidGettersAndSettersCompliant.py +++ b/src/test/resources/checks/avoidGettersAndSettersCompliant.py @@ -1,5 +1,9 @@ from datetime import date +class Something: + def __init__(self, value): + self.value = value + class Client(): def __init__(self, age, weight): From 4791e29995606a6f811e8995719ad20340d089bc Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Thu, 1 Feb 2024 22:25:18 +0100 Subject: [PATCH 2/4] [ISSUE 17] EC7 - correction setter problem on constructor method - CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bf8fea..9450223 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - [#18](https://github.com/green-code-initiative/ecoCode-python/issues/18) Add support for SonarQube 10.4 "DownloadOnlyWhenRequired" feature +- [#17](https://github.com/green-code-initiative/ecoCode-python/issues/17) EC7 - correction setter problem on constructor method" ### Changed From 5c72ad16cf89773cf28ebd6ef9117e116e4caec6 Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Thu, 1 Feb 2024 23:24:07 +0100 Subject: [PATCH 3/4] [ISSUE 17] EC7 - correction setter problem on constructor method - CHANGELOG update --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9450223..8504141 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,10 +10,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - [#18](https://github.com/green-code-initiative/ecoCode-python/issues/18) Add support for SonarQube 10.4 "DownloadOnlyWhenRequired" feature -- [#17](https://github.com/green-code-initiative/ecoCode-python/issues/17) EC7 - correction setter problem on constructor method" ### 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 From 631365e08a52179b4f2fed6b827753c0b3c0e071 Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Thu, 1 Feb 2024 23:25:21 +0100 Subject: [PATCH 4/4] [ISSUE 17] EC7 - correction setter problem on constructor method - CHANGELOG update bis --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8504141..45486cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ 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" +- [#17](https://github.com/green-code-initiative/ecoCode-python/issues/17) EC7 - correction setter problem on constructor method ### Deleted