Skip to content

Commit 0f82c82

Browse files
authored
Merge pull request #9 from pylint-dev/8-add-scipy-import-checker
8 add scipy import checker
2 parents b05c639 + da3833d commit 0f82c82

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 import of scipy library."""
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 ScipyImportChecker(BaseChecker):
16+
name = "scipy-import"
17+
msgs = {
18+
"W8401": (
19+
"Direct or aliased Scipy import detected",
20+
"scipy-import",
21+
"Using `import scipy` or `import scipy as ...` is not recommended. For better clarity and consistency, "
22+
"it is advisable to import specific submodules directly, using the `from scipy import ...` syntax. "
23+
"This approach prevents confusion and aligns with common practices by explicitly stating which "
24+
"components of Scipy are being used.",
25+
),
26+
}
27+
28+
@only_required_for_messages("scipy-import")
29+
def visit_import(self, node: nodes.Import) -> None:
30+
for name, _ in node.names:
31+
if name == "scipy":
32+
self.add_message("scipy-import", node=node, confidence=HIGH)

pylint_ml/plugin.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from pylint_ml.checkers.numpy.import_numpy import NumpyImportChecker
66
from pylint_ml.checkers.pandas.import_pandas import PandasImportChecker
7+
from pylint_ml.checkers.scipy.import_scipy import ScipyImportChecker
78
from pylint_ml.checkers.tensorflow.import_tensorflow import TensorflowImportChecker
89
from pylint_ml.checkers.torch.import_torch import TorchImportChecker
910

@@ -22,5 +23,10 @@ def register(linter: PyLinter) -> None:
2223
# Torch
2324
linter.register_checker(TorchImportChecker(linter))
2425

25-
# Sklearn
2626
# Scipy
27+
linter.register_checker(ScipyImportChecker(linter))
28+
29+
# Sklearn
30+
31+
# Theano
32+
# Matplotlib

tests/checkers/test_scipy/__init__.py

Whitespace-only changes.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import astroid
2+
import pylint.testutils
3+
from pylint.interfaces import HIGH
4+
5+
from pylint_ml.checkers.scipy.import_scipy import ScipyImportChecker
6+
7+
8+
class TestScipyImport(pylint.testutils.CheckerTestCase):
9+
CHECKER_CLASS = ScipyImportChecker
10+
11+
def test_correct_scipy_import(self):
12+
scipy_import_node = astroid.extract_node(
13+
"""
14+
from scipy.misc import imread, imsave, imresize
15+
"""
16+
)
17+
18+
with self.assertNoMessages():
19+
self.checker.visit_import(scipy_import_node)
20+
21+
def test_incorrect_scipy_import(self):
22+
scipy_import_node = astroid.extract_node(
23+
"""
24+
import scipy as spy
25+
"""
26+
)
27+
28+
with self.assertAddsMessages(
29+
pylint.testutils.MessageTest(
30+
msg_id="scipy-import",
31+
confidence=HIGH,
32+
node=scipy_import_node,
33+
),
34+
ignore_position=True,
35+
):
36+
self.checker.visit_import(scipy_import_node)

0 commit comments

Comments
 (0)