summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Miller <725mrm@gmail.com>2018-05-17 09:55:30 -0700
committerClaudiu Popa <pcmanticore@gmail.com>2018-05-17 12:55:30 -0400
commit7b3bb28294a0b756d9846048ae9b2fcd937111e9 (patch)
treeb2291ffe84ee27226e635204b6691de81698d055
parent49c6da5e7c90f7c53162fff1d8d9a2b6474ee28a (diff)
downloadpylint-git-7b3bb28294a0b756d9846048ae9b2fcd937111e9.tar.gz
Fix handling of inline scopes in too-many-statements (#1979)
Nested scopes don't reset reset the `too-many-statements` check state. Close #1978
-rw-r--r--CONTRIBUTORS.txt2
-rw-r--r--ChangeLog4
-rw-r--r--pylint/checkers/design_analysis.py24
-rw-r--r--pylint/test/functional/too_many_statements.py65
-rw-r--r--pylint/test/functional/too_many_statements.txt1
5 files changed, 87 insertions, 9 deletions
diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt
index 4b3443a02..2c685c391 100644
--- a/CONTRIBUTORS.txt
+++ b/CONTRIBUTORS.txt
@@ -188,6 +188,8 @@ Order doesn't matter (not that much, at least ;)
Added new check `logging-fstring-interpolation`
Documentation typo fixes
+* Mark Roman Miller: fix inline defs in too-many-statements
+
* Adam Dangoor: contributor
* Gary Tyler McLeod: contributor
diff --git a/ChangeLog b/ChangeLog
index f54ef2f8c..6c413b033 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -227,6 +227,10 @@ Release date: |TBA|
Close #638
* Updated the default report format to include paths that can be clicked on in some terminals (e.g. iTerm).
+
+ * Fix inline def behavior with ``too-many-statements`` checker
+
+ Close #1978
* Fix `KeyError` raised when using docparams and NotImplementedError is documented.
diff --git a/pylint/checkers/design_analysis.py b/pylint/checkers/design_analysis.py
index cd5a8d631..94482b67a 100644
--- a/pylint/checkers/design_analysis.py
+++ b/pylint/checkers/design_analysis.py
@@ -171,13 +171,18 @@ class MisdesignChecker(BaseChecker):
self.stats = None
self._returns = None
self._branches = None
- self._stmts = 0
+ self._stmts = None
def open(self):
"""initialize visit variables"""
self.stats = self.linter.add_stats()
self._returns = []
self._branches = defaultdict(int)
+ self._stmts = []
+
+ def _inc_all_stmts(self, amount):
+ for i in range(len(self._stmts)):
+ self._stmts[i] += amount
@decorators.cachedproperty
def _ignored_argument_names(self):
@@ -260,8 +265,8 @@ class MisdesignChecker(BaseChecker):
if locnum > self.config.max_locals:
self.add_message('too-many-locals', node=node,
args=(locnum, self.config.max_locals))
- # init statements counter
- self._stmts = 1
+ # init new statements counter
+ self._stmts.append(1)
visit_asyncfunctiondef = visit_functiondef
@@ -281,9 +286,10 @@ class MisdesignChecker(BaseChecker):
self.add_message('too-many-branches', node=node,
args=(branches, self.config.max_branches))
# check number of statements
- if self._stmts > self.config.max_statements:
+ stmts = self._stmts.pop()
+ if stmts > self.config.max_statements:
self.add_message('too-many-statements', node=node,
- args=(self._stmts, self.config.max_statements))
+ args=(stmts, self.config.max_statements))
leave_asyncfunctiondef = leave_functiondef
@@ -298,7 +304,7 @@ class MisdesignChecker(BaseChecker):
necessary
"""
if node.is_statement:
- self._stmts += 1
+ self._inc_all_stmts(1)
def visit_tryexcept(self, node):
"""increments the branches counter"""
@@ -306,12 +312,12 @@ class MisdesignChecker(BaseChecker):
if node.orelse:
branches += 1
self._inc_branch(node, branches)
- self._stmts += branches
+ self._inc_all_stmts(branches)
def visit_tryfinally(self, node):
"""increments the branches counter"""
self._inc_branch(node, 2)
- self._stmts += 2
+ self._inc_all_stmts(2)
@check_messages('too-many-boolean-expressions')
def visit_if(self, node):
@@ -323,7 +329,7 @@ class MisdesignChecker(BaseChecker):
not isinstance(node.orelse[0], If)):
branches += 1
self._inc_branch(node, branches)
- self._stmts += branches
+ self._inc_all_stmts(branches)
def _check_boolean_expressions(self, node):
"""Go through "if" node `node` and counts its boolean expressions
diff --git a/pylint/test/functional/too_many_statements.py b/pylint/test/functional/too_many_statements.py
index 305db8071..b3e2fd796 100644
--- a/pylint/test/functional/too_many_statements.py
+++ b/pylint/test/functional/too_many_statements.py
@@ -58,3 +58,68 @@ def stupid_function(arg): # [too-many-statements]
print(1)
print(1)
print(1)
+
+def long_function_with_inline_def(fake): # [too-many-statements]
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ def an_inline_function(var):
+ return var + var
+ fake = an_inline_function(fake)
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
+ fake += 1
diff --git a/pylint/test/functional/too_many_statements.txt b/pylint/test/functional/too_many_statements.txt
index 255e97ee1..9a8abfc36 100644
--- a/pylint/test/functional/too_many_statements.txt
+++ b/pylint/test/functional/too_many_statements.txt
@@ -1 +1,2 @@
too-many-statements:5:stupid_function:Too many statements (55/50)
+too-many-statements:62:long_function_with_inline_def:Too many statements (62/50)