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, F405, F821] Warn on undefined names #274

Open
wants to merge 2 commits into
base: gh/"ansley"/4/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
143 changes: 143 additions & 0 deletions src/fixit/rules/pyflakes/no_undefined_name.py
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
Comment on lines +100 to +104
Copy link
Member

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?

"""
),
]

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
"""
),
]