From 45f08a994fa9be0f3cc3a22cd45b47b05768550c Mon Sep 17 00:00:00 2001 From: Ansley Ussery Date: Wed, 8 Mar 2023 11:14:20 -0500 Subject: [PATCH] [F403] Warn on star import [ghstack-poisoned] --- src/fixit/rules/pyflakes/no_import_star.py | 43 ++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/fixit/rules/pyflakes/no_import_star.py diff --git a/src/fixit/rules/pyflakes/no_import_star.py b/src/fixit/rules/pyflakes/no_import_star.py new file mode 100644 index 00000000..1bc9e7d9 --- /dev/null +++ b/src/fixit/rules/pyflakes/no_import_star.py @@ -0,0 +1,43 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +import libcst as cst + +from fixit import CstLintRule, InvalidTestCase as Invalid, ValidTestCase as Valid + + +class NoImportStarRule(CstLintRule): + """ + Discourage `from ... import *` statements. + + Autofix: N/A + """ + + MESSAGE = ("`from ... import *` is discouraged because it makes it " + "difficult to see where the imported object is defined. Instead," + " explicitly import the desired module, class, or function.") + + def __init__(self) -> None: + super().__init__() + + def visit_ImportFrom(self, node: cst.ImportFrom) -> None: + if isinstance(node.names, cst.ImportStar): + self.report(node) + + VALID = [ + Valid( + """ + from a import b, c, d + """ + ), + ] + + INVALID = [ + Invalid( + """ + from a import * + """ + ), + ]