checker.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. # -*- test-case-name: pyflakes -*-
  2. # (c) 2005-2010 Divmod, Inc.
  3. # See LICENSE file for details
  4. import __builtin__
  5. import os.path
  6. import _ast
  7. from . import messages
  8. # utility function to iterate over an AST node's children, adapted
  9. # from Python 2.6's standard ast module
  10. try:
  11. import ast
  12. iter_child_nodes = ast.iter_child_nodes
  13. except (ImportError, AttributeError):
  14. def iter_child_nodes(node, astcls=_ast.AST):
  15. """
  16. Yield all direct child nodes of *node*, that is, all fields that are nodes
  17. and all items of fields that are lists of nodes.
  18. """
  19. for name in node._fields:
  20. field = getattr(node, name, None)
  21. if isinstance(field, astcls):
  22. yield field
  23. elif isinstance(field, list):
  24. for item in field:
  25. yield item
  26. class Binding(object):
  27. """
  28. Represents the binding of a value to a name.
  29. The checker uses this to keep track of which names have been bound and
  30. which names have not. See L{Assignment} for a special type of binding that
  31. is checked with stricter rules.
  32. @ivar used: pair of (L{Scope}, line-number) indicating the scope and
  33. line number that this binding was last used
  34. """
  35. def __init__(self, name, source):
  36. self.name = name
  37. self.source = source
  38. self.used = False
  39. def __str__(self):
  40. return self.name
  41. def __repr__(self):
  42. return '<%s object %r from line %r at 0x%x>' % (self.__class__.__name__,
  43. self.name,
  44. self.source.lineno,
  45. id(self))
  46. class UnBinding(Binding):
  47. '''Created by the 'del' operator.'''
  48. class Importation(Binding):
  49. """
  50. A binding created by an import statement.
  51. @ivar fullName: The complete name given to the import statement,
  52. possibly including multiple dotted components.
  53. @type fullName: C{str}
  54. """
  55. def __init__(self, name, source):
  56. self.fullName = name
  57. name = name.split('.')[0]
  58. super(Importation, self).__init__(name, source)
  59. class Argument(Binding):
  60. """
  61. Represents binding a name as an argument.
  62. """
  63. class Assignment(Binding):
  64. """
  65. Represents binding a name with an explicit assignment.
  66. The checker will raise warnings for any Assignment that isn't used. Also,
  67. the checker does not consider assignments in tuple/list unpacking to be
  68. Assignments, rather it treats them as simple Bindings.
  69. """
  70. class FunctionDefinition(Binding):
  71. _property_decorator = False
  72. class ExportBinding(Binding):
  73. """
  74. A binding created by an C{__all__} assignment. If the names in the list
  75. can be determined statically, they will be treated as names for export and
  76. additional checking applied to them.
  77. The only C{__all__} assignment that can be recognized is one which takes
  78. the value of a literal list containing literal strings. For example::
  79. __all__ = ["foo", "bar"]
  80. Names which are imported and not otherwise used but appear in the value of
  81. C{__all__} will not have an unused import warning reported for them.
  82. """
  83. def names(self):
  84. """
  85. Return a list of the names referenced by this binding.
  86. """
  87. names = []
  88. if isinstance(self.source, _ast.List):
  89. for node in self.source.elts:
  90. if isinstance(node, _ast.Str):
  91. names.append(node.s)
  92. return names
  93. class Scope(dict):
  94. importStarred = False # set to True when import * is found
  95. def __repr__(self):
  96. return '<%s at 0x%x %s>' % (self.__class__.__name__, id(self), dict.__repr__(self))
  97. def __init__(self):
  98. super(Scope, self).__init__()
  99. class ClassScope(Scope):
  100. pass
  101. class FunctionScope(Scope):
  102. """
  103. I represent a name scope for a function.
  104. @ivar globals: Names declared 'global' in this function.
  105. """
  106. def __init__(self):
  107. super(FunctionScope, self).__init__()
  108. self.globals = {}
  109. class ModuleScope(Scope):
  110. pass
  111. # Globally defined names which are not attributes of the __builtin__ module.
  112. _MAGIC_GLOBALS = ['__file__', '__builtins__']
  113. class Checker(object):
  114. """
  115. I check the cleanliness and sanity of Python code.
  116. @ivar _deferredFunctions: Tracking list used by L{deferFunction}. Elements
  117. of the list are two-tuples. The first element is the callable passed
  118. to L{deferFunction}. The second element is a copy of the scope stack
  119. at the time L{deferFunction} was called.
  120. @ivar _deferredAssignments: Similar to C{_deferredFunctions}, but for
  121. callables which are deferred assignment checks.
  122. """
  123. nodeDepth = 0
  124. traceTree = False
  125. def __init__(self, tree, filename=None):
  126. if filename is None:
  127. filename = '(none)'
  128. self._deferredFunctions = []
  129. self._deferredAssignments = []
  130. self.dead_scopes = []
  131. self.messages = []
  132. self.filename = filename
  133. self.scopeStack = [ModuleScope()]
  134. self.futuresAllowed = True
  135. self.handleChildren(tree)
  136. self._runDeferred(self._deferredFunctions)
  137. # Set _deferredFunctions to None so that deferFunction will fail
  138. # noisily if called after we've run through the deferred functions.
  139. self._deferredFunctions = None
  140. self._runDeferred(self._deferredAssignments)
  141. # Set _deferredAssignments to None so that deferAssignment will fail
  142. # noisly if called after we've run through the deferred assignments.
  143. self._deferredAssignments = None
  144. del self.scopeStack[1:]
  145. self.popScope()
  146. self.check_dead_scopes()
  147. def deferFunction(self, callable):
  148. '''
  149. Schedule a function handler to be called just before completion.
  150. This is used for handling function bodies, which must be deferred
  151. because code later in the file might modify the global scope. When
  152. `callable` is called, the scope at the time this is called will be
  153. restored, however it will contain any new bindings added to it.
  154. '''
  155. self._deferredFunctions.append((callable, self.scopeStack[:]))
  156. def deferAssignment(self, callable):
  157. """
  158. Schedule an assignment handler to be called just after deferred
  159. function handlers.
  160. """
  161. self._deferredAssignments.append((callable, self.scopeStack[:]))
  162. def _runDeferred(self, deferred):
  163. """
  164. Run the callables in C{deferred} using their associated scope stack.
  165. """
  166. for handler, scope in deferred:
  167. self.scopeStack = scope
  168. handler()
  169. def scope(self):
  170. return self.scopeStack[-1]
  171. scope = property(scope)
  172. def popScope(self):
  173. self.dead_scopes.append(self.scopeStack.pop())
  174. def check_dead_scopes(self):
  175. """
  176. Look at scopes which have been fully examined and report names in them
  177. which were imported but unused.
  178. """
  179. for scope in self.dead_scopes:
  180. export = isinstance(scope.get('__all__'), ExportBinding)
  181. if export:
  182. all = scope['__all__'].names()
  183. if os.path.split(self.filename)[1] != '__init__.py':
  184. # Look for possible mistakes in the export list
  185. undefined = set(all) - set(scope)
  186. for name in undefined:
  187. self.report(
  188. messages.UndefinedExport,
  189. scope['__all__'].source,
  190. name)
  191. else:
  192. all = []
  193. # Look for imported names that aren't used.
  194. for importation in scope.itervalues():
  195. if isinstance(importation, Importation):
  196. if not importation.used and importation.name not in all:
  197. self.report(
  198. messages.UnusedImport,
  199. importation.source,
  200. importation.name)
  201. def pushFunctionScope(self):
  202. self.scopeStack.append(FunctionScope())
  203. def pushClassScope(self):
  204. self.scopeStack.append(ClassScope())
  205. def report(self, messageClass, *args, **kwargs):
  206. self.messages.append(messageClass(self.filename, *args, **kwargs))
  207. def handleChildren(self, tree):
  208. for node in iter_child_nodes(tree):
  209. self.handleNode(node, tree)
  210. def isDocstring(self, node):
  211. """
  212. Determine if the given node is a docstring, as long as it is at the
  213. correct place in the node tree.
  214. """
  215. return isinstance(node, _ast.Str) or \
  216. (isinstance(node, _ast.Expr) and
  217. isinstance(node.value, _ast.Str))
  218. def handleNode(self, node, parent):
  219. node.parent = parent
  220. if self.traceTree:
  221. print ' ' * self.nodeDepth + node.__class__.__name__
  222. self.nodeDepth += 1
  223. if self.futuresAllowed and not \
  224. (isinstance(node, _ast.ImportFrom) or self.isDocstring(node)):
  225. self.futuresAllowed = False
  226. nodeType = node.__class__.__name__.upper()
  227. try:
  228. handler = getattr(self, nodeType)
  229. handler(node)
  230. finally:
  231. self.nodeDepth -= 1
  232. if self.traceTree:
  233. print ' ' * self.nodeDepth + 'end ' + node.__class__.__name__
  234. def ignore(self, node):
  235. pass
  236. # "stmt" type nodes
  237. RETURN = DELETE = PRINT = WHILE = IF = WITH = RAISE = TRYEXCEPT = \
  238. TRYFINALLY = ASSERT = EXEC = EXPR = handleChildren
  239. CONTINUE = BREAK = PASS = ignore
  240. # "expr" type nodes
  241. BOOLOP = BINOP = UNARYOP = IFEXP = DICT = SET = YIELD = COMPARE = \
  242. CALL = REPR = ATTRIBUTE = SUBSCRIPT = LIST = TUPLE = handleChildren
  243. NUM = STR = ELLIPSIS = ignore
  244. # "slice" type nodes
  245. SLICE = EXTSLICE = INDEX = handleChildren
  246. # expression contexts are node instances too, though being constants
  247. LOAD = STORE = DEL = AUGLOAD = AUGSTORE = PARAM = ignore
  248. # same for operators
  249. AND = OR = ADD = SUB = MULT = DIV = MOD = POW = LSHIFT = RSHIFT = \
  250. BITOR = BITXOR = BITAND = FLOORDIV = INVERT = NOT = UADD = USUB = \
  251. EQ = NOTEQ = LT = LTE = GT = GTE = IS = ISNOT = IN = NOTIN = ignore
  252. # additional node types
  253. COMPREHENSION = EXCEPTHANDLER = KEYWORD = handleChildren
  254. def addBinding(self, loc, value, reportRedef=True):
  255. '''Called when a binding is altered.
  256. - `loc` is the location (an object with lineno and optionally
  257. col_offset attributes) of the statement responsible for the change
  258. - `value` is the optional new value, a Binding instance, associated
  259. with the binding; if None, the binding is deleted if it exists.
  260. - if `reportRedef` is True (default), rebinding while unused will be
  261. reported.
  262. '''
  263. if (isinstance(self.scope.get(value.name), FunctionDefinition)
  264. and isinstance(value, FunctionDefinition)):
  265. if not value._property_decorator:
  266. self.report(messages.RedefinedFunction,
  267. loc, value.name, self.scope[value.name].source)
  268. if not isinstance(self.scope, ClassScope):
  269. for scope in self.scopeStack[::-1]:
  270. existing = scope.get(value.name)
  271. if (isinstance(existing, Importation)
  272. and not existing.used
  273. and (not isinstance(value, Importation) or value.fullName == existing.fullName)
  274. and reportRedef):
  275. self.report(messages.RedefinedWhileUnused,
  276. loc, value.name, scope[value.name].source)
  277. if isinstance(value, UnBinding):
  278. try:
  279. del self.scope[value.name]
  280. except KeyError:
  281. self.report(messages.UndefinedName, loc, value.name)
  282. else:
  283. self.scope[value.name] = value
  284. def GLOBAL(self, node):
  285. """
  286. Keep track of globals declarations.
  287. """
  288. if isinstance(self.scope, FunctionScope):
  289. self.scope.globals.update(dict.fromkeys(node.names))
  290. def LISTCOMP(self, node):
  291. # handle generators before element
  292. for gen in node.generators:
  293. self.handleNode(gen, node)
  294. self.handleNode(node.elt, node)
  295. GENERATOREXP = SETCOMP = LISTCOMP
  296. # dictionary comprehensions; introduced in Python 2.7
  297. def DICTCOMP(self, node):
  298. for gen in node.generators:
  299. self.handleNode(gen, node)
  300. self.handleNode(node.key, node)
  301. self.handleNode(node.value, node)
  302. def FOR(self, node):
  303. """
  304. Process bindings for loop variables.
  305. """
  306. vars = []
  307. def collectLoopVars(n):
  308. if isinstance(n, _ast.Name):
  309. vars.append(n.id)
  310. elif isinstance(n, _ast.expr_context):
  311. return
  312. else:
  313. for c in iter_child_nodes(n):
  314. collectLoopVars(c)
  315. collectLoopVars(node.target)
  316. for varn in vars:
  317. if (isinstance(self.scope.get(varn), Importation)
  318. # unused ones will get an unused import warning
  319. and self.scope[varn].used):
  320. self.report(messages.ImportShadowedByLoopVar,
  321. node, varn, self.scope[varn].source)
  322. self.handleChildren(node)
  323. def NAME(self, node):
  324. """
  325. Handle occurrence of Name (which can be a load/store/delete access.)
  326. """
  327. # Locate the name in locals / function / globals scopes.
  328. if isinstance(node.ctx, (_ast.Load, _ast.AugLoad)):
  329. # try local scope
  330. importStarred = self.scope.importStarred
  331. try:
  332. self.scope[node.id].used = (self.scope, node)
  333. except KeyError:
  334. pass
  335. else:
  336. return
  337. # try enclosing function scopes
  338. for scope in self.scopeStack[-2:0:-1]:
  339. importStarred = importStarred or scope.importStarred
  340. if not isinstance(scope, FunctionScope):
  341. continue
  342. try:
  343. scope[node.id].used = (self.scope, node)
  344. except KeyError:
  345. pass
  346. else:
  347. return
  348. # try global scope
  349. importStarred = importStarred or self.scopeStack[0].importStarred
  350. try:
  351. self.scopeStack[0][node.id].used = (self.scope, node)
  352. except KeyError:
  353. if ((not hasattr(__builtin__, node.id))
  354. and node.id not in _MAGIC_GLOBALS
  355. and not importStarred):
  356. if (os.path.basename(self.filename) == '__init__.py' and
  357. node.id == '__path__'):
  358. # the special name __path__ is valid only in packages
  359. pass
  360. else:
  361. self.report(messages.UndefinedName, node, node.id)
  362. elif isinstance(node.ctx, (_ast.Store, _ast.AugStore)):
  363. # if the name hasn't already been defined in the current scope
  364. if isinstance(self.scope, FunctionScope) and node.id not in self.scope:
  365. # for each function or module scope above us
  366. for scope in self.scopeStack[:-1]:
  367. if not isinstance(scope, (FunctionScope, ModuleScope)):
  368. continue
  369. # if the name was defined in that scope, and the name has
  370. # been accessed already in the current scope, and hasn't
  371. # been declared global
  372. if (node.id in scope
  373. and scope[node.id].used
  374. and scope[node.id].used[0] is self.scope
  375. and node.id not in self.scope.globals):
  376. # then it's probably a mistake
  377. self.report(messages.UndefinedLocal,
  378. scope[node.id].used[1],
  379. node.id,
  380. scope[node.id].source)
  381. break
  382. if isinstance(node.parent,
  383. (_ast.For, _ast.comprehension, _ast.Tuple, _ast.List)):
  384. binding = Binding(node.id, node)
  385. elif (node.id == '__all__' and
  386. isinstance(self.scope, ModuleScope)):
  387. binding = ExportBinding(node.id, node.parent.value)
  388. else:
  389. binding = Assignment(node.id, node)
  390. if node.id in self.scope:
  391. binding.used = self.scope[node.id].used
  392. self.addBinding(node, binding)
  393. elif isinstance(node.ctx, _ast.Del):
  394. if isinstance(self.scope, FunctionScope) and \
  395. node.id in self.scope.globals:
  396. del self.scope.globals[node.id]
  397. else:
  398. self.addBinding(node, UnBinding(node.id, node))
  399. else:
  400. # must be a Param context -- this only happens for names in function
  401. # arguments, but these aren't dispatched through here
  402. raise RuntimeError(
  403. "Got impossible expression context: %r" % (node.ctx,))
  404. def FUNCTIONDEF(self, node):
  405. # the decorators attribute is called decorator_list as of Python 2.6
  406. if hasattr(node, 'decorators'):
  407. for deco in node.decorators:
  408. self.handleNode(deco, node)
  409. else:
  410. for deco in node.decorator_list:
  411. self.handleNode(deco, node)
  412. # Check for property decorator
  413. func_def = FunctionDefinition(node.name, node)
  414. for decorator in node.decorator_list:
  415. if getattr(decorator, 'attr', None) in ('setter', 'deleter'):
  416. func_def._property_decorator = True
  417. self.addBinding(node, func_def)
  418. self.LAMBDA(node)
  419. def LAMBDA(self, node):
  420. for default in node.args.defaults:
  421. self.handleNode(default, node)
  422. def runFunction():
  423. args = []
  424. def addArgs(arglist):
  425. for arg in arglist:
  426. if isinstance(arg, _ast.Tuple):
  427. addArgs(arg.elts)
  428. else:
  429. if arg.id in args:
  430. self.report(messages.DuplicateArgument,
  431. node, arg.id)
  432. args.append(arg.id)
  433. self.pushFunctionScope()
  434. addArgs(node.args.args)
  435. # vararg/kwarg identifiers are not Name nodes
  436. if node.args.vararg:
  437. args.append(node.args.vararg)
  438. if node.args.kwarg:
  439. args.append(node.args.kwarg)
  440. for name in args:
  441. self.addBinding(node, Argument(name, node), reportRedef=False)
  442. if isinstance(node.body, list):
  443. # case for FunctionDefs
  444. for stmt in node.body:
  445. self.handleNode(stmt, node)
  446. else:
  447. # case for Lambdas
  448. self.handleNode(node.body, node)
  449. def checkUnusedAssignments():
  450. """
  451. Check to see if any assignments have not been used.
  452. """
  453. for name, binding in self.scope.iteritems():
  454. if (not binding.used and not name in self.scope.globals
  455. and isinstance(binding, Assignment)):
  456. self.report(messages.UnusedVariable,
  457. binding.source, name)
  458. self.deferAssignment(checkUnusedAssignments)
  459. self.popScope()
  460. self.deferFunction(runFunction)
  461. def CLASSDEF(self, node):
  462. """
  463. Check names used in a class definition, including its decorators, base
  464. classes, and the body of its definition. Additionally, add its name to
  465. the current scope.
  466. """
  467. # decorator_list is present as of Python 2.6
  468. for deco in getattr(node, 'decorator_list', []):
  469. self.handleNode(deco, node)
  470. for baseNode in node.bases:
  471. self.handleNode(baseNode, node)
  472. self.pushClassScope()
  473. for stmt in node.body:
  474. self.handleNode(stmt, node)
  475. self.popScope()
  476. self.addBinding(node, Binding(node.name, node))
  477. def ASSIGN(self, node):
  478. self.handleNode(node.value, node)
  479. for target in node.targets:
  480. self.handleNode(target, node)
  481. def AUGASSIGN(self, node):
  482. # AugAssign is awkward: must set the context explicitly and visit twice,
  483. # once with AugLoad context, once with AugStore context
  484. node.target.ctx = _ast.AugLoad()
  485. self.handleNode(node.target, node)
  486. self.handleNode(node.value, node)
  487. node.target.ctx = _ast.AugStore()
  488. self.handleNode(node.target, node)
  489. def IMPORT(self, node):
  490. for alias in node.names:
  491. name = alias.asname or alias.name
  492. importation = Importation(name, node)
  493. self.addBinding(node, importation)
  494. def IMPORTFROM(self, node):
  495. if node.module == '__future__':
  496. if not self.futuresAllowed:
  497. self.report(messages.LateFutureImport, node,
  498. [n.name for n in node.names])
  499. else:
  500. self.futuresAllowed = False
  501. for alias in node.names:
  502. if alias.name == '*':
  503. self.scope.importStarred = True
  504. self.report(messages.ImportStarUsed, node, node.module)
  505. continue
  506. name = alias.asname or alias.name
  507. importation = Importation(name, node)
  508. if node.module == '__future__':
  509. importation.used = (self.scope, node)
  510. self.addBinding(node, importation)