Skip to content

Commit b29355f

Browse files
authored
SONARPY-1514: Rule S6742 should raise issues on chains of 7 or more operations. (#1600)
1 parent 43a2209 commit b29355f

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
public class PandasChainInstructionCheck extends PythonSubscriptionCheck {
4444

4545
private static final String MESSAGE = "Refactor this long chain of instructions with pandas.pipe";
46-
private static final int MAX_CHAIN_LENGTH = 5;
46+
private static final int MAX_CHAIN_LENGTH = 7;
4747

4848
private static final String DATAFRAME_FQN = "pandas.core.frame.DataFrame";
4949

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,38 @@
44

55
def non_compliant(df: pd.DataFrame, df2: DataFrame):
66

7-
df2.set_index("name").T.filter(like='joe', axis=0)[1].mean().head() # Noncompliant
8-
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9-
DataFrame().set_index("name").filter(like='joe', axis=0).groupby("team")["salary"].mean().head() # Noncompliant {{Refactor this long chain of instructions with pandas.pipe}}
10-
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11-
df.set_index("name").filter(like='joe', axis=0).groupby("team")["salary"].mean().head() # FN see SONARPY-1503
7+
df2.set_index("name").T.filter(like='joe', axis=0)[1].add(10).mean().round().to_parquet() # Noncompliant
8+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9+
DataFrame().set_index("name").filter(like='joe', axis=0).groupby("team")["salary"].add(10).mean().round().to_parquet() # Noncompliant {{Refactor this long chain of instructions with pandas.pipe}}
10+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11+
df.set_index("name").filter(like='joe', axis=0).groupby("team")["salary"].add(10).mean().round().to_parquet() # FN see SONARPY-1503
1212

13-
df2.set_index("name").filter(like='joe', axis=0).groupby("team")["salary"]["test"].mean().head() # Noncompliant {{Refactor this long chain of instructions with pandas.pipe}}
14-
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15-
pd.read_csv("some_csv.csv").filter(like='joe', axis=0).groupby("team")["salary"]["test"].mean().head() # Noncompliant
16-
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13+
df2.set_index("name").filter(like='joe', axis=0).groupby("team")["salary"]["test"].add(10).mean().round().to_parquet() # Noncompliant {{Refactor this long chain of instructions with pandas.pipe}}
14+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
pd.read_csv("some_csv.csv").filter(like='joe', axis=0).groupby("team")["salary"]["test"].add(10).mean().round().to_parquet() # Noncompliant
16+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1717

1818

1919
# Here we do not raise an issue only because we do not support subscription with Name
2020
# If support is added for such case we would encounter FPs when the subscription with Name is at the beginning of the chain
21-
pd.read_csv("some_csv.csv").filter(like='joe', axis=0).groupby("team")["salary"]["test"].axes[1].unique() # FN
21+
pd.read_csv("some_csv.csv").filter(like='joe', axis=0).add(10).groupby("team")["salary"]["test"].axes[1].unique().to_json() # FN
2222

2323
# Here we should not raise an issue as the chain is done mainly on an Index object which does not have a pipe method
24-
pd.read_csv("some_csv.csv").axes[1].join(pd.Index([4, 5, 6])).repeat([1,2]).drop_duplicates().insert(1, 42)
24+
pd.read_csv("some_csv.csv").axes[1].join(pd.Index([4, 5, 6])).T.repeat([1,2]).drop_duplicates().insert(1, 42).sort_values()
2525

2626
def compliant(df: pd.DataFrame, my_function, something, df2: DataFrame):
2727

28-
df2.set_index("name").T.filter(like='joe', axis=0)[1].mean()
28+
df2.set_index("name").T.filter(like='joe', axis=0)[1].add(10).mean().to_html()
2929

30-
(df2.set_index("name").T.filter(like='joe', axis=0))[1].mean()
30+
(df2.set_index("name").T.filter(like='joe', axis=0))[1].add(10).mean().round().to_html()
3131

3232
df2.set_index("name").filter(like='joe', axis=0).mean().head()
3333

3434
pd.read_csv("some_csv.csv").filter(like='joe', axis=0).groupby("team")["salary"]["test"].head()
3535

3636
df.set_index("name").filter(like='joe', axis=0).groupby("team")["salary"].mean()
3737

38-
DataFrame().set_index("name").pipe(my_function).filter(like='joe', axis=0).groupby("team")["salary"].mean()
38+
DataFrame().set_index("name").pipe(my_function).filter(like='joe', axis=0).groupby("team")["salary"].add(10).round().mean().to_json()
3939

40-
something.set_index("name").filter(like='joe', axis=0).groupby("team")["salary"].mean().head()
40+
something.set_index("name").filter(like='joe', axis=0).groupby("team")["salary"].add(10).round().mean().to_parquet()
4141

0 commit comments

Comments
 (0)