Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
name = getNodeName(node)
if not name:
return
if on_conditional_branch():
# We cannot predict if this conditional branch is going to
# be executed.
return
if isinstance(self.scope, FunctionScope) and name in self.scope.globals:
self.scope.globals.remove(name)
else:
try:
del self.scope[name]
except KeyError:
self.report(messages.UndefinedName, node, name)
def star_import_used_line_numbers(messages):
"""Yield line number of star import usage."""
for message in messages:
if isinstance(message, pyflakes.messages.ImportStarUsed):
yield message.lineno
saved_stack = self.scopeStack
self.scopeStack = [self.scopeStack[0]]
node_offset = self.offset or (0, 0)
self.pushScope(DoctestScope)
if '_' not in self.scopeStack[0]:
self.addBinding(None, Builtin('_'))
for example in examples:
try:
tree = ast.parse(example.source, "")
except SyntaxError:
e = sys.exc_info()[1]
if PYPY:
e.offset += 1
position = (node_lineno + example.lineno + e.lineno,
example.indent + 4 + (e.offset or 0))
self.report(messages.DoctestSyntaxError, node, position)
else:
self.offset = (node_offset[0] + node_lineno + example.lineno,
node_offset[1] + example.indent + 4)
self.handleChildren(tree)
self.offset = node_offset
self.popScope()
self.scopeStack = saved_stack
def checkReturnWithArgumentInsideGenerator():
"""
Check to see if there is any return statement with
arguments but the function is a generator.
"""
if self.scope.isGenerator and self.scope.returnValue:
self.report(messages.ReturnWithArgsInsideGenerator,
self.scope.returnValue)
self.deferAssignment(checkReturnWithArgumentInsideGenerator)
def checkDeadScopes(self):
"""
Look at scopes which have been fully examined and report names in them
which were imported but unused.
"""
for scope in self.deadScopes:
if isinstance(scope.get('__all__'), ExportBinding):
all_names = set(scope['__all__'].names)
if not scope.importStarred and \
os.path.basename(self.filename) != '__init__.py':
# Look for possible mistakes in the export list
undefined = all_names.difference(scope)
for name in undefined:
self.report(messages.UndefinedExport,
scope['__all__'].source, name)
else:
all_names = []
# Look for imported names that aren't used.
for value in scope.values():
if isinstance(value, Importation):
used = value.used or value.name in all_names
if not used:
messg = messages.UnusedImport
self.report(messg, value.source, value.name)
for node in value.redefined:
if isinstance(self.getParent(node), ast.For):
messg = messages.ImportShadowedByLoopVar
elif used:
continue
try:
os.makedirs(output_dir)
except OSError:
pass
for file_path in file_dict[key][dir_name]:
basename = os.path.basename(file_path)
input_file_path = os.path.join(input_dir, basename)
output_file_path = os.path.join(output_dir, basename)
with open(input_file_path, 'r') as read:
file_content = read.read()
reset_lists()
pyfl.check(file_content, input_file_path, GLOBAL_REPORTER)
bad_idx = []
try:
unused_imports = GLOBAL_CHECKER.my_error_dict[str(pym.UnusedImport)]
except KeyError:
unused_imports = []
for lineno, _, _ in unused_imports:
bad_idx.append(lineno-1)
lines = file_content.splitlines(1)
for idx, line in enumerate(lines):
if FUNCDEF_RE.match(line):
curr_idx = idx
while EMPTY_RE.match(lines[curr_idx-1]):
bad_idx.append(curr_idx-1)
curr_idx -= 1
for idx in reversed(sorted(bad_idx)):
del lines[idx]
for idx, line in reversed(list(enumerate(lines[:]))):
def addArgs(arglist):
for arg in arglist:
if isinstance(arg, ast.Tuple):
addArgs(arg.elts)
else:
if arg.id in args:
self.report(messages.DuplicateArgument,
node.lineno, arg.id)
args.append(arg.id)
addArgs(node.args.args)
try:
(lineno, offset, line) = value[1][1:]
except IndexError:
print >> sys.stderr, 'could not compile %r' % (fname,)
return 1
if line.endswith("\n"):
line = line[:-1]
print >> sys.stderr, '%s:%d: could not compile' % (fname, lineno)
print >> sys.stderr, line
print >> sys.stderr, " " * (offset-2), "^"
else:
imports = set()
with ImportChecker(tree, fname) as checker:
for msg in checker.messages:
if isinstance(msg, pyflakes.messages.UndefinedName):
name = msg.message_args[0]
imp = checker.find_import(name)
if imp is not None and imp not in imports:
imports.add(imp)
if imports:
output("\n".join(imports) + "\n", fname)
else:
output('', fname)