-
Notifications
You must be signed in to change notification settings - Fork 65
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, F405, F821] Warn on undefined names #274
Open
ansley
wants to merge
2
commits into
gh/"ansley"/4/base
Choose a base branch
from
gh/"ansley"/4/head
base: gh/"ansley"/4/base
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
The head ref may contain hidden characters: "gh/\"ansley\"/4/head"
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
# 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 NoUndefinedNameRule(CstLintRule): | ||
""" | ||
The variable name is either undefined, or it's imported implicitly | ||
through a `from ... import *` statement. | ||
|
||
Autofix: N/A | ||
""" | ||
|
||
METADATA_DEPENDENCIES = (cst.metadata.ScopeProvider,) | ||
|
||
def __init__(self) -> None: | ||
super().__init__() | ||
self.has_import_star = False | ||
|
||
def message(self, name: str) -> str: | ||
if not self.has_import_star: | ||
return f"{name} must be defined before use" | ||
else: | ||
return (f"{name} is either undefined or it has been defined " | ||
"implicitly through a `from ... import *` statement. " | ||
f"Define {name}, changing any import-star statements " | ||
"to explicit imports as necessary.") | ||
|
||
def visit_ImportFrom(self, node: cst.ImportFrom) -> None: | ||
if isinstance(node.names, cst.ImportStar): | ||
self.has_import_star = True | ||
|
||
def visit_Name(self, node: cst.Name) -> None: | ||
try: | ||
metadata = self.get_metadata(cst.metadata.ScopeProvider, node) | ||
assert metadata is not None | ||
accesses = metadata.accesses | ||
except KeyError: | ||
# Not all Names have ExpressionContext | ||
return | ||
for access in accesses: | ||
if access.node is node and len(access.referents) == 0: | ||
assert isinstance(access.node, cst.Name) | ||
name = access.node.value | ||
self.report(node, self.message(name)) | ||
|
||
VALID = [ | ||
Valid( | ||
""" | ||
y = 1 + 2 | ||
""" | ||
), | ||
Valid( | ||
""" | ||
def b(l): | ||
return sum(l) | ||
|
||
x = b([1, 3, 5, 6, 8]) | ||
""" | ||
), | ||
Valid( | ||
""" | ||
from a import * | ||
import c | ||
|
||
x = c.d([1, 3, 5, 6, 8]) | ||
""" | ||
), | ||
Valid( | ||
""" | ||
from a import b | ||
|
||
def fn(): | ||
return b([1, 3, 5, 6, 8]) | ||
""" | ||
), | ||
Valid( | ||
""" | ||
from a import b | ||
|
||
x = [1, 3, 5, 6, 8] | ||
y = [2, 4, 6, 7, 9] | ||
|
||
z = [b(l) for l in [x, y]] | ||
""" | ||
), | ||
Valid( | ||
""" | ||
for i in [1, 3, 5, 6, 8]: | ||
print(i) | ||
""" | ||
), | ||
Valid( | ||
""" | ||
def fn(): | ||
global b | ||
b = 3 | ||
|
||
x = b + 2 | ||
""" | ||
), | ||
] | ||
|
||
INVALID = [ | ||
Invalid( | ||
""" | ||
def b(l: List[int]): | ||
return sum(l) | ||
|
||
x = b([1, 3, 5, 6, 8]) | ||
""" | ||
), | ||
Invalid( | ||
""" | ||
x = b([1, 3, 5, 6, 8]) | ||
|
||
def b(l): | ||
return sum(l) | ||
""" | ||
), | ||
Invalid( | ||
""" | ||
from a import * | ||
|
||
x = b([1, 3, 5, 6, 8]) | ||
""" | ||
), | ||
Invalid( | ||
""" | ||
from a import b | ||
|
||
def fn(): | ||
x = b([1, 3, 5, 6, 8]) | ||
|
||
y = x + 2 | ||
""" | ||
), | ||
] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where is
b
defined here?