File tree Expand file tree Collapse file tree 4 files changed +75
-1
lines changed
tests/checkers/test_scipy Expand file tree Collapse file tree 4 files changed +75
-1
lines changed Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change 4
4
5
5
from pylint_ml .checkers .numpy .import_numpy import NumpyImportChecker
6
6
from pylint_ml .checkers .pandas .import_pandas import PandasImportChecker
7
+ from pylint_ml .checkers .scipy .import_scipy import ScipyImportChecker
7
8
from pylint_ml .checkers .tensorflow .import_tensorflow import TensorflowImportChecker
8
9
from pylint_ml .checkers .torch .import_torch import TorchImportChecker
9
10
@@ -22,5 +23,10 @@ def register(linter: PyLinter) -> None:
22
23
# Torch
23
24
linter .register_checker (TorchImportChecker (linter ))
24
25
25
- # Sklearn
26
26
# Scipy
27
+ linter .register_checker (ScipyImportChecker (linter ))
28
+
29
+ # Sklearn
30
+
31
+ # Theano
32
+ # Matplotlib
Original file line number Diff line number Diff line change
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 )
You can’t perform that action at this time.
0 commit comments