Skip to content

Commit 7d3315a

Browse files
author
Peter Hamfelt
committed
Add pandas dataframe and series bool checker
1 parent 718be9a commit 7d3315a

File tree

4 files changed

+150
-0
lines changed

4 files changed

+150
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Licensed under the MIT: https://mit-license.org/
2+
# For details: https://github.com/pylint-dev/pylint-ml/LICENSE
3+
# Copyright (c) https://github.com/pylint-dev/pylint-ml/CONTRIBUTORS.txt
4+
5+
"""Check for usage of the deprecated pandas DataFrame.bool() method."""
6+
7+
from __future__ import annotations
8+
9+
from astroid import nodes
10+
from pylint.checkers import BaseChecker
11+
from pylint.checkers.utils import only_required_for_messages
12+
from pylint.interfaces import HIGH
13+
14+
15+
class PandasDataFrameBoolChecker(BaseChecker):
16+
name = "pandas-dataframe-bool"
17+
msgs = {
18+
"W8104": (
19+
"Use of deprecated pandas DataFrame.bool() method",
20+
"pandas-dataframe-bool",
21+
"Avoid using the deprecated pandas DataFrame.bool() method.",
22+
),
23+
}
24+
25+
@only_required_for_messages("pandas-dataframe-bool")
26+
def visit_call(self, node: nodes.Call) -> None:
27+
if isinstance(node.func, nodes.Attribute):
28+
method_name = getattr(node.func, "attrname", None)
29+
module_name = getattr(node.func.expr, "name", None)
30+
31+
if method_name == "bool" and module_name == "pd":
32+
self.add_message("pandas-dataframe-bool", node=node, confidence=HIGH)
33+
34+
def _check_method_usage(self, node):
35+
method_name = getattr(node.func, "attrname", None)
36+
module_name = getattr(node.func.expr, "name", None)
37+
if method_name == "bool" and module_name == "pd":
38+
self.add_message("pandas-dataframe-bool", node=node, confidence=HIGH)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Licensed under the MIT: https://mit-license.org/
2+
# For details: https://github.com/pylint-dev/pylint-ml/LICENSE
3+
# Copyright (c) https://github.com/pylint-dev/pylint-ml/CONTRIBUTORS.txt
4+
5+
"""Check for usage of the deprecated pandas Series.bool() method."""
6+
7+
from __future__ import annotations
8+
9+
from astroid import nodes
10+
from pylint.checkers import BaseChecker
11+
from pylint.checkers.utils import only_required_for_messages
12+
from pylint.interfaces import HIGH
13+
14+
15+
class PandasSeriesBoolChecker(BaseChecker):
16+
name = "pandas-series-bool"
17+
msgs = {
18+
"W8105": (
19+
"Use of deprecated pandas Series.bool() method",
20+
"pandas-series-bool",
21+
"Avoid using the deprecated pandas Series.bool() method.",
22+
),
23+
}
24+
25+
@only_required_for_messages("pandas-series-bool")
26+
def visit_call(self, node: nodes.Call) -> None:
27+
if isinstance(node.func, nodes.Attribute):
28+
method_name = getattr(node.func, "attrname", None)
29+
module_name = getattr(node.func.expr, "name", None)
30+
31+
if method_name == "bool" and module_name == "pd":
32+
self.add_message("pandas-series-bool", node=node, confidence=HIGH)
33+
34+
def _check_method_usage(self, node):
35+
method_name = getattr(node.func, "attrname", None)
36+
module_name = getattr(node.func.expr, "name", None)
37+
if method_name == "bool" and module_name == "pd":
38+
self.add_message("pandas-series-bool", node=node, confidence=HIGH)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import astroid
2+
import pylint.testutils
3+
from pylint.interfaces import HIGH
4+
5+
from pylint_ml.checkers.pandas.pandas_dataframe_bool import PandasDataFrameBoolChecker
6+
7+
8+
class TestDataFrameBoolChecker(pylint.testutils.CheckerTestCase):
9+
CHECKER_CLASS = PandasDataFrameBoolChecker
10+
11+
def test_dataframe_bool_usage(self):
12+
node = astroid.extract_node(
13+
"""
14+
import pandas as pd
15+
df_customers = pd.DataFrame(data)
16+
df_customers.bool() # [pandas-dataframe-bool]
17+
"""
18+
)
19+
with self.assertAddsMessages(
20+
pylint.testutils.MessageTest(
21+
msg_id="pandas-dataframe-bool",
22+
confidence=HIGH,
23+
node=node,
24+
)
25+
):
26+
self.checker.visit_call(node)
27+
28+
def test_no_bool_usage(self):
29+
node = astroid.extract_node(
30+
"""
31+
import pandas as pd
32+
df_customers = pd.DataFrame(data)
33+
df_customers.sum() # This should pass without warnings
34+
"""
35+
)
36+
with self.assertNoMessages():
37+
self.checker.visit_call(node)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import astroid
2+
import pylint.testutils
3+
from pylint.interfaces import HIGH
4+
5+
from pylint_ml.checkers.pandas.pandas_series_bool import PandasSeriesBoolChecker
6+
7+
8+
class TestSeriesBoolChecker(pylint.testutils.CheckerTestCase):
9+
CHECKER_CLASS = PandasSeriesBoolChecker
10+
11+
def test_series_bool_usage(self):
12+
node = astroid.extract_node(
13+
"""
14+
import pandas as pd
15+
series = pd.Series(data)
16+
series.bool() # [pandas-series-bool]
17+
"""
18+
)
19+
with self.assertAddsMessages(
20+
pylint.testutils.MessageTest(
21+
msg_id="pandas-series-bool",
22+
confidence=HIGH,
23+
node=node,
24+
)
25+
):
26+
self.checker.visit_call(node)
27+
28+
def test_no_bool_usage(self):
29+
node = astroid.extract_node(
30+
"""
31+
import pandas as pd
32+
series = pd.Series(data)
33+
series.sum() # This should pass without warnings
34+
"""
35+
)
36+
with self.assertNoMessages():
37+
self.checker.visit_call(node)

0 commit comments

Comments
 (0)