Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/fixit/rules/pyflakes/no_import_star.py
Original file line number Diff line number Diff line change
@@ -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 *
"""
),
]