Skip to content

Commit d30802b

Browse files
authored
SONARPY-2184: S125: Fix FP when analyzing databricks notebook (#2060)
1 parent 4ca8e09 commit d30802b

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class CommentedCodeCheck extends PythonSubscriptionCheck {
4949
private static final Pattern IS_EMPTY_PATTERN = Pattern.compile("\\s*");
5050

5151
private static final String DEFAULT_EXCEPTION_PATTERN = "(fmt|py\\w+):.*";
52+
private static final Pattern DATABRICKS_MAGIC_COMMAND_PATTERN = Pattern.compile("^\\h*(MAGIC|COMMAND).*");
5253
private static final PythonParser parser = PythonParser.create();
5354

5455
private Pattern exceptionPattern;
@@ -125,7 +126,8 @@ private String getTextForParsing(List<Trivia> triviaGroup) {
125126
}
126127

127128
private boolean isException(String text) {
128-
return exceptionPattern.matcher(text).matches();
129+
boolean isDatabricksMagicCommand = DATABRICKS_MAGIC_COMMAND_PATTERN.matcher(text).matches();
130+
return exceptionPattern.matcher(text).matches() || isDatabricksMagicCommand;
129131
}
130132

131133
private static boolean isOneWord(String text) {

python-checks/src/test/resources/checks/commentedCode.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,12 @@ def testFunction():
138138

139139
# abcfmt: off
140140
# abcpylint: disable=line-too-long
141+
142+
143+
# Databricks notebooks
144+
# COMMAND ----------
145+
146+
# MAGIC %md
147+
# MAGIC ## Alter tables
148+
149+
# COMMAND ----------

0 commit comments

Comments
 (0)