base.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. # Copyright (c) 2003-2010 LOGILAB S.A. (Paris, FRANCE).
  2. # Copyright (c) 2009-2010 Arista Networks, Inc.
  3. # http://www.logilab.fr/ -- mailto:contact@logilab.fr
  4. # This program is free software; you can redistribute it and/or modify it under
  5. # the terms of the GNU General Public License as published by the Free Software
  6. # Foundation; either version 2 of the License, or (at your option) any later
  7. # version.
  8. #
  9. # This program is distributed in the hope that it will be useful, but WITHOUT
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License along with
  14. # this program; if not, write to the Free Software Foundation, Inc.,
  15. # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. """basic checker for Python code"""
  17. from logilab import astng
  18. from logilab.common.ureports import Table
  19. from logilab.astng import are_exclusive
  20. from pylint.interfaces import IASTNGChecker
  21. from pylint.reporters import diff_string
  22. from pylint.checkers import BaseChecker, EmptyReport
  23. from pylint.checkers.utils import check_messages, clobber_in_except, is_inside_except
  24. import re
  25. # regex for class/function/variable/constant name
  26. CLASS_NAME_RGX = re.compile('[A-Z_][a-zA-Z0-9]+$')
  27. MOD_NAME_RGX = re.compile('(([a-z_][a-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$')
  28. CONST_NAME_RGX = re.compile('(([A-Z_][A-Z0-9_]*)|(__.*__))$')
  29. COMP_VAR_RGX = re.compile('[A-Za-z_][A-Za-z0-9_]*$')
  30. DEFAULT_NAME_RGX = re.compile('[a-z_][a-z0-9_]{2,30}$')
  31. # do not require a doc string on system methods
  32. NO_REQUIRED_DOC_RGX = re.compile('__.*__')
  33. del re
  34. def in_loop(node):
  35. """return True if the node is inside a kind of for loop"""
  36. parent = node.parent
  37. while parent is not None:
  38. if isinstance(parent, (astng.For, astng.ListComp, astng.SetComp,
  39. astng.DictComp, astng.GenExpr)):
  40. return True
  41. parent = parent.parent
  42. return False
  43. def in_nested_list(nested_list, obj):
  44. """return true if the object is an element of <nested_list> or of a nested
  45. list
  46. """
  47. for elmt in nested_list:
  48. if isinstance(elmt, (list, tuple)):
  49. if in_nested_list(elmt, obj):
  50. return True
  51. elif elmt == obj:
  52. return True
  53. return False
  54. def report_by_type_stats(sect, stats, old_stats):
  55. """make a report of
  56. * percentage of different types documented
  57. * percentage of different types with a bad name
  58. """
  59. # percentage of different types documented and/or with a bad name
  60. nice_stats = {}
  61. for node_type in ('module', 'class', 'method', 'function'):
  62. try:
  63. total = stats[node_type]
  64. except KeyError:
  65. raise EmptyReport()
  66. nice_stats[node_type] = {}
  67. if total != 0:
  68. try:
  69. documented = total - stats['undocumented_'+node_type]
  70. percent = (documented * 100.) / total
  71. nice_stats[node_type]['percent_documented'] = '%.2f' % percent
  72. except KeyError:
  73. nice_stats[node_type]['percent_documented'] = 'NC'
  74. try:
  75. percent = (stats['badname_'+node_type] * 100.) / total
  76. nice_stats[node_type]['percent_badname'] = '%.2f' % percent
  77. except KeyError:
  78. nice_stats[node_type]['percent_badname'] = 'NC'
  79. lines = ('type', 'number', 'old number', 'difference',
  80. '%documented', '%badname')
  81. for node_type in ('module', 'class', 'method', 'function'):
  82. new = stats[node_type]
  83. old = old_stats.get(node_type, None)
  84. if old is not None:
  85. diff_str = diff_string(old, new)
  86. else:
  87. old, diff_str = 'NC', 'NC'
  88. lines += (node_type, str(new), str(old), diff_str,
  89. nice_stats[node_type].get('percent_documented', '0'),
  90. nice_stats[node_type].get('percent_badname', '0'))
  91. sect.append(Table(children=lines, cols=6, rheaders=1))
  92. def redefined_by_decorator(node):
  93. """return True if the object is a method redefined via decorator.
  94. For example:
  95. @property
  96. def x(self): return self._x
  97. @x.setter
  98. def x(self, value): self._x = value
  99. """
  100. if node.decorators:
  101. for decorator in node.decorators.nodes:
  102. if (isinstance(decorator, astng.Getattr) and
  103. getattr(decorator.expr, 'name', None) == node.name):
  104. return True
  105. return False
  106. class _BasicChecker(BaseChecker):
  107. __implements__ = IASTNGChecker
  108. name = 'basic'
  109. class BasicErrorChecker(_BasicChecker):
  110. msgs = {
  111. 'E0100': ('__init__ method is a generator',
  112. 'Used when the special class method __init__ is turned into a '
  113. 'generator by a yield in its body.'),
  114. 'E0101': ('Explicit return in __init__',
  115. 'Used when the special class method __init__ has an explicit \
  116. return value.'),
  117. 'E0102': ('%s already defined line %s',
  118. 'Used when a function / class / method is redefined.'),
  119. 'E0103': ('%r not properly in loop',
  120. 'Used when break or continue keywords are used outside a loop.'),
  121. 'E0104': ('Return outside function',
  122. 'Used when a "return" statement is found outside a function or '
  123. 'method.'),
  124. 'E0105': ('Yield outside function',
  125. 'Used when a "yield" statement is found outside a function or '
  126. 'method.'),
  127. 'E0106': ('Return with argument inside generator',
  128. 'Used when a "return" statement with an argument is found '
  129. 'outside in a generator function or method (e.g. with some '
  130. '"yield" statements).'),
  131. 'E0107': ("Use of the non-existent %s operator",
  132. "Used when you attempt to use the C-style pre-increment or"
  133. "pre-decrement operator -- and ++, which doesn't exist in Python."),
  134. }
  135. def __init__(self, linter):
  136. _BasicChecker.__init__(self, linter)
  137. @check_messages('E0102')
  138. def visit_class(self, node):
  139. self._check_redefinition('class', node)
  140. @check_messages('E0100', 'E0101', 'E0102', 'E0106')
  141. def visit_function(self, node):
  142. if not redefined_by_decorator(node):
  143. self._check_redefinition(node.is_method() and 'method' or 'function', node)
  144. # checks for max returns, branch, return in __init__
  145. returns = node.nodes_of_class(astng.Return,
  146. skip_klass=(astng.Function, astng.Class))
  147. if node.is_method() and node.name == '__init__':
  148. if node.is_generator():
  149. self.add_message('E0100', node=node)
  150. else:
  151. values = [r.value for r in returns]
  152. if [v for v in values if not (v is None or
  153. (isinstance(v, astng.Const) and v.value is None)
  154. or (isinstance(v, astng.Name) and v.name == 'None'))]:
  155. self.add_message('E0101', node=node)
  156. elif node.is_generator():
  157. # make sure we don't mix non-None returns and yields
  158. for retnode in returns:
  159. if isinstance(retnode.value, astng.Const) and \
  160. retnode.value.value is not None:
  161. self.add_message('E0106', node=node,
  162. line=retnode.fromlineno)
  163. @check_messages('E0104')
  164. def visit_return(self, node):
  165. if not isinstance(node.frame(), astng.Function):
  166. self.add_message('E0104', node=node)
  167. @check_messages('E0105')
  168. def visit_yield(self, node):
  169. if not isinstance(node.frame(), astng.Function):
  170. self.add_message('E0105', node=node)
  171. @check_messages('E0103')
  172. def visit_continue(self, node):
  173. self._check_in_loop(node, 'continue')
  174. @check_messages('E0103')
  175. def visit_break(self, node):
  176. self._check_in_loop(node, 'break')
  177. @check_messages('E0107')
  178. def visit_unaryop(self, node):
  179. """check use of the non-existent ++ adn -- operator operator"""
  180. if ((node.op in '+-') and
  181. isinstance(node.operand, astng.UnaryOp) and
  182. (node.operand.op == node.op)):
  183. self.add_message('E0107', node=node, args=node.op*2)
  184. def _check_in_loop(self, node, node_name):
  185. """check that a node is inside a for or while loop"""
  186. _node = node.parent
  187. while _node:
  188. if isinstance(_node, (astng.For, astng.While)):
  189. break
  190. _node = _node.parent
  191. else:
  192. self.add_message('E0103', node=node, args=node_name)
  193. def _check_redefinition(self, redeftype, node):
  194. """check for redefinition of a function / method / class name"""
  195. defined_self = node.parent.frame()[node.name]
  196. if defined_self is not node and not are_exclusive(node, defined_self):
  197. self.add_message('E0102', node=node,
  198. args=(redeftype, defined_self.fromlineno))
  199. class BasicChecker(_BasicChecker):
  200. """checks for :
  201. * doc strings
  202. * modules / classes / functions / methods / arguments / variables name
  203. * number of arguments, local variables, branches, returns and statements in
  204. functions, methods
  205. * required module attributes
  206. * dangerous default values as arguments
  207. * redefinition of function / method / class
  208. * uses of the global statement
  209. """
  210. __implements__ = IASTNGChecker
  211. name = 'basic'
  212. msgs = {
  213. 'W0101': ('Unreachable code',
  214. 'Used when there is some code behind a "return" or "raise" \
  215. statement, which will never be accessed.'),
  216. 'W0102': ('Dangerous default value %s as argument',
  217. 'Used when a mutable value as list or dictionary is detected in \
  218. a default value for an argument.'),
  219. 'W0104': ('Statement seems to have no effect',
  220. 'Used when a statement doesn\'t have (or at least seems to) \
  221. any effect.'),
  222. 'W0105': ('String statement has no effect',
  223. 'Used when a string is used as a statement (which of course \
  224. has no effect). This is a particular case of W0104 with its \
  225. own message so you can easily disable it if you\'re using \
  226. those strings as documentation, instead of comments.'),
  227. 'W0106': ('Expression "%s" is assigned to nothing',
  228. 'Used when an expression that is not a function call is assigned\
  229. to nothing. Probably something else was intended.'),
  230. 'W0108': ('Lambda may not be necessary',
  231. 'Used when the body of a lambda expression is a function call \
  232. on the same argument list as the lambda itself; such lambda \
  233. expressions are in all but a few cases replaceable with the \
  234. function being called in the body of the lambda.'),
  235. 'W0109': ("Duplicate key %r in dictionary",
  236. "Used when a dictionary expression binds the same key multiple \
  237. times."),
  238. 'W0122': ('Use of the exec statement',
  239. 'Used when you use the "exec" statement, to discourage its \
  240. usage. That doesn\'t mean you can not use it !'),
  241. 'W0141': ('Used builtin function %r',
  242. 'Used when a black listed builtin function is used (see the '
  243. 'bad-function option). Usual black listed functions are the ones '
  244. 'like map, or filter , where Python offers now some cleaner '
  245. 'alternative like list comprehension.'),
  246. 'W0142': ('Used * or ** magic',
  247. 'Used when a function or method is called using `*args` or '
  248. '`**kwargs` to dispatch arguments. This doesn\'t improve '
  249. 'readability and should be used with care.'),
  250. 'W0150': ("%s statement in finally block may swallow exception",
  251. "Used when a break or a return statement is found inside the \
  252. finally clause of a try...finally block: the exceptions raised \
  253. in the try clause will be silently swallowed instead of being \
  254. re-raised."),
  255. 'W0199': ('Assert called on a 2-uple. Did you mean \'assert x,y\'?',
  256. 'A call of assert on a tuple will always evaluate to true if '
  257. 'the tuple is not empty, and will always evaluate to false if '
  258. 'it is.'),
  259. 'C0121': ('Missing required attribute "%s"', # W0103
  260. 'Used when an attribute required for modules is missing.'),
  261. }
  262. options = (('required-attributes',
  263. {'default' : (), 'type' : 'csv',
  264. 'metavar' : '<attributes>',
  265. 'help' : 'Required attributes for module, separated by a '
  266. 'comma'}
  267. ),
  268. ('bad-functions',
  269. {'default' : ('map', 'filter', 'apply', 'input'),
  270. 'type' :'csv', 'metavar' : '<builtin function names>',
  271. 'help' : 'List of builtins function names that should not be '
  272. 'used, separated by a comma'}
  273. ),
  274. )
  275. reports = ( ('RP0101', 'Statistics by type', report_by_type_stats), )
  276. def __init__(self, linter):
  277. _BasicChecker.__init__(self, linter)
  278. self.stats = None
  279. self._tryfinallys = None
  280. def open(self):
  281. """initialize visit variables and statistics
  282. """
  283. self._tryfinallys = []
  284. self.stats = self.linter.add_stats(module=0, function=0,
  285. method=0, class_=0)
  286. def visit_module(self, node):
  287. """check module name, docstring and required arguments
  288. """
  289. self.stats['module'] += 1
  290. for attr in self.config.required_attributes:
  291. if attr not in node:
  292. self.add_message('C0121', node=node, args=attr)
  293. def visit_class(self, node):
  294. """check module name, docstring and redefinition
  295. increment branch counter
  296. """
  297. self.stats['class'] += 1
  298. @check_messages('W0104', 'W0105')
  299. def visit_discard(self, node):
  300. """check for various kind of statements without effect"""
  301. expr = node.value
  302. if isinstance(expr, astng.Const) and isinstance(expr.value,
  303. basestring):
  304. # treat string statement in a separated message
  305. self.add_message('W0105', node=node)
  306. return
  307. # ignore if this is :
  308. # * a direct function call
  309. # * the unique child of a try/except body
  310. # * a yield (which are wrapped by a discard node in _ast XXX)
  311. # warn W0106 if we have any underlying function call (we can't predict
  312. # side effects), else W0104
  313. if (isinstance(expr, (astng.Yield, astng.CallFunc)) or
  314. (isinstance(node.parent, astng.TryExcept) and
  315. node.parent.body == [node])):
  316. return
  317. if any(expr.nodes_of_class(astng.CallFunc)):
  318. self.add_message('W0106', node=node, args=expr.as_string())
  319. else:
  320. self.add_message('W0104', node=node)
  321. @check_messages('W0108')
  322. def visit_lambda(self, node):
  323. """check whether or not the lambda is suspicious
  324. """
  325. # if the body of the lambda is a call expression with the same
  326. # argument list as the lambda itself, then the lambda is
  327. # possibly unnecessary and at least suspicious.
  328. if node.args.defaults:
  329. # If the arguments of the lambda include defaults, then a
  330. # judgment cannot be made because there is no way to check
  331. # that the defaults defined by the lambda are the same as
  332. # the defaults defined by the function called in the body
  333. # of the lambda.
  334. return
  335. call = node.body
  336. if not isinstance(call, astng.CallFunc):
  337. # The body of the lambda must be a function call expression
  338. # for the lambda to be unnecessary.
  339. return
  340. # XXX are lambda still different with astng >= 0.18 ?
  341. # *args and **kwargs need to be treated specially, since they
  342. # are structured differently between the lambda and the function
  343. # call (in the lambda they appear in the args.args list and are
  344. # indicated as * and ** by two bits in the lambda's flags, but
  345. # in the function call they are omitted from the args list and
  346. # are indicated by separate attributes on the function call node).
  347. ordinary_args = list(node.args.args)
  348. if node.args.kwarg:
  349. if (not call.kwargs
  350. or not isinstance(call.kwargs, astng.Name)
  351. or node.args.kwarg != call.kwargs.name):
  352. return
  353. elif call.kwargs:
  354. return
  355. if node.args.vararg:
  356. if (not call.starargs
  357. or not isinstance(call.starargs, astng.Name)
  358. or node.args.vararg != call.starargs.name):
  359. return
  360. elif call.starargs:
  361. return
  362. # The "ordinary" arguments must be in a correspondence such that:
  363. # ordinary_args[i].name == call.args[i].name.
  364. if len(ordinary_args) != len(call.args):
  365. return
  366. for i in xrange(len(ordinary_args)):
  367. if not isinstance(call.args[i], astng.Name):
  368. return
  369. if node.args.args[i].name != call.args[i].name:
  370. return
  371. self.add_message('W0108', line=node.fromlineno, node=node)
  372. def visit_function(self, node):
  373. """check function name, docstring, arguments, redefinition,
  374. variable names, max locals
  375. """
  376. self.stats[node.is_method() and 'method' or 'function'] += 1
  377. # check for dangerous default values as arguments
  378. for default in node.args.defaults:
  379. try:
  380. value = default.infer().next()
  381. except astng.InferenceError:
  382. continue
  383. if isinstance(value, (astng.Dict, astng.List)):
  384. if value is default:
  385. msg = default.as_string()
  386. else:
  387. msg = '%s (%s)' % (default.as_string(), value.as_string())
  388. self.add_message('W0102', node=node, args=(msg,))
  389. if value.qname() == '__builtin__.set':
  390. if isinstance(default, astng.CallFunc):
  391. msg = default.as_string()
  392. else:
  393. msg = '%s (%s)' % (default.as_string(), value.qname())
  394. self.add_message('W0102', node=node, args=(msg,))
  395. @check_messages('W0101', 'W0150')
  396. def visit_return(self, node):
  397. """1 - check is the node has a right sibling (if so, that's some
  398. unreachable code)
  399. 2 - check is the node is inside the finally clause of a try...finally
  400. block
  401. """
  402. self._check_unreachable(node)
  403. # Is it inside final body of a try...finally bloc ?
  404. self._check_not_in_finally(node, 'return', (astng.Function,))
  405. @check_messages('W0101')
  406. def visit_continue(self, node):
  407. """check is the node has a right sibling (if so, that's some unreachable
  408. code)
  409. """
  410. self._check_unreachable(node)
  411. @check_messages('W0101', 'W0150')
  412. def visit_break(self, node):
  413. """1 - check is the node has a right sibling (if so, that's some
  414. unreachable code)
  415. 2 - check is the node is inside the finally clause of a try...finally
  416. block
  417. """
  418. # 1 - Is it right sibling ?
  419. self._check_unreachable(node)
  420. # 2 - Is it inside final body of a try...finally bloc ?
  421. self._check_not_in_finally(node, 'break', (astng.For, astng.While,))
  422. @check_messages('W0101')
  423. def visit_raise(self, node):
  424. """check is the node has a right sibling (if so, that's some unreachable
  425. code)
  426. """
  427. self._check_unreachable(node)
  428. @check_messages('W0122')
  429. def visit_exec(self, node):
  430. """just print a warning on exec statements"""
  431. self.add_message('W0122', node=node)
  432. @check_messages('W0141', 'W0142')
  433. def visit_callfunc(self, node):
  434. """visit a CallFunc node -> check if this is not a blacklisted builtin
  435. call and check for * or ** use
  436. """
  437. if isinstance(node.func, astng.Name):
  438. name = node.func.name
  439. # ignore the name if it's not a builtin (i.e. not defined in the
  440. # locals nor globals scope)
  441. if not (name in node.frame() or
  442. name in node.root()):
  443. if name in self.config.bad_functions:
  444. self.add_message('W0141', node=node, args=name)
  445. if node.starargs or node.kwargs:
  446. scope = node.scope()
  447. if isinstance(scope, astng.Function):
  448. toprocess = [(n, vn) for (n, vn) in ((node.starargs, scope.args.vararg),
  449. (node.kwargs, scope.args.kwarg)) if n]
  450. if toprocess:
  451. for cfnode, fargname in toprocess[:]:
  452. if getattr(cfnode, 'name', None) == fargname:
  453. toprocess.remove((cfnode, fargname))
  454. if not toprocess:
  455. return # W0142 can be skipped
  456. self.add_message('W0142', node=node.func)
  457. @check_messages('W0199')
  458. def visit_assert(self, node):
  459. """check the use of an assert statement on a tuple."""
  460. if node.fail is None and isinstance(node.test, astng.Tuple) and \
  461. len(node.test.elts) == 2:
  462. self.add_message('W0199', line=node.fromlineno, node=node)
  463. @check_messages('W0109')
  464. def visit_dict(self, node):
  465. """check duplicate key in dictionary"""
  466. keys = set()
  467. for k, v in node.items:
  468. if isinstance(k, astng.Const):
  469. key = k.value
  470. if key in keys:
  471. self.add_message('W0109', node=node, args=key)
  472. keys.add(key)
  473. def visit_tryfinally(self, node):
  474. """update try...finally flag"""
  475. self._tryfinallys.append(node)
  476. def leave_tryfinally(self, node):
  477. """update try...finally flag"""
  478. self._tryfinallys.pop()
  479. def _check_unreachable(self, node):
  480. """check unreachable code"""
  481. unreach_stmt = node.next_sibling()
  482. if unreach_stmt is not None:
  483. self.add_message('W0101', node=unreach_stmt)
  484. def _check_not_in_finally(self, node, node_name, breaker_classes=()):
  485. """check that a node is not inside a finally clause of a
  486. try...finally statement.
  487. If we found before a try...finally bloc a parent which its type is
  488. in breaker_classes, we skip the whole check."""
  489. # if self._tryfinallys is empty, we're not a in try...finally bloc
  490. if not self._tryfinallys:
  491. return
  492. # the node could be a grand-grand...-children of the try...finally
  493. _parent = node.parent
  494. _node = node
  495. while _parent and not isinstance(_parent, breaker_classes):
  496. if hasattr(_parent, 'finalbody') and _node in _parent.finalbody:
  497. self.add_message('W0150', node=node, args=node_name)
  498. return
  499. _node = _parent
  500. _parent = _node.parent
  501. class NameChecker(_BasicChecker):
  502. msgs = {
  503. 'C0102': ('Black listed name "%s"',
  504. 'Used when the name is listed in the black list (unauthorized \
  505. names).'),
  506. 'C0103': ('Invalid name "%s" (should match %s)',
  507. 'Used when the name doesn\'t match the regular expression \
  508. associated to its type (constant, variable, class...).'),
  509. }
  510. options = (('module-rgx',
  511. {'default' : MOD_NAME_RGX,
  512. 'type' :'regexp', 'metavar' : '<regexp>',
  513. 'help' : 'Regular expression which should only match correct '
  514. 'module names'}
  515. ),
  516. ('const-rgx',
  517. {'default' : CONST_NAME_RGX,
  518. 'type' :'regexp', 'metavar' : '<regexp>',
  519. 'help' : 'Regular expression which should only match correct '
  520. 'module level names'}
  521. ),
  522. ('class-rgx',
  523. {'default' : CLASS_NAME_RGX,
  524. 'type' :'regexp', 'metavar' : '<regexp>',
  525. 'help' : 'Regular expression which should only match correct '
  526. 'class names'}
  527. ),
  528. ('function-rgx',
  529. {'default' : DEFAULT_NAME_RGX,
  530. 'type' :'regexp', 'metavar' : '<regexp>',
  531. 'help' : 'Regular expression which should only match correct '
  532. 'function names'}
  533. ),
  534. ('method-rgx',
  535. {'default' : DEFAULT_NAME_RGX,
  536. 'type' :'regexp', 'metavar' : '<regexp>',
  537. 'help' : 'Regular expression which should only match correct '
  538. 'method names'}
  539. ),
  540. ('attr-rgx',
  541. {'default' : DEFAULT_NAME_RGX,
  542. 'type' :'regexp', 'metavar' : '<regexp>',
  543. 'help' : 'Regular expression which should only match correct '
  544. 'instance attribute names'}
  545. ),
  546. ('argument-rgx',
  547. {'default' : DEFAULT_NAME_RGX,
  548. 'type' :'regexp', 'metavar' : '<regexp>',
  549. 'help' : 'Regular expression which should only match correct '
  550. 'argument names'}),
  551. ('variable-rgx',
  552. {'default' : DEFAULT_NAME_RGX,
  553. 'type' :'regexp', 'metavar' : '<regexp>',
  554. 'help' : 'Regular expression which should only match correct '
  555. 'variable names'}
  556. ),
  557. ('inlinevar-rgx',
  558. {'default' : COMP_VAR_RGX,
  559. 'type' :'regexp', 'metavar' : '<regexp>',
  560. 'help' : 'Regular expression which should only match correct '
  561. 'list comprehension / generator expression variable \
  562. names'}
  563. ),
  564. # XXX use set
  565. ('good-names',
  566. {'default' : ('i', 'j', 'k', 'ex', 'Run', '_'),
  567. 'type' :'csv', 'metavar' : '<names>',
  568. 'help' : 'Good variable names which should always be accepted,'
  569. ' separated by a comma'}
  570. ),
  571. ('bad-names',
  572. {'default' : ('foo', 'bar', 'baz', 'toto', 'tutu', 'tata'),
  573. 'type' :'csv', 'metavar' : '<names>',
  574. 'help' : 'Bad variable names which should always be refused, '
  575. 'separated by a comma'}
  576. ),
  577. )
  578. def open(self):
  579. self.stats = self.linter.add_stats(badname_module=0,
  580. badname_class=0, badname_function=0,
  581. badname_method=0, badname_attr=0,
  582. badname_const=0,
  583. badname_variable=0,
  584. badname_inlinevar=0,
  585. badname_argument=0)
  586. @check_messages('C0102', 'C0103')
  587. def visit_module(self, node):
  588. self._check_name('module', node.name.split('.')[-1], node)
  589. @check_messages('C0102', 'C0103')
  590. def visit_class(self, node):
  591. self._check_name('class', node.name, node)
  592. for attr, anodes in node.instance_attrs.items():
  593. self._check_name('attr', attr, anodes[0])
  594. @check_messages('C0102', 'C0103')
  595. def visit_function(self, node):
  596. self._check_name(node.is_method() and 'method' or 'function',
  597. node.name, node)
  598. # check arguments name
  599. args = node.args.args
  600. if args is not None:
  601. self._recursive_check_names(args, node)
  602. @check_messages('C0102', 'C0103')
  603. def visit_assname(self, node):
  604. """check module level assigned names"""
  605. frame = node.frame()
  606. ass_type = node.ass_type()
  607. if isinstance(ass_type, (astng.Comprehension, astng.Comprehension)):
  608. self._check_name('inlinevar', node.name, node)
  609. elif isinstance(frame, astng.Module):
  610. if isinstance(ass_type, astng.Assign) and not in_loop(ass_type):
  611. self._check_name('const', node.name, node)
  612. elif isinstance(ass_type, astng.ExceptHandler):
  613. self._check_name('variable', node.name, node)
  614. elif isinstance(frame, astng.Function):
  615. # global introduced variable aren't in the function locals
  616. if node.name in frame:
  617. self._check_name('variable', node.name, node)
  618. def _recursive_check_names(self, args, node):
  619. """check names in a possibly recursive list <arg>"""
  620. for arg in args:
  621. if isinstance(arg, astng.AssName):
  622. self._check_name('argument', arg.name, node)
  623. else:
  624. self._recursive_check_names(arg.elts, node)
  625. def _check_name(self, node_type, name, node):
  626. """check for a name using the type's regexp"""
  627. if is_inside_except(node):
  628. clobbering, _ = clobber_in_except(node)
  629. if clobbering:
  630. return
  631. if name in self.config.good_names:
  632. return
  633. if name in self.config.bad_names:
  634. self.stats['badname_' + node_type] += 1
  635. self.add_message('C0102', node=node, args=name)
  636. return
  637. regexp = getattr(self.config, node_type + '_rgx')
  638. if regexp.match(name) is None:
  639. self.add_message('C0103', node=node, args=(name, regexp.pattern))
  640. self.stats['badname_' + node_type] += 1
  641. class DocStringChecker(_BasicChecker):
  642. msgs = {
  643. 'C0111': ('Missing docstring', # W0131
  644. 'Used when a module, function, class or method has no docstring.\
  645. Some special methods like __init__ doesn\'t necessary require a \
  646. docstring.'),
  647. 'C0112': ('Empty docstring', # W0132
  648. 'Used when a module, function, class or method has an empty \
  649. docstring (it would be too easy ;).'),
  650. }
  651. options = (('no-docstring-rgx',
  652. {'default' : NO_REQUIRED_DOC_RGX,
  653. 'type' : 'regexp', 'metavar' : '<regexp>',
  654. 'help' : 'Regular expression which should only match '
  655. 'functions or classes name which do not require a '
  656. 'docstring'}
  657. ),
  658. )
  659. def open(self):
  660. self.stats = self.linter.add_stats(undocumented_module=0,
  661. undocumented_function=0,
  662. undocumented_method=0,
  663. undocumented_class=0)
  664. def visit_module(self, node):
  665. self._check_docstring('module', node)
  666. def visit_class(self, node):
  667. if self.config.no_docstring_rgx.match(node.name) is None:
  668. self._check_docstring('class', node)
  669. def visit_function(self, node):
  670. if self.config.no_docstring_rgx.match(node.name) is None:
  671. ftype = node.is_method() and 'method' or 'function'
  672. if isinstance(node.parent.frame(), astng.Class):
  673. overridden = False
  674. # check if node is from a method overridden by its ancestor
  675. for ancestor in node.parent.frame().ancestors():
  676. if node.name in ancestor and \
  677. isinstance(ancestor[node.name], astng.Function):
  678. overridden = True
  679. break
  680. if not overridden:
  681. self._check_docstring(ftype, node)
  682. else:
  683. self._check_docstring(ftype, node)
  684. def _check_docstring(self, node_type, node):
  685. """check the node has a non empty docstring"""
  686. docstring = node.doc
  687. if docstring is None:
  688. self.stats['undocumented_'+node_type] += 1
  689. self.add_message('C0111', node=node)
  690. elif not docstring.strip():
  691. self.stats['undocumented_'+node_type] += 1
  692. self.add_message('C0112', node=node)
  693. class PassChecker(_BasicChecker):
  694. """check is the pass statement is really necessary"""
  695. msgs = {'W0107': ('Unnecessary pass statement',
  696. 'Used when a "pass" statement that can be avoided is '
  697. 'encountered.)'),
  698. }
  699. def visit_pass(self, node):
  700. if len(node.parent.child_sequence(node)) > 1:
  701. self.add_message('W0107', node=node)
  702. def register(linter):
  703. """required method to auto register this checker"""
  704. linter.register_checker(BasicErrorChecker(linter))
  705. linter.register_checker(BasicChecker(linter))
  706. linter.register_checker(NameChecker(linter))
  707. linter.register_checker(DocStringChecker(linter))
  708. linter.register_checker(PassChecker(linter))