Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bad_builtin.py 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # Copyright (c) 2016 Claudiu Popa <pcmanticore@gmail.com>
  2. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  3. # For details: https://github.com/PyCQA/pylint/blob/master/COPYING
  4. """Checker for deprecated builtins."""
  5. import sys
  6. import astroid
  7. from pylint.checkers import BaseChecker
  8. from pylint.checkers.utils import check_messages
  9. from pylint.interfaces import IAstroidChecker
  10. BAD_FUNCTIONS = ['map', 'filter']
  11. if sys.version_info < (3, 0):
  12. BAD_FUNCTIONS.append('input')
  13. # Some hints regarding the use of bad builtins.
  14. BUILTIN_HINTS = {
  15. 'map': 'Using a list comprehension can be clearer.',
  16. }
  17. BUILTIN_HINTS['filter'] = BUILTIN_HINTS['map']
  18. class BadBuiltinChecker(BaseChecker):
  19. __implements__ = (IAstroidChecker, )
  20. name = 'deprecated_builtins'
  21. msgs = {'W0141': ('Used builtin function %s',
  22. 'bad-builtin',
  23. 'Used when a black listed builtin function is used (see the '
  24. 'bad-function option). Usual black listed functions are the ones '
  25. 'like map, or filter , where Python offers now some cleaner '
  26. 'alternative like list comprehension.'),
  27. }
  28. options = (('bad-functions',
  29. {'default' : BAD_FUNCTIONS,
  30. 'type' :'csv', 'metavar' : '<builtin function names>',
  31. 'help' : 'List of builtins function names that should not be '
  32. 'used, separated by a comma'}
  33. ),
  34. )
  35. @check_messages('bad-builtin')
  36. def visit_call(self, node):
  37. if isinstance(node.func, astroid.Name):
  38. name = node.func.name
  39. # ignore the name if it's not a builtin (i.e. not defined in the
  40. # locals nor globals scope)
  41. if not (name in node.frame() or name in node.root()):
  42. if name in self.config.bad_functions:
  43. hint = BUILTIN_HINTS.get(name)
  44. if hint:
  45. args = "%r. %s" % (name, hint)
  46. else:
  47. args = repr(name)
  48. self.add_message('bad-builtin', node=node, args=args)
  49. def register(linter):
  50. """Required method to auto register this checker.
  51. :param linter: Main interface object for Pylint plugins
  52. :type linter: Pylint object
  53. """
  54. linter.register_checker(BadBuiltinChecker(linter))