Skip to content

Commit 2ac07fe

Browse files
authored
Merge pull request #5 from pylint-dev/add-tensorflow-import-checker
Add tensorflow import checker
2 parents c6c7a4d + f3788f9 commit 2ac07fe

File tree

6 files changed

+100
-17
lines changed

6 files changed

+100
-17
lines changed

.gitignore

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,4 @@ cython_debug/
157157
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158158
# and can be added to the global gitignore or merged into this file. For a more nuclear
159159
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160-
.idea/
161-
162-
# Mr Developer
163-
.mr.developer.cfg
164-
.project
165-
.pydevproject
166-
.idea
167-
env.txt
168-
poetry.lock
160+
# .idea/

CONTRIBUTORS.txt

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

pylint_ml/checkers/pandas/import_pandas.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
class PandasImportChecker(BaseChecker):
1616
name = "pandas-import"
1717
msgs = {
18-
"W8001": (
18+
"W8101": (
1919
"Pandas imported with incorrect alias",
2020
"pandas-import",
2121
"Pandas should be imported with the alias `pd` to maintain consistency with common practices. "
2222
"Importing pandas with any other alias can lead to confusion. "
2323
"Consider using `import pandas as pd` for clarity and adherence to the convention.",
2424
),
25-
"W8002": (
25+
"W8102": (
2626
"Direct import from Pandas discouraged",
2727
"pandas-importfrom",
2828
"Direct imports from Pandas using `from pandas import ...` are discouraged to maintain code clarity and "
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 tensorflow 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 TensorflowImportChecker(BaseChecker):
16+
name = "tensorflow-import"
17+
msgs = {
18+
"W8401": (
19+
"Tensorflow imported with incorrect alias",
20+
"tensorflow-import",
21+
"Tensorflow should be imported with the alias `tf` to maintain consistency with common practices. "
22+
"Importing Tensorflow with any other alias can lead to confusion. "
23+
"Consider using `import tensorflow as tf` for clarity and adherence to the convention.",
24+
),
25+
"W8402": (
26+
"Direct import from Tensorflow discouraged",
27+
"tensorflow-importfrom",
28+
"Direct imports from Tensorflow using `from tensorflow import ...` are discouraged to maintain code "
29+
"clarity and prevent potential conflicts. Tensorflow should be imported with the alias `tf` following "
30+
"common practices. Using any other alias or direct import method can lead to confusion. "
31+
"Consider using `import tensorflow as tf` to adhere to the convention and ensure consistency.",
32+
),
33+
}
34+
35+
@only_required_for_messages("tensorflow-import")
36+
def visit_import(self, node: nodes.Import) -> None:
37+
for name, alias in node.names:
38+
if name == "tensorflow" and alias != "tf":
39+
self.add_message("tensorflow-import", node=node, confidence=HIGH)
40+
41+
@only_required_for_messages("tensorflow-importfrom")
42+
def visit_importfrom(self, node: nodes.ImportFrom) -> None:
43+
if node.modname == "tensorflow":
44+
self.add_message("tensorflow-importfrom", node=node, confidence=HIGH)

tests/checkers/test_tensorflow/__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.tensorflow.import_tensorflow import TensorflowImportChecker
6+
7+
8+
class TestTensorflowImport(pylint.testutils.CheckerTestCase):
9+
CHECKER_CLASS = TensorflowImportChecker
10+
11+
def test_correct_tensorflow_import(self):
12+
tensorflow_import_node = astroid.extract_node(
13+
"""
14+
import tensorflow as tf
15+
"""
16+
)
17+
18+
with self.assertNoMessages():
19+
self.checker.visit_import(tensorflow_import_node)
20+
21+
def test_incorrect_tensorflow_import(self):
22+
tensorflow_import_node = astroid.extract_node(
23+
"""
24+
import tensorflow as tflow
25+
"""
26+
)
27+
28+
with self.assertAddsMessages(
29+
pylint.testutils.MessageTest(
30+
msg_id="tensorflow-import",
31+
confidence=HIGH,
32+
node=tensorflow_import_node,
33+
),
34+
ignore_position=True,
35+
):
36+
self.checker.visit_import(tensorflow_import_node)
37+
38+
def test_incorrect_tensorflow_import_from(self):
39+
tensorflow_importfrom_node = astroid.extract_node(
40+
"""
41+
from tensorflow import math
42+
"""
43+
)
44+
45+
with self.assertAddsMessages(
46+
pylint.testutils.MessageTest(
47+
msg_id="tensorflow-importfrom",
48+
confidence=HIGH,
49+
node=tensorflow_importfrom_node,
50+
),
51+
ignore_position=True,
52+
):
53+
self.checker.visit_importfrom(tensorflow_importfrom_node)

0 commit comments

Comments
 (0)