summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2015-09-04 10:35:47 +0300
committerClaudiu Popa <pcmanticore@gmail.com>2015-09-04 10:35:47 +0300
commita0e6e67af6f05b81fe7575d4a372b2ea4b20fe46 (patch)
treefbba45546b1abbbce84d31477b0497d560134197
parenta4e62fb0938f1fb737d0c4e7e36ec218e00e3972 (diff)
downloadpylint-a0e6e67af6f05b81fe7575d4a372b2ea4b20fe46.tar.gz
Allow a bare raise only inside an except clause.
-rw-r--r--ChangeLog2
-rw-r--r--pylint/checkers/exceptions.py9
-rw-r--r--pylint/test/functional/misplaced_bare_raise.py12
-rw-r--r--pylint/test/functional/misplaced_bare_raise.txt13
4 files changed, 18 insertions, 18 deletions
diff --git a/ChangeLog b/ChangeLog
index 8678b84..e969f62 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -267,7 +267,7 @@ ChangeLog for Pylint
* Add a new error, 'misplaced-bare-raise'.
- The error is used when a bare raise is not used inside a try suite.
+ The error is used when a bare raise is not used inside an except clause.
This can generate a RuntimeError in Python, if there are no active exceptions
to be reraised. While it works in Python 2 due to the fact that the exception
leaks outside of the except block, it's nevertheless a behaviour that
diff --git a/pylint/checkers/exceptions.py b/pylint/checkers/exceptions.py
index 3a11401..7359e96 100644
--- a/pylint/checkers/exceptions.py
+++ b/pylint/checkers/exceptions.py
@@ -79,9 +79,9 @@ MSGS = {
'where the exception context is not an exception, '
'nor None.',
{'minversion': (3, 0)}),
- 'E0704': ('The raise statement is not inside a try suite',
+ 'E0704': ('The raise statement is not inside an except clause',
'misplaced-bare-raise',
- 'Used when a bare raise is not used inside a try suite. '
+ 'Used when a bare raise is not used inside an except clause. '
'This generates an error, since there are no active exceptions '
'to be reraised. An exception to this rule is represented by '
'a bare raise inside a finally clause, which might work, as long '
@@ -180,12 +180,11 @@ class ExceptionsChecker(BaseChecker):
current = node
# Stop when a new scope is generated or when the raise
# statement is found inside a TryFinally.
- ignores = (astroid.ExceptHandler, astroid.TryExcept,
- astroid.FunctionDef, astroid.TryFinally)
+ ignores = (astroid.ExceptHandler, astroid.FunctionDef, astroid.TryFinally)
while current and not isinstance(current.parent, ignores):
current = current.parent
- expected = (astroid.ExceptHandler, astroid.TryExcept)
+ expected = (astroid.ExceptHandler,)
if (not current
or not isinstance(current.parent, expected)):
self.add_message('misplaced-bare-raise', node=node)
diff --git a/pylint/test/functional/misplaced_bare_raise.py b/pylint/test/functional/misplaced_bare_raise.py
index 1defbb4..ad60214 100644
--- a/pylint/test/functional/misplaced_bare_raise.py
+++ b/pylint/test/functional/misplaced_bare_raise.py
@@ -2,7 +2,7 @@
# pylint: disable=unused-variable, too-few-public-methods, invalid-name
try:
- raise
+ raise # [misplaced-bare-raise]
except Exception:
pass
@@ -12,18 +12,18 @@ except Exception:
raise
try:
+ pass
+except Exception:
if 1 == 2:
raise
-except Exception:
- raise
def test():
try:
- raise
+ pass
except Exception:
def chest():
try:
- raise
+ pass
except Exception:
raise
raise
@@ -50,7 +50,7 @@ finally:
class A(object):
try:
- raise
+ pass
except Exception:
raise
raise # [misplaced-bare-raise]
diff --git a/pylint/test/functional/misplaced_bare_raise.txt b/pylint/test/functional/misplaced_bare_raise.txt
index 75fe783..c60f3ca 100644
--- a/pylint/test/functional/misplaced_bare_raise.txt
+++ b/pylint/test/functional/misplaced_bare_raise.txt
@@ -1,6 +1,7 @@
-misplaced-bare-raise:35:test1.best:The raise statement is not inside a try suite
-misplaced-bare-raise:38:test1:The raise statement is not inside a try suite
-misplaced-bare-raise:39::The raise statement is not inside a try suite
-misplaced-bare-raise:48::The raise statement is not inside a try suite
-misplaced-bare-raise:56:A:The raise statement is not inside a try suite
-misplaced-bare-raise:67::The raise statement is not inside a try suite \ No newline at end of file
+misplaced-bare-raise:5::The raise statement is not inside an except clause
+misplaced-bare-raise:35:test1.best:The raise statement is not inside an except clause
+misplaced-bare-raise:38:test1:The raise statement is not inside an except clause
+misplaced-bare-raise:39::The raise statement is not inside an except clause
+misplaced-bare-raise:48::The raise statement is not inside an except clause
+misplaced-bare-raise:56:A:The raise statement is not inside an except clause
+misplaced-bare-raise:67::The raise statement is not inside an except clause \ No newline at end of file