pep8ext_naming.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. # -*- coding: utf-8 -*-
  2. """Checker of PEP-8 Naming Conventions."""
  3. import sys
  4. from collections import deque
  5. from fnmatch import fnmatch
  6. from functools import partial
  7. from itertools import chain
  8. from flake8_polyfill import options
  9. try:
  10. import ast
  11. from ast import iter_child_nodes
  12. except ImportError:
  13. from flake8.util import ast, iter_child_nodes
  14. __version__ = '0.10.0'
  15. PYTHON_VERSION = sys.version_info[:3]
  16. PY2 = PYTHON_VERSION[0] == 2
  17. METACLASS_BASES = frozenset(('type', 'ABCMeta'))
  18. # Node types which may contain class methods
  19. METHOD_CONTAINER_NODES = {ast.If, ast.While, ast.For, ast.With}
  20. FUNC_NODES = (ast.FunctionDef,)
  21. if PY2:
  22. METHOD_CONTAINER_NODES |= {ast.TryExcept, ast.TryFinally}
  23. else:
  24. METHOD_CONTAINER_NODES |= {ast.Try}
  25. if PYTHON_VERSION > (3, 5):
  26. FUNC_NODES += (ast.AsyncFunctionDef,)
  27. METHOD_CONTAINER_NODES |= {ast.AsyncWith, ast.AsyncFor}
  28. if PY2:
  29. def _unpack_args(args):
  30. ret = []
  31. for arg in args:
  32. if isinstance(arg, ast.Tuple):
  33. ret.extend(_unpack_args(arg.elts))
  34. else:
  35. ret.append((arg, arg.id))
  36. return ret
  37. def get_arg_name_tuples(node):
  38. return _unpack_args(node.args.args)
  39. elif PYTHON_VERSION < (3, 8):
  40. def get_arg_name_tuples(node):
  41. groups = (node.args.args, node.args.kwonlyargs)
  42. return [(arg, arg.arg) for args in groups for arg in args]
  43. else:
  44. def get_arg_name_tuples(node):
  45. groups = (node.args.posonlyargs, node.args.args, node.args.kwonlyargs)
  46. return [(arg, arg.arg) for args in groups for arg in args]
  47. class _ASTCheckMeta(type):
  48. def __init__(cls, class_name, bases, namespace):
  49. try:
  50. cls._checks.append(cls())
  51. except AttributeError:
  52. cls._checks = []
  53. def _err(self, node, code, **kwargs):
  54. lineno, col_offset = node.lineno, node.col_offset
  55. if isinstance(node, ast.ClassDef):
  56. if PYTHON_VERSION < (3, 8):
  57. lineno += len(node.decorator_list)
  58. col_offset += 6
  59. elif isinstance(node, FUNC_NODES):
  60. if PYTHON_VERSION < (3, 8):
  61. lineno += len(node.decorator_list)
  62. col_offset += 4
  63. code_str = getattr(self, code)
  64. if kwargs:
  65. code_str = code_str.format(**kwargs)
  66. return lineno, col_offset + 1, '%s %s' % (code, code_str), self
  67. def _ignored(name, ignore):
  68. return any(fnmatch(name, i) for i in ignore)
  69. BaseASTCheck = _ASTCheckMeta('BaseASTCheck', (object,),
  70. {'__doc__': "Base for AST Checks.", 'err': _err})
  71. class _FunctionType(object):
  72. CLASSMETHOD = 'classmethod'
  73. STATICMETHOD = 'staticmethod'
  74. FUNCTION = 'function'
  75. METHOD = 'method'
  76. _default_ignore_names = [
  77. 'setUp',
  78. 'tearDown',
  79. 'setUpClass',
  80. 'tearDownClass',
  81. 'setUpTestData',
  82. 'failureException',
  83. 'longMessage',
  84. 'maxDiff']
  85. _default_classmethod_decorators = ['classmethod']
  86. _default_staticmethod_decorators = ['staticmethod']
  87. def _build_decorator_to_type(classmethod_decorators, staticmethod_decorators):
  88. decorator_to_type = {}
  89. for decorator in classmethod_decorators:
  90. decorator_to_type[decorator] = _FunctionType.CLASSMETHOD
  91. for decorator in staticmethod_decorators:
  92. decorator_to_type[decorator] = _FunctionType.STATICMETHOD
  93. return decorator_to_type
  94. class NamingChecker(object):
  95. """Checker of PEP-8 Naming Conventions."""
  96. name = 'naming'
  97. version = __version__
  98. decorator_to_type = _build_decorator_to_type(
  99. _default_classmethod_decorators, _default_staticmethod_decorators)
  100. ignore_names = frozenset(_default_ignore_names)
  101. def __init__(self, tree, filename):
  102. self.visitors = BaseASTCheck._checks
  103. self.parents = deque()
  104. self._node = tree
  105. @classmethod
  106. def add_options(cls, parser):
  107. options.register(parser, '--ignore-names',
  108. default=_default_ignore_names,
  109. action='store',
  110. type='string',
  111. parse_from_config=True,
  112. comma_separated_list=True,
  113. help='List of names or glob patterns the pep8-naming '
  114. 'plugin should ignore. (Defaults to %default)')
  115. options.register(parser, '--classmethod-decorators',
  116. default=_default_classmethod_decorators,
  117. action='store',
  118. type='string',
  119. parse_from_config=True,
  120. comma_separated_list=True,
  121. help='List of method decorators pep8-naming plugin '
  122. 'should consider classmethods (Defaults to '
  123. '%default)')
  124. options.register(parser, '--staticmethod-decorators',
  125. default=_default_staticmethod_decorators,
  126. action='store',
  127. type='string',
  128. parse_from_config=True,
  129. comma_separated_list=True,
  130. help='List of method decorators pep8-naming plugin '
  131. 'should consider staticmethods (Defaults to '
  132. '%default)')
  133. @classmethod
  134. def parse_options(cls, options):
  135. cls.ignore_names = frozenset(options.ignore_names)
  136. cls.decorator_to_type = _build_decorator_to_type(
  137. options.classmethod_decorators,
  138. options.staticmethod_decorators)
  139. def run(self):
  140. return self.visit_tree(self._node) if self._node else ()
  141. def visit_tree(self, node):
  142. for error in self.visit_node(node):
  143. yield error
  144. self.parents.append(node)
  145. for child in iter_child_nodes(node):
  146. for error in self.visit_tree(child):
  147. yield error
  148. self.parents.pop()
  149. def visit_node(self, node):
  150. if isinstance(node, ast.ClassDef):
  151. self.tag_class_functions(node)
  152. elif isinstance(node, FUNC_NODES):
  153. self.find_global_defs(node)
  154. method = 'visit_' + node.__class__.__name__.lower()
  155. parents = self.parents
  156. ignore_names = self.ignore_names
  157. for visitor in self.visitors:
  158. visitor_method = getattr(visitor, method, None)
  159. if visitor_method is None:
  160. continue
  161. for error in visitor_method(node, parents, ignore_names):
  162. yield error
  163. def tag_class_functions(self, cls_node):
  164. """Tag functions if they are methods, classmethods, staticmethods"""
  165. # tries to find all 'old style decorators' like
  166. # m = staticmethod(m)
  167. late_decoration = {}
  168. for node in iter_child_nodes(cls_node):
  169. if not (isinstance(node, ast.Assign) and
  170. isinstance(node.value, ast.Call) and
  171. isinstance(node.value.func, ast.Name)):
  172. continue
  173. func_name = node.value.func.id
  174. if func_name not in self.decorator_to_type:
  175. continue
  176. meth = (len(node.value.args) == 1 and node.value.args[0])
  177. if isinstance(meth, ast.Name):
  178. late_decoration[meth.id] = self.decorator_to_type[func_name]
  179. # If this class inherits from a known metaclass base class, it is
  180. # itself a metaclass, and we'll consider all of it's methods to be
  181. # classmethods.
  182. bases = chain(
  183. (b.id for b in cls_node.bases if isinstance(b, ast.Name)),
  184. (b.attr for b in cls_node.bases if isinstance(b, ast.Attribute)),
  185. )
  186. ismetaclass = any(name for name in bases if name in METACLASS_BASES)
  187. self.set_function_nodes_types(
  188. iter_child_nodes(cls_node), ismetaclass, late_decoration)
  189. def set_function_nodes_types(self, nodes, ismetaclass, late_decoration):
  190. # iterate over all functions and tag them
  191. for node in nodes:
  192. if type(node) in METHOD_CONTAINER_NODES:
  193. self.set_function_nodes_types(
  194. iter_child_nodes(node), ismetaclass, late_decoration)
  195. if not isinstance(node, FUNC_NODES):
  196. continue
  197. node.function_type = _FunctionType.METHOD
  198. if node.name in ('__new__', '__init_subclass__') or ismetaclass:
  199. node.function_type = _FunctionType.CLASSMETHOD
  200. if node.name in late_decoration:
  201. node.function_type = late_decoration[node.name]
  202. elif node.decorator_list:
  203. names = [self.decorator_to_type[d.id]
  204. for d in node.decorator_list
  205. if isinstance(d, ast.Name) and
  206. d.id in self.decorator_to_type]
  207. if names:
  208. node.function_type = names[0]
  209. @staticmethod
  210. def find_global_defs(func_def_node):
  211. global_names = set()
  212. nodes_to_check = deque(iter_child_nodes(func_def_node))
  213. while nodes_to_check:
  214. node = nodes_to_check.pop()
  215. if isinstance(node, ast.Global):
  216. global_names.update(node.names)
  217. if not isinstance(node, (ast.ClassDef,) + FUNC_NODES):
  218. nodes_to_check.extend(iter_child_nodes(node))
  219. func_def_node.global_names = global_names
  220. class ClassNameCheck(BaseASTCheck):
  221. """
  222. Almost without exception, class names use the CapWords convention.
  223. Classes for internal use have a leading underscore in addition.
  224. """
  225. N801 = "class name '{name}' should use CapWords convention"
  226. def visit_classdef(self, node, parents, ignore=None):
  227. name = node.name
  228. if _ignored(name, ignore):
  229. return
  230. name = name.strip('_')
  231. if not name[:1].isupper() or '_' in name:
  232. yield self.err(node, 'N801', name=name)
  233. class FunctionNameCheck(BaseASTCheck):
  234. """
  235. Function names should be lowercase, with words separated by underscores
  236. as necessary to improve readability.
  237. Functions *not* being methods '__' in front and back are not allowed.
  238. mixedCase is allowed only in contexts where that's already the
  239. prevailing style (e.g. threading.py), to retain backwards compatibility.
  240. """
  241. N802 = "function name '{name}' should be lowercase"
  242. N807 = "function name '{name}' should not start and end with '__'"
  243. def visit_functiondef(self, node, parents, ignore=None):
  244. function_type = getattr(node, 'function_type', _FunctionType.FUNCTION)
  245. name = node.name
  246. if _ignored(name, ignore):
  247. return
  248. if name in ('__dir__', '__getattr__'):
  249. return
  250. if name.lower() != name:
  251. yield self.err(node, 'N802', name=name)
  252. if (function_type == _FunctionType.FUNCTION
  253. and name[:2] == '__' and name[-2:] == '__'):
  254. yield self.err(node, 'N807', name=name)
  255. visit_asyncfunctiondef = visit_functiondef
  256. class FunctionArgNamesCheck(BaseASTCheck):
  257. """
  258. The argument names of a function should be lowercase, with words separated
  259. by underscores.
  260. A classmethod should have 'cls' as first argument.
  261. A method should have 'self' as first argument.
  262. """
  263. N803 = "argument name '{name}' should be lowercase"
  264. N804 = "first argument of a classmethod should be named 'cls'"
  265. N805 = "first argument of a method should be named 'self'"
  266. def visit_functiondef(self, node, parents, ignore=None):
  267. def arg_name(arg):
  268. try:
  269. return arg, arg.arg
  270. except AttributeError: # PY2
  271. return node, arg
  272. for arg, name in arg_name(node.args.vararg), arg_name(node.args.kwarg):
  273. if name is None or _ignored(name, ignore):
  274. continue
  275. if name.lower() != name:
  276. yield self.err(arg, 'N803', name=name)
  277. return
  278. arg_name_tuples = get_arg_name_tuples(node)
  279. if not arg_name_tuples:
  280. return
  281. arg0, name0 = arg_name_tuples[0]
  282. function_type = getattr(node, 'function_type', _FunctionType.FUNCTION)
  283. if function_type == _FunctionType.METHOD:
  284. if name0 != 'self' and not _ignored(name0, ignore):
  285. yield self.err(arg0, 'N805')
  286. elif function_type == _FunctionType.CLASSMETHOD:
  287. if name0 != 'cls' and not _ignored(name0, ignore):
  288. yield self.err(arg0, 'N804')
  289. for arg, name in arg_name_tuples:
  290. if name.lower() != name and not _ignored(name, ignore):
  291. yield self.err(arg, 'N803', name=name)
  292. return
  293. visit_asyncfunctiondef = visit_functiondef
  294. class ImportAsCheck(BaseASTCheck):
  295. """
  296. Don't change the naming convention via an import
  297. """
  298. N811 = "constant '{name}' imported as non constant '{asname}'"
  299. N812 = "lowercase '{name}' imported as non lowercase '{asname}'"
  300. N813 = "camelcase '{name}' imported as lowercase '{asname}'"
  301. N814 = "camelcase '{name}' imported as constant '{asname}'"
  302. N817 = "camelcase '{name}' imported as acronym '{asname}'"
  303. def visit_importfrom(self, node, parents, ignore=None):
  304. for name in node.names:
  305. asname = name.asname
  306. if not asname:
  307. continue
  308. original_name = name.name
  309. err_kwargs = {'name': original_name, 'asname': asname}
  310. if original_name.isupper():
  311. if not asname.isupper():
  312. yield self.err(node, 'N811', **err_kwargs)
  313. elif original_name.islower():
  314. if asname.lower() != asname:
  315. yield self.err(node, 'N812', **err_kwargs)
  316. elif asname.islower():
  317. yield self.err(node, 'N813', **err_kwargs)
  318. elif asname.isupper():
  319. if ''.join(filter(str.isupper, original_name)) == asname:
  320. yield self.err(node, 'N817', **err_kwargs)
  321. else:
  322. yield self.err(node, 'N814', **err_kwargs)
  323. visit_import = visit_importfrom
  324. class VariablesCheck(BaseASTCheck):
  325. """
  326. Class attributes and local variables in functions should be lowercase
  327. """
  328. N806 = "variable '{name}' in function should be lowercase"
  329. N815 = "variable '{name}' in class scope should not be mixedCase"
  330. N816 = "variable '{name}' in global scope should not be mixedCase"
  331. def _find_errors(self, assignment_target, parents, ignore):
  332. for parent_func in reversed(parents):
  333. if isinstance(parent_func, ast.ClassDef):
  334. checker = self.class_variable_check
  335. break
  336. if isinstance(parent_func, FUNC_NODES):
  337. checker = partial(self.function_variable_check, parent_func)
  338. break
  339. else:
  340. checker = self.global_variable_check
  341. for name in _extract_names(assignment_target):
  342. if _ignored(name, ignore):
  343. continue
  344. error_code = checker(name)
  345. if error_code:
  346. yield self.err(assignment_target, error_code, name=name)
  347. @staticmethod
  348. def is_namedtupe(node_value):
  349. if isinstance(node_value, ast.Call):
  350. if isinstance(node_value.func, ast.Attribute):
  351. if node_value.func.attr == 'namedtuple':
  352. return True
  353. elif isinstance(node_value.func, ast.Name):
  354. if node_value.func.id == 'namedtuple':
  355. return True
  356. return False
  357. def visit_assign(self, node, parents, ignore=None):
  358. if self.is_namedtupe(node.value):
  359. return
  360. for target in node.targets:
  361. for error in self._find_errors(target, parents, ignore):
  362. yield error
  363. def visit_namedexpr(self, node, parents, ignore):
  364. if self.is_namedtupe(node.value):
  365. return
  366. for error in self._find_errors(node.target, parents, ignore):
  367. yield error
  368. visit_annassign = visit_namedexpr
  369. def visit_with(self, node, parents, ignore):
  370. if PY2:
  371. for error in self._find_errors(
  372. node.optional_vars, parents, ignore):
  373. yield error
  374. return
  375. for item in node.items:
  376. for error in self._find_errors(
  377. item.optional_vars, parents, ignore):
  378. yield error
  379. visit_asyncwith = visit_with
  380. def visit_for(self, node, parents, ignore):
  381. for error in self._find_errors(node.target, parents, ignore):
  382. yield error
  383. visit_asyncfor = visit_for
  384. def visit_excepthandler(self, node, parents, ignore):
  385. if node.name:
  386. for error in self._find_errors(node, parents, ignore):
  387. yield error
  388. def visit_generatorexp(self, node, parents, ignore):
  389. for gen in node.generators:
  390. for error in self._find_errors(gen.target, parents, ignore):
  391. yield error
  392. visit_listcomp = visit_dictcomp = visit_setcomp = visit_generatorexp
  393. @staticmethod
  394. def global_variable_check(name):
  395. if is_mixed_case(name):
  396. return 'N816'
  397. @staticmethod
  398. def class_variable_check(name):
  399. if is_mixed_case(name):
  400. return 'N815'
  401. @staticmethod
  402. def function_variable_check(func, var_name):
  403. if var_name in func.global_names:
  404. return None
  405. if var_name.lower() == var_name:
  406. return None
  407. return 'N806'
  408. def _extract_names(assignment_target):
  409. """Yield assignment_target ids."""
  410. target_type = type(assignment_target)
  411. if target_type is ast.Name:
  412. yield assignment_target.id
  413. elif target_type in (ast.Tuple, ast.List):
  414. for element in assignment_target.elts:
  415. element_type = type(element)
  416. if element_type is ast.Name:
  417. yield element.id
  418. elif element_type in (ast.Tuple, ast.List):
  419. for n in _extract_names(element):
  420. yield n
  421. elif not PY2 and element_type is ast.Starred: # PEP 3132
  422. for n in _extract_names(element.value):
  423. yield n
  424. elif target_type is ast.ExceptHandler:
  425. if PY2:
  426. # Python 2 supports unpacking tuple exception values.
  427. if isinstance(assignment_target.name, ast.Tuple):
  428. for name in assignment_target.name.elts:
  429. yield name.id
  430. elif isinstance(assignment_target.name, ast.Attribute):
  431. # Python 2 also supports assigning an exception to an attribute
  432. # eg. except Exception as obj.attr
  433. yield assignment_target.name.attr
  434. else:
  435. yield assignment_target.name.id
  436. else:
  437. yield assignment_target.name
  438. def is_mixed_case(name):
  439. return name.lower() != name and name.lstrip('_')[:1].islower()