Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if offset is not None:
sys.stderr.write(" " * offset + "^\n")
return 1
else:
# Okay, it's syntactically valid. Now check it.
w = checker.Checker(tree, filename)
lines = codeString.split('\n')
messages = [message for message in w.messages
if lines[message.lineno - 1].find('pyflakes:ignore') < 0]
messages.sort(lambda a, b: cmp(a.lineno, b.lineno))
false_positives = 0
for warning in messages:
if not (re.match('.*__init__.py', str(warning)) and
isinstance(warning, (UnusedImport, ImportStarUsed))):
print(warning)
else:
false_positives += 1
return len(messages) - false_positives
def get_pyflakes_warnings(code, defined_names=frozenset(),
skip=(UnusedImport, ImportStarUsed, ImportStarUsage)):
"""
Get pyflakes warnings for code
Return a list of (row, col, msg, m) tuples, where
row is the line number (starting at 0),
col is the column number (starting at 0),
msg is the string message for the warning, and
m is the pyflakes Message class for the message
defined_names should be a frozenset of names which should be considered
already defined in the global namespace for the code.
skip should be a tuple of pyflakes message classes to skip.
"""
from .mypython import validate_text
def IMPORTFROM(self, node):
if node.module == '__future__':
if not self.futuresAllowed:
self.report(messages.LateFutureImport, node,
[n.name for n in node.names])
else:
self.futuresAllowed = False
for alias in node.names:
if alias.name == '*':
self.scope.importStarred = True
self.report(messages.ImportStarUsed, node, node.module)
continue
name = alias.asname or alias.name
importation = Importation(name, node)
if node.module == '__future__':
importation.used = (self.scope, node)
self.addBinding(node, importation)
elif isinstance(error, PythonError):
if len(errors) == 1 and False:
outlines = [view.full_line(view.text_point(error.lineno, 0)) for lineno in lines]
view.add_regions('pyflakes-syntax', outlines, 'keyword', drawType)#sublime.DRAW_EMPTY_AS_OVERWRITE | sublime.DRAW_OUTLINED)
return
elif isinstance(error, messages.UnusedImport):
underlineImport(error.lineno, error.name)
elif isinstance(error, messages.RedefinedWhileUnused):
underlineWord(error.lineno, error.name)
elif isinstance(error, messages.ImportShadowedByLoopVar):
underlineForVar(error.lineno, error.name)
elif isinstance(error, messages.ImportStarUsed):
underlineImport(error.lineno, '\*')
elif isinstance(error, messages.UndefinedName):
underlineWord(error.lineno, error.name)
elif isinstance(error, messages.UndefinedExport):
underlineWord(error.lineno, error.name)
elif isinstance(error, messages.UndefinedLocal):
underlineWord(error.lineno, error.name)
elif isinstance(error, messages.DuplicateArgument):
underlineDuplicateArgument(error.lineno, error.name)
elif isinstance(error, messages.RedefinedFunction):
underlineWord(error.lineno, error.name)
def IMPORTFROM(self, node):
if node.module == '__future__':
if not self.futuresAllowed:
self.report(messages.LateFutureImport,
node, [n.name for n in node.names])
else:
self.futuresAllowed = False
for alias in node.names:
if alias.name == '*':
self.scope.importStarred = True
self.report(messages.ImportStarUsed, node, node.module)
continue
name = alias.asname or alias.name
importation = Importation(name, node)
if node.module == '__future__':
importation.used = (self.scope, node)
self.addBinding(node, importation)
for alias in node.names:
name = alias.asname or alias.name
if node.module == '__future__':
importation = FutureImportation(name, node, self.scope)
if alias.name not in __future__.all_feature_names:
self.report(messages.FutureFeatureNotDefined,
node, alias.name)
elif alias.name == '*':
# Only Python 2, local import * is a SyntaxWarning
if not PY2 and not isinstance(self.scope, ModuleScope):
self.report(messages.ImportStarNotPermitted,
node, module)
continue
self.scope.importStarred = True
self.report(messages.ImportStarUsed, node, module)
importation = StarImportation(module, node)
else:
importation = ImportationFrom(name, node,
module, alias.name)
self.addBinding(node, importation)
for alias in node.names:
name = alias.asname or alias.name
if node.module == '__future__':
importation = FutureImportation(name, node, self.scope)
if alias.name not in __future__.all_feature_names:
self.report(messages.FutureFeatureNotDefined,
node, alias.name)
elif alias.name == '*':
# Only Python 2, local import * is a SyntaxWarning
if not PY2 and not isinstance(self.scope, ModuleScope):
self.report(messages.ImportStarNotPermitted,
node, module)
continue
self.scope.importStarred = True
self.report(messages.ImportStarUsed, node, module)
importation = StarImportation(module, node)
else:
importation = ImportationFrom(name, node,
module, alias.name)
self.addBinding(node, importation)
def star_imports(checker):
stars = []
for message in checker.messages:
if isinstance(message, ImportStarUsed):
stars.append(message.message_args[0])
return stars
def IMPORTFROM(self, node):
if node.module == '__future__':
if not self.futuresAllowed:
self.report(messages.LateFutureImport,
node, [n.name for n in node.names])
else:
self.futuresAllowed = False
for alias in node.names:
if alias.name == '*':
self.scope.importStarred = True
self.report(messages.ImportStarUsed, node, node.module)
continue
name = alias.asname or alias.name
importation = Importation(name, node)
if node.module == '__future__':
importation.used = (self.scope, node)
self.addBinding(node, importation)
for alias in node.names:
name = alias.asname or alias.name
if node.module == '__future__':
importation = FutureImportation(name, node, self.scope)
if alias.name not in __future__.all_feature_names:
self.report(messages.FutureFeatureNotDefined,
node, alias.name)
elif alias.name == '*':
# Only Python 2, local import * is a SyntaxWarning
if not PY2 and not isinstance(self.scope, ModuleScope):
self.report(messages.ImportStarNotPermitted,
node, module)
continue
self.scope.importStarred = True
self.report(messages.ImportStarUsed, node, module)
importation = StarImportation(module, node)
else:
importation = ImportationFrom(name, node,
module, alias.name)
self.addBinding(node, importation)