Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[F403] Warn on star import #273

Open
wants to merge 2 commits into
base: gh/"ansley"/3/base
Choose a base branch
from
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 *
"""
),
]