Skip to content

26 add pandas inplace parameter set checker #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions pylint_ml/checkers/pandas/pandas_inplace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Licensed under the MIT: https://mit-license.org/
# For details: https://github.com/pylint-dev/pylint-ml/LICENSE
# Copyright (c) https://github.com/pylint-dev/pylint-ml/CONTRIBUTORS.txt

"""Check for improper usage of the inplace parameter in pandas operations."""

from __future__ import annotations

from astroid import nodes
from pylint.checkers import BaseChecker
from pylint.checkers.utils import only_required_for_messages
from pylint.interfaces import HIGH


class PandasInplaceChecker(BaseChecker):
name = "pandas-inplace"
msgs = {
"W8109": (
"Avoid using 'inplace=True' in pandas operations.",
"pandas-inplace",
"Using 'inplace=True' can lead to unclear and potentially problematic code. Prefer using assignment "
"instead.",
),
}

_inplace_methods = {
"drop",
"fillna",
"replace",
"rename",
"set_index",
"reset_index",
"sort_values",
"sort_index",
"drop_duplicates",
"update",
"clip",
}

@only_required_for_messages("pandas-inplace")
def visit_call(self, node: nodes.Call) -> None:
# Check if the call is to a method that supports 'inplace'
if isinstance(node.func, nodes.Attribute):
method_name = node.func.attrname
if method_name in self._inplace_methods:
for keyword in node.keywords:
if keyword.arg == "inplace" and getattr(keyword.value, "value", False) is True:
self.add_message("pandas-inplace", node=node, confidence=HIGH)
break
104 changes: 104 additions & 0 deletions tests/checkers/test_pandas/test_pandas_inplace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import astroid
import pylint.testutils
from pylint.interfaces import HIGH

from pylint_ml.checkers.pandas.pandas_inplace import PandasInplaceChecker


class TestPandasInplaceChecker(pylint.testutils.CheckerTestCase):
CHECKER_CLASS = PandasInplaceChecker

def test_inplace_used_in_drop(self):
node = astroid.extract_node(
"""
import pandas as pd
df = pd.DataFrame({
"A": [1, 2, 3],
"B": [4, 5, 6]
})
df.drop(columns=["A"], inplace=True) # [pandas-inplace]
"""
)
with self.assertAddsMessages(
pylint.testutils.MessageTest(
msg_id="pandas-inplace",
confidence=HIGH,
node=node,
),
ignore_position=True,
):
self.checker.visit_call(node)

def test_inplace_used_in_fillna(self):
node = astroid.extract_node(
"""
import pandas as pd
df = pd.DataFrame({
"A": [1, None, 3],
"B": [4, 5, None]
})
df.fillna(0, inplace=True) # [pandas-inplace]
"""
)
with self.assertAddsMessages(
pylint.testutils.MessageTest(
msg_id="pandas-inplace",
confidence=HIGH,
node=node,
),
ignore_position=True,
):
self.checker.visit_call(node)

def test_inplace_used_in_sort_values(self):
node = astroid.extract_node(
"""
import pandas as pd
df = pd.DataFrame({
"A": [3, 2, 1],
"B": [4, 5, 6]
})
df.sort_values(by="A", inplace=True) # [pandas-inplace]
"""
)
with self.assertAddsMessages(
pylint.testutils.MessageTest(
msg_id="pandas-inplace",
confidence=HIGH,
node=node,
),
ignore_position=True,
):
self.checker.visit_call(node)

def test_no_inplace(self):
node = astroid.extract_node(
"""
import pandas as pd
df = pd.DataFrame({
"A": [1, 2, 3],
"B": [4, 5, 6]
})
df = df.drop(columns=["A"]) # This should not trigger any warnings
"""
)

inplace_call = node.value

with self.assertNoMessages():
self.checker.visit_call(inplace_call)

def test_inplace_used_in_unsupported_method(self):
node = astroid.extract_node(
"""
import pandas as pd
df = pd.DataFrame({
"A": [1, 2, 3],
"B": [4, 5, 6]
})
df.append({"A": 4, "B": 7}, inplace=True) # This should not trigger any warnings
"""
)

with self.assertNoMessages():
self.checker.visit_call(node)