Skip to content

Commit d078ae5

Browse files
authored
SONARPY-2097: Fix incomplete quickfix message (#2014)
1 parent 69bad99 commit d078ae5

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

python-checks/src/main/java/org/sonar/python/checks/SklearnCachedPipelineDontAccessTransformersCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ private static Stream<Name> extractFromPipeline(Expression stepArgumentExpressio
146146

147147
private static Optional<PythonQuickFix> getQuickFix(Name pipelineBindingVariable, Tree name, QualifiedExpression qualifiedExpression, Map<Name, String> nameToStepName) {
148148
return Optional.ofNullable(nameToStepName.get(name))
149-
.map(stepName -> PythonQuickFix.newQuickFix("Access the property through the ")
149+
.map(stepName -> PythonQuickFix.newQuickFix("Replace the direct access to the transformer with an access to the `named_steps` attribute of the pipeline.")
150150
.addTextEdit(TextEditUtils.replace(qualifiedExpression.qualifier(), String.format("%s.named_steps[\"%s\"]", pipelineBindingVariable.name(), stepName)))
151151
.build());
152152
}

python-checks/src/test/java/org/sonar/python/checks/SklearnCachedPipelineDontAccessTransformersCheckTest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,20 @@ void test_quickfix1(){
5858
print(pipeline.named_steps["scaler"].center_)
5959
"""
6060
);
61+
PythonQuickFixVerifier.verifyQuickFixMessages(
62+
new SklearnCachedPipelineDontAccessTransformersCheck(),
63+
"""
64+
from sklearn.pipeline import Pipeline
65+
scaler = RobustScaler()
66+
knn = KNeighborsRegressor(n_neighbors=5)
67+
68+
pipeline = Pipeline([
69+
('scaler', scaler),
70+
('knn', knn),
71+
], memory="cache")
72+
print(scaler.center_)
73+
""",
74+
"Replace the direct access to the transformer with an access to the `named_steps` attribute of the pipeline."
75+
);
6176
}
62-
}
77+
}

0 commit comments

Comments
 (0)