design_analysis.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. # Copyright (c) 2006, 2009-2010, 2012-2015 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
  2. # Copyright (c) 2012, 2014 Google, Inc.
  3. # Copyright (c) 2014-2020 Claudiu Popa <pcmanticore@gmail.com>
  4. # Copyright (c) 2014 Arun Persaud <arun@nubati.net>
  5. # Copyright (c) 2015 Ionel Cristian Maries <contact@ionelmc.ro>
  6. # Copyright (c) 2016 Łukasz Rogalski <rogalski.91@gmail.com>
  7. # Copyright (c) 2017 ahirnish <ahirnish@gmail.com>
  8. # Copyright (c) 2018 Lucas Cimon <lucas.cimon@gmail.com>
  9. # Copyright (c) 2018 Mike Frysinger <vapier@gmail.com>
  10. # Copyright (c) 2018 Mark Miller <725mrm@gmail.com>
  11. # Copyright (c) 2018 Ashley Whetter <ashley@awhetter.co.uk>
  12. # Copyright (c) 2018 Ville Skyttä <ville.skytta@iki.fi>
  13. # Copyright (c) 2018 Jakub Wilk <jwilk@jwilk.net>
  14. # Copyright (c) 2019-2021 Pierre Sassoulas <pierre.sassoulas@gmail.com>
  15. # Copyright (c) 2019 Michael Scott Cuthbert <cuthbert@mit.edu>
  16. # Copyright (c) 2020 hippo91 <guillaume.peillex@gmail.com>
  17. # Copyright (c) 2020 Anthony Sottile <asottile@umich.edu>
  18. # Copyright (c) 2021 Mike Fiedler <miketheman@gmail.com>
  19. # Copyright (c) 2021 Youngsoo Sung <ysung@bepro11.com>
  20. # Copyright (c) 2021 Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
  21. # Copyright (c) 2021 bot <bot@noreply.github.com>
  22. # Copyright (c) 2021 Andrew Haigh <hello@nelf.in>
  23. # Copyright (c) 2021 Melvin <31448155+melvio@users.noreply.github.com>
  24. # Copyright (c) 2021 Rebecca Turner <rbt@sent.as>
  25. # Copyright (c) 2021 Marc Mueller <30130371+cdce8p@users.noreply.github.com>
  26. # Copyright (c) 2021 Yu Shao, Pang <36848472+yushao2@users.noreply.github.com>
  27. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  28. # For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
  29. """check for signs of poor design"""
  30. import re
  31. from collections import defaultdict
  32. from typing import FrozenSet, Iterator, List, Set, cast
  33. import astroid
  34. from astroid import nodes
  35. from pylint import utils
  36. from pylint.checkers import BaseChecker
  37. from pylint.checkers.utils import check_messages
  38. from pylint.interfaces import IAstroidChecker
  39. MSGS = { # pylint: disable=consider-using-namedtuple-or-dataclass
  40. "R0901": (
  41. "Too many ancestors (%s/%s)",
  42. "too-many-ancestors",
  43. "Used when class has too many parent classes, try to reduce "
  44. "this to get a simpler (and so easier to use) class.",
  45. ),
  46. "R0902": (
  47. "Too many instance attributes (%s/%s)",
  48. "too-many-instance-attributes",
  49. "Used when class has too many instance attributes, try to reduce "
  50. "this to get a simpler (and so easier to use) class.",
  51. ),
  52. "R0903": (
  53. "Too few public methods (%s/%s)",
  54. "too-few-public-methods",
  55. "Used when class has too few public methods, so be sure it's "
  56. "really worth it.",
  57. ),
  58. "R0904": (
  59. "Too many public methods (%s/%s)",
  60. "too-many-public-methods",
  61. "Used when class has too many public methods, try to reduce "
  62. "this to get a simpler (and so easier to use) class.",
  63. ),
  64. "R0911": (
  65. "Too many return statements (%s/%s)",
  66. "too-many-return-statements",
  67. "Used when a function or method has too many return statement, "
  68. "making it hard to follow.",
  69. ),
  70. "R0912": (
  71. "Too many branches (%s/%s)",
  72. "too-many-branches",
  73. "Used when a function or method has too many branches, "
  74. "making it hard to follow.",
  75. ),
  76. "R0913": (
  77. "Too many arguments (%s/%s)",
  78. "too-many-arguments",
  79. "Used when a function or method takes too many arguments.",
  80. ),
  81. "R0914": (
  82. "Too many local variables (%s/%s)",
  83. "too-many-locals",
  84. "Used when a function or method has too many local variables.",
  85. ),
  86. "R0915": (
  87. "Too many statements (%s/%s)",
  88. "too-many-statements",
  89. "Used when a function or method has too many statements. You "
  90. "should then split it in smaller functions / methods.",
  91. ),
  92. "R0916": (
  93. "Too many boolean expressions in if statement (%s/%s)",
  94. "too-many-boolean-expressions",
  95. "Used when an if statement contains too many boolean expressions.",
  96. ),
  97. }
  98. SPECIAL_OBJ = re.compile("^_{2}[a-z]+_{2}$")
  99. DATACLASSES_DECORATORS = frozenset({"dataclass", "attrs"})
  100. DATACLASS_IMPORT = "dataclasses"
  101. TYPING_NAMEDTUPLE = "typing.NamedTuple"
  102. TYPING_TYPEDDICT = "typing.TypedDict"
  103. # Set of stdlib classes to ignore when calculating number of ancestors
  104. STDLIB_CLASSES_IGNORE_ANCESTOR = frozenset(
  105. (
  106. "builtins.object",
  107. "builtins.tuple",
  108. "builtins.dict",
  109. "builtins.list",
  110. "builtins.set",
  111. "bulitins.frozenset",
  112. "collections.ChainMap",
  113. "collections.Counter",
  114. "collections.OrderedDict",
  115. "collections.UserDict",
  116. "collections.UserList",
  117. "collections.UserString",
  118. "collections.defaultdict",
  119. "collections.deque",
  120. "collections.namedtuple",
  121. "_collections_abc.Awaitable",
  122. "_collections_abc.Coroutine",
  123. "_collections_abc.AsyncIterable",
  124. "_collections_abc.AsyncIterator",
  125. "_collections_abc.AsyncGenerator",
  126. "_collections_abc.Hashable",
  127. "_collections_abc.Iterable",
  128. "_collections_abc.Iterator",
  129. "_collections_abc.Generator",
  130. "_collections_abc.Reversible",
  131. "_collections_abc.Sized",
  132. "_collections_abc.Container",
  133. "_collections_abc.Collection",
  134. "_collections_abc.Set",
  135. "_collections_abc.MutableSet",
  136. "_collections_abc.Mapping",
  137. "_collections_abc.MutableMapping",
  138. "_collections_abc.MappingView",
  139. "_collections_abc.KeysView",
  140. "_collections_abc.ItemsView",
  141. "_collections_abc.ValuesView",
  142. "_collections_abc.Sequence",
  143. "_collections_abc.MutableSequence",
  144. "_collections_abc.ByteString",
  145. "typing.Tuple",
  146. "typing.List",
  147. "typing.Dict",
  148. "typing.Set",
  149. "typing.FrozenSet",
  150. "typing.Deque",
  151. "typing.DefaultDict",
  152. "typing.OrderedDict",
  153. "typing.Counter",
  154. "typing.ChainMap",
  155. "typing.Awaitable",
  156. "typing.Coroutine",
  157. "typing.AsyncIterable",
  158. "typing.AsyncIterator",
  159. "typing.AsyncGenerator",
  160. "typing.Iterable",
  161. "typing.Iterator",
  162. "typing.Generator",
  163. "typing.Reversible",
  164. "typing.Container",
  165. "typing.Collection",
  166. "typing.AbstractSet",
  167. "typing.MutableSet",
  168. "typing.Mapping",
  169. "typing.MutableMapping",
  170. "typing.Sequence",
  171. "typing.MutableSequence",
  172. "typing.ByteString",
  173. "typing.MappingView",
  174. "typing.KeysView",
  175. "typing.ItemsView",
  176. "typing.ValuesView",
  177. "typing.ContextManager",
  178. "typing.AsyncContextManager",
  179. "typing.Hashable",
  180. "typing.Sized",
  181. )
  182. )
  183. def _is_exempt_from_public_methods(node: astroid.ClassDef) -> bool:
  184. """Check if a class is exempt from too-few-public-methods"""
  185. # If it's a typing.Namedtuple, typing.TypedDict or an Enum
  186. for ancestor in node.ancestors():
  187. if ancestor.name == "Enum" and ancestor.root().name == "enum":
  188. return True
  189. if ancestor.qname() in (TYPING_NAMEDTUPLE, TYPING_TYPEDDICT):
  190. return True
  191. # Or if it's a dataclass
  192. if not node.decorators:
  193. return False
  194. root_locals = set(node.root().locals)
  195. for decorator in node.decorators.nodes:
  196. if isinstance(decorator, astroid.Call):
  197. decorator = decorator.func
  198. if not isinstance(decorator, (astroid.Name, astroid.Attribute)):
  199. continue
  200. if isinstance(decorator, astroid.Name):
  201. name = decorator.name
  202. else:
  203. name = decorator.attrname
  204. if name in DATACLASSES_DECORATORS and (
  205. root_locals.intersection(DATACLASSES_DECORATORS)
  206. or DATACLASS_IMPORT in root_locals
  207. ):
  208. return True
  209. return False
  210. def _count_boolean_expressions(bool_op):
  211. """Counts the number of boolean expressions in BoolOp `bool_op` (recursive)
  212. example: a and (b or c or (d and e)) ==> 5 boolean expressions
  213. """
  214. nb_bool_expr = 0
  215. for bool_expr in bool_op.get_children():
  216. if isinstance(bool_expr, astroid.BoolOp):
  217. nb_bool_expr += _count_boolean_expressions(bool_expr)
  218. else:
  219. nb_bool_expr += 1
  220. return nb_bool_expr
  221. def _count_methods_in_class(node):
  222. all_methods = sum(1 for method in node.methods() if not method.name.startswith("_"))
  223. # Special methods count towards the number of public methods,
  224. # but don't count towards there being too many methods.
  225. for method in node.mymethods():
  226. if SPECIAL_OBJ.search(method.name) and method.name != "__init__":
  227. all_methods += 1
  228. return all_methods
  229. def _get_parents_iter(
  230. node: nodes.ClassDef, ignored_parents: FrozenSet[str]
  231. ) -> Iterator[nodes.ClassDef]:
  232. r"""Get parents of ``node``, excluding ancestors of ``ignored_parents``.
  233. If we have the following inheritance diagram:
  234. F
  235. /
  236. D E
  237. \/
  238. B C
  239. \/
  240. A # class A(B, C): ...
  241. And ``ignored_parents`` is ``{"E"}``, then this function will return
  242. ``{A, B, C, D}`` -- both ``E`` and its ancestors are excluded.
  243. """
  244. parents: Set[nodes.ClassDef] = set()
  245. to_explore = cast(List[nodes.ClassDef], list(node.ancestors(recurs=False)))
  246. while to_explore:
  247. parent = to_explore.pop()
  248. if parent.qname() in ignored_parents:
  249. continue
  250. if parent not in parents:
  251. # This guard might appear to be performing the same function as
  252. # adding the resolved parents to a set to eliminate duplicates
  253. # (legitimate due to diamond inheritance patterns), but its
  254. # additional purpose is to prevent cycles (not normally possible,
  255. # but potential due to inference) and thus guarantee termination
  256. # of the while-loop
  257. yield parent
  258. parents.add(parent)
  259. to_explore.extend(parent.ancestors(recurs=False))
  260. def _get_parents(
  261. node: nodes.ClassDef, ignored_parents: FrozenSet[str]
  262. ) -> Set[nodes.ClassDef]:
  263. return set(_get_parents_iter(node, ignored_parents))
  264. class MisdesignChecker(BaseChecker):
  265. """checks for sign of poor/misdesign:
  266. * number of methods, attributes, local variables...
  267. * size, complexity of functions, methods
  268. """
  269. __implements__ = (IAstroidChecker,)
  270. # configuration section name
  271. name = "design"
  272. # messages
  273. msgs = MSGS
  274. priority = -2
  275. # configuration options
  276. options = (
  277. (
  278. "max-args",
  279. {
  280. "default": 5,
  281. "type": "int",
  282. "metavar": "<int>",
  283. "help": "Maximum number of arguments for function / method.",
  284. },
  285. ),
  286. (
  287. "max-locals",
  288. {
  289. "default": 15,
  290. "type": "int",
  291. "metavar": "<int>",
  292. "help": "Maximum number of locals for function / method body.",
  293. },
  294. ),
  295. (
  296. "max-returns",
  297. {
  298. "default": 6,
  299. "type": "int",
  300. "metavar": "<int>",
  301. "help": "Maximum number of return / yield for function / "
  302. "method body.",
  303. },
  304. ),
  305. (
  306. "max-branches",
  307. {
  308. "default": 12,
  309. "type": "int",
  310. "metavar": "<int>",
  311. "help": "Maximum number of branch for function / method body.",
  312. },
  313. ),
  314. (
  315. "max-statements",
  316. {
  317. "default": 50,
  318. "type": "int",
  319. "metavar": "<int>",
  320. "help": "Maximum number of statements in function / method body.",
  321. },
  322. ),
  323. (
  324. "max-parents",
  325. {
  326. "default": 7,
  327. "type": "int",
  328. "metavar": "<num>",
  329. "help": "Maximum number of parents for a class (see R0901).",
  330. },
  331. ),
  332. (
  333. "ignored-parents",
  334. {
  335. "default": (),
  336. "type": "csv",
  337. "metavar": "<comma separated list of class names>",
  338. "help": "List of qualified class names to ignore when counting class parents (see R0901)",
  339. },
  340. ),
  341. (
  342. "max-attributes",
  343. {
  344. "default": 7,
  345. "type": "int",
  346. "metavar": "<num>",
  347. "help": "Maximum number of attributes for a class \
  348. (see R0902).",
  349. },
  350. ),
  351. (
  352. "min-public-methods",
  353. {
  354. "default": 2,
  355. "type": "int",
  356. "metavar": "<num>",
  357. "help": "Minimum number of public methods for a class \
  358. (see R0903).",
  359. },
  360. ),
  361. (
  362. "max-public-methods",
  363. {
  364. "default": 20,
  365. "type": "int",
  366. "metavar": "<num>",
  367. "help": "Maximum number of public methods for a class \
  368. (see R0904).",
  369. },
  370. ),
  371. (
  372. "max-bool-expr",
  373. {
  374. "default": 5,
  375. "type": "int",
  376. "metavar": "<num>",
  377. "help": "Maximum number of boolean expressions in an if "
  378. "statement (see R0916).",
  379. },
  380. ),
  381. (
  382. "exclude-too-few-public-methods",
  383. {
  384. "default": [],
  385. "type": "regexp_csv",
  386. "metavar": "<pattern>[,<pattern>...]",
  387. "help": "List of regular expressions of class ancestor names "
  388. "to ignore when counting public methods (see R0903)",
  389. },
  390. ),
  391. )
  392. def __init__(self, linter=None):
  393. super().__init__(linter)
  394. self._returns = None
  395. self._branches = None
  396. self._stmts = None
  397. def open(self):
  398. """initialize visit variables"""
  399. self.linter.stats.reset_node_count()
  400. self._returns = []
  401. self._branches = defaultdict(int)
  402. self._stmts = []
  403. self._exclude_too_few_public_methods = utils.get_global_option(
  404. self, "exclude-too-few-public-methods", default=[]
  405. )
  406. def _inc_all_stmts(self, amount):
  407. for i, _ in enumerate(self._stmts):
  408. self._stmts[i] += amount
  409. @astroid.decorators.cachedproperty
  410. def _ignored_argument_names(self):
  411. return utils.get_global_option(self, "ignored-argument-names", default=None)
  412. @check_messages(
  413. "too-many-ancestors",
  414. "too-many-instance-attributes",
  415. "too-few-public-methods",
  416. "too-many-public-methods",
  417. )
  418. def visit_classdef(self, node: nodes.ClassDef) -> None:
  419. """check size of inheritance hierarchy and number of instance attributes"""
  420. parents = _get_parents(
  421. node, STDLIB_CLASSES_IGNORE_ANCESTOR.union(self.config.ignored_parents)
  422. )
  423. nb_parents = len(parents)
  424. if nb_parents > self.config.max_parents:
  425. self.add_message(
  426. "too-many-ancestors",
  427. node=node,
  428. args=(nb_parents, self.config.max_parents),
  429. )
  430. if len(node.instance_attrs) > self.config.max_attributes:
  431. self.add_message(
  432. "too-many-instance-attributes",
  433. node=node,
  434. args=(len(node.instance_attrs), self.config.max_attributes),
  435. )
  436. @check_messages("too-few-public-methods", "too-many-public-methods")
  437. def leave_classdef(self, node: nodes.ClassDef) -> None:
  438. """check number of public methods"""
  439. my_methods = sum(
  440. 1 for method in node.mymethods() if not method.name.startswith("_")
  441. )
  442. # Does the class contain less than n public methods ?
  443. # This checks only the methods defined in the current class,
  444. # since the user might not have control over the classes
  445. # from the ancestors. It avoids some false positives
  446. # for classes such as unittest.TestCase, which provides
  447. # a lot of assert methods. It doesn't make sense to warn
  448. # when the user subclasses TestCase to add his own tests.
  449. if my_methods > self.config.max_public_methods:
  450. self.add_message(
  451. "too-many-public-methods",
  452. node=node,
  453. args=(my_methods, self.config.max_public_methods),
  454. )
  455. # Stop here if the class is excluded via configuration.
  456. if node.type == "class" and self._exclude_too_few_public_methods:
  457. for ancestor in node.ancestors():
  458. if any(
  459. pattern.match(ancestor.qname())
  460. for pattern in self._exclude_too_few_public_methods
  461. ):
  462. return
  463. # Stop here for exception, metaclass, interface classes and other
  464. # classes for which we don't need to count the methods.
  465. if node.type != "class" or _is_exempt_from_public_methods(node):
  466. return
  467. # Does the class contain more than n public methods ?
  468. # This checks all the methods defined by ancestors and
  469. # by the current class.
  470. all_methods = _count_methods_in_class(node)
  471. if all_methods < self.config.min_public_methods:
  472. self.add_message(
  473. "too-few-public-methods",
  474. node=node,
  475. args=(all_methods, self.config.min_public_methods),
  476. )
  477. @check_messages(
  478. "too-many-return-statements",
  479. "too-many-branches",
  480. "too-many-arguments",
  481. "too-many-locals",
  482. "too-many-statements",
  483. "keyword-arg-before-vararg",
  484. )
  485. def visit_functiondef(self, node: nodes.FunctionDef) -> None:
  486. """check function name, docstring, arguments, redefinition,
  487. variable names, max locals
  488. """
  489. # init branch and returns counters
  490. self._returns.append(0)
  491. # check number of arguments
  492. args = node.args.args
  493. ignored_argument_names = self._ignored_argument_names
  494. if args is not None:
  495. ignored_args_num = 0
  496. if ignored_argument_names:
  497. ignored_args_num = sum(
  498. 1 for arg in args if ignored_argument_names.match(arg.name)
  499. )
  500. argnum = len(args) - ignored_args_num
  501. if argnum > self.config.max_args:
  502. self.add_message(
  503. "too-many-arguments",
  504. node=node,
  505. args=(len(args), self.config.max_args),
  506. )
  507. else:
  508. ignored_args_num = 0
  509. # check number of local variables
  510. locnum = len(node.locals) - ignored_args_num
  511. if locnum > self.config.max_locals:
  512. self.add_message(
  513. "too-many-locals", node=node, args=(locnum, self.config.max_locals)
  514. )
  515. # init new statements counter
  516. self._stmts.append(1)
  517. visit_asyncfunctiondef = visit_functiondef
  518. @check_messages(
  519. "too-many-return-statements",
  520. "too-many-branches",
  521. "too-many-arguments",
  522. "too-many-locals",
  523. "too-many-statements",
  524. )
  525. def leave_functiondef(self, node: nodes.FunctionDef) -> None:
  526. """most of the work is done here on close:
  527. checks for max returns, branch, return in __init__
  528. """
  529. returns = self._returns.pop()
  530. if returns > self.config.max_returns:
  531. self.add_message(
  532. "too-many-return-statements",
  533. node=node,
  534. args=(returns, self.config.max_returns),
  535. )
  536. branches = self._branches[node]
  537. if branches > self.config.max_branches:
  538. self.add_message(
  539. "too-many-branches",
  540. node=node,
  541. args=(branches, self.config.max_branches),
  542. )
  543. # check number of statements
  544. stmts = self._stmts.pop()
  545. if stmts > self.config.max_statements:
  546. self.add_message(
  547. "too-many-statements",
  548. node=node,
  549. args=(stmts, self.config.max_statements),
  550. )
  551. leave_asyncfunctiondef = leave_functiondef
  552. def visit_return(self, _: nodes.Return) -> None:
  553. """count number of returns"""
  554. if not self._returns:
  555. return # return outside function, reported by the base checker
  556. self._returns[-1] += 1
  557. def visit_default(self, node: nodes.NodeNG) -> None:
  558. """default visit method -> increments the statements counter if
  559. necessary
  560. """
  561. if node.is_statement:
  562. self._inc_all_stmts(1)
  563. def visit_tryexcept(self, node: nodes.TryExcept) -> None:
  564. """increments the branches counter"""
  565. branches = len(node.handlers)
  566. if node.orelse:
  567. branches += 1
  568. self._inc_branch(node, branches)
  569. self._inc_all_stmts(branches)
  570. def visit_tryfinally(self, node: nodes.TryFinally) -> None:
  571. """increments the branches counter"""
  572. self._inc_branch(node, 2)
  573. self._inc_all_stmts(2)
  574. @check_messages("too-many-boolean-expressions")
  575. def visit_if(self, node: nodes.If) -> None:
  576. """increments the branches counter and checks boolean expressions"""
  577. self._check_boolean_expressions(node)
  578. branches = 1
  579. # don't double count If nodes coming from some 'elif'
  580. if node.orelse and (
  581. len(node.orelse) > 1 or not isinstance(node.orelse[0], astroid.If)
  582. ):
  583. branches += 1
  584. self._inc_branch(node, branches)
  585. self._inc_all_stmts(branches)
  586. def _check_boolean_expressions(self, node):
  587. """Go through "if" node `node` and counts its boolean expressions
  588. if the "if" node test is a BoolOp node
  589. """
  590. condition = node.test
  591. if not isinstance(condition, astroid.BoolOp):
  592. return
  593. nb_bool_expr = _count_boolean_expressions(condition)
  594. if nb_bool_expr > self.config.max_bool_expr:
  595. self.add_message(
  596. "too-many-boolean-expressions",
  597. node=condition,
  598. args=(nb_bool_expr, self.config.max_bool_expr),
  599. )
  600. def visit_while(self, node: nodes.While) -> None:
  601. """increments the branches counter"""
  602. branches = 1
  603. if node.orelse:
  604. branches += 1
  605. self._inc_branch(node, branches)
  606. visit_for = visit_while
  607. def _inc_branch(self, node, branchesnum=1):
  608. """increments the branches counter"""
  609. self._branches[node.scope()] += branchesnum
  610. def register(linter):
  611. """required method to auto register this checker"""
  612. linter.register_checker(MisdesignChecker(linter))