Skip to content

Commit

Permalink
Added pylint plugin import checker
Browse files Browse the repository at this point in the history
  • Loading branch information
Eyal Ezer committed May 28, 2024
1 parent e9e4e50 commit a8d9b8a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ persistent=yes

# List of plugins (as comma separated values of python modules names) to load,
# usually to register additional checkers.
load-plugins=
load-plugins=superset.extensions.pylint

# Use multiple processes to speed up Pylint.
jobs=2
Expand Down
60 changes: 60 additions & 0 deletions superset/extensions/pylint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import os
from astroid import nodes
from pylint.checkers import BaseChecker
from pylint.lint import PyLinter


class JSONLibraryImportChecker(BaseChecker):

name = "json-library-import-checker"
priority = -1
msgs = {
"C9999": (
"Disallowed json import used, use superset.utils.json instead",
"disallowed-import",
"Used when a disallowed import is used in a specific file.",
),
}
exclude_files = [
"setup.py",
"superset/utils/json.py",
"superset/config.py",
"superset/cli/update.py",
"superset/key_value/types.py",
"superset/translations/utils.py",
"superset/extensions/__init__.py",
]
path_strip_prefix = os.getcwd() + os.sep

def visit_import(self, node: nodes.Import) -> None:
file = (node.root().file).replace(self.path_strip_prefix, "", 1)
if file not in self.exclude_files:
for module_name, _ in node.names:
if module_name in ["json", "simplejson"]:
self.add_message("disallowed-import", node=node)

def visit_importfrom(self, node: nodes.ImportFrom) -> None:
file = (node.root().file).replace(self.path_strip_prefix, "", 1)
if file not in self.exclude_files:
if node.modname in ["json", "simplejson"]:
self.add_message("disallowed-import", node=node)


def register(linter: PyLinter) -> None:
linter.register_checker(JSONLibraryImportChecker(linter))

0 comments on commit a8d9b8a

Please sign in to comment.