Skip to content

Commit dc1feb3

Browse files
committed
Add test import numpy
1 parent cc25bc2 commit dc1feb3

File tree

12 files changed

+146
-59
lines changed

12 files changed

+146
-59
lines changed

pylint_ml/__pkginfo__.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

pylint_ml/checkers/numpy/import_numpy.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,36 @@
99
from astroid import nodes
1010
from pylint.checkers import BaseChecker
1111
from pylint.checkers.utils import only_required_for_messages
12+
from pylint.interfaces import HIGH
1213

1314

1415
class NumpyImportChecker(BaseChecker):
15-
name = "numpy_import"
16+
name = "numpy-import"
1617
msgs = {
17-
"W8010": (
18+
"W8001": (
1819
"Numpy imported with incorrect alias",
19-
"numpy-imported-incorrectly",
20+
"numpy-import",
2021
"Numpy should be imported with the alias `np` to maintain consistency with common practices. "
2122
"Importing numpy with any other alias can lead to confusion. "
2223
"Consider using `import numpy as np` for clarity and adherence to the convention.",
23-
)
24+
),
25+
"W8002": (
26+
"Direct import from Numpy discouraged",
27+
"numpy-importfrom",
28+
"Direct imports from Numpy using `from numpy import ...` are discouraged to maintain code clarity and "
29+
"prevent potential conflicts. Numpy should be imported with the alias `np` following common practices. "
30+
"Using any other alias or direct import method can lead to confusion. "
31+
"Consider using `import numpy as np` to adhere to the convention and ensure consistency.",
32+
),
2433
}
2534

26-
@only_required_for_messages("numpy_import")
35+
@only_required_for_messages("numpy-import")
2736
def visit_import(self, node: nodes.Import) -> None:
2837
for name, alias in node.names:
2938
if name == "numpy" and alias != "np":
30-
self.add_message("numpy_import", node=node)
39+
self.add_message("numpy-import", node=node, confidence=HIGH)
40+
41+
@only_required_for_messages("numpy-importfrom")
42+
def visit_importfrom(self, node: nodes.ImportFrom) -> None:
43+
if node.modname == "numpy":
44+
self.add_message("numpy-importfrom", node=node, confidence=HIGH)

pylint_ml/checkers/pandas/import_pandas.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,36 @@
99
from astroid import nodes
1010
from pylint.checkers import BaseChecker
1111
from pylint.checkers.utils import only_required_for_messages
12+
from pylint.interfaces import HIGH
1213

1314

1415
class PandasImportChecker(BaseChecker):
15-
name = "pandas_import"
16+
name = "pandas-import"
1617
msgs = {
17-
"W8020": (
18+
"W8001": (
1819
"Pandas imported with incorrect alias",
19-
"pandas-imported-incorrectly",
20+
"pandas-import",
2021
"Pandas should be imported with the alias `pd` to maintain consistency with common practices. "
2122
"Importing pandas with any other alias can lead to confusion. "
2223
"Consider using `import pandas as pd` for clarity and adherence to the convention.",
23-
)
24+
),
25+
"W8002": (
26+
"Direct import from Pandas discouraged",
27+
"pandas-importfrom",
28+
"Direct imports from Pandas using `from pandas import ...` are discouraged to maintain code clarity and "
29+
"prevent potential conflicts. Pandas should be imported with the alias `pd` following common practices. "
30+
"Using any other alias or direct import method can lead to confusion. "
31+
"Consider using `import pandas as pd` to adhere to the convention and ensure consistency.",
32+
),
2433
}
2534

26-
@only_required_for_messages("pandas_import")
35+
@only_required_for_messages("pandas-import")
2736
def visit_import(self, node: nodes.Import) -> None:
2837
for name, alias in node.names:
2938
if name == "pandas" and alias != "pd":
30-
self.add_message("pandas_import", node=node)
39+
self.add_message("pandas-import", node=node, confidence=HIGH)
40+
41+
@only_required_for_messages("pandas-importfrom")
42+
def visit_importfrom(self, node: nodes.ImportFrom) -> None:
43+
if node.modname == "pandas":
44+
self.add_message("pandas-importfrom", node=node, confidence=HIGH)
File renamed without changes.

pylint_ml/checkers/tensorflow/__init__.py

Whitespace-only changes.

tests/checkers/__init__.py

Whitespace-only changes.

tests/checkers/test_numpy/__init__.py

Whitespace-only changes.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import astroid
2+
import pylint.testutils
3+
from pylint.interfaces import HIGH
4+
5+
from pylint_ml.checkers.numpy.import_numpy import NumpyImportChecker
6+
7+
8+
class TestNumpyImport(pylint.testutils.CheckerTestCase):
9+
CHECKER_CLASS = NumpyImportChecker
10+
11+
def test_correct_numpy_import(self):
12+
import_node = astroid.extract_node(
13+
"""
14+
import numpy as np
15+
"""
16+
)
17+
18+
with self.assertNoMessages():
19+
self.checker.visit_import(import_node)
20+
21+
def test_incorrect_numpy_import(self):
22+
import_node = astroid.extract_node(
23+
"""
24+
import numpy as npy
25+
"""
26+
)
27+
28+
with self.assertAddsMessages(
29+
pylint.testutils.MessageTest(
30+
msg_id="numpy-import",
31+
confidence=HIGH,
32+
node=import_node,
33+
),
34+
ignore_position=True,
35+
):
36+
self.checker.visit_import(import_node)
37+
38+
def test_incorrect_numpy_import_from(self):
39+
importfrom_node = astroid.extract_node(
40+
"""
41+
from numpy import min
42+
"""
43+
)
44+
45+
with self.assertAddsMessages(
46+
pylint.testutils.MessageTest(
47+
msg_id="numpy-importfrom",
48+
confidence=HIGH,
49+
node=importfrom_node,
50+
),
51+
ignore_position=True,
52+
):
53+
self.checker.visit_importfrom(importfrom_node)

tests/checkers/test_pandas/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)