diff --git a/.pylintrc b/.pylintrc index 5f51e9fcff3ef..e11376fc1053b 100644 --- a/.pylintrc +++ b/.pylintrc @@ -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 diff --git a/superset/extensions/pylint.py b/superset/extensions/pylint.py new file mode 100644 index 0000000000000..faf24d03b54b8 --- /dev/null +++ b/superset/extensions/pylint.py @@ -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)) \ No newline at end of file