summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2019-04-09 15:48:19 +0200
committerClaudiu Popa <pcmanticore@gmail.com>2019-04-09 15:52:11 +0200
commit474f4082a91e451929fa629b4d76c06407cfde47 (patch)
treeae722adb96e99a12047d0d70dc8b9f981850efd8
parent26c45aaf72e9e6596111867885a5e361eeea2303 (diff)
downloadpylint-git-474f4082a91e451929fa629b4d76c06407cfde47.tar.gz
When we can't infer bare except handlers, skip ``try-except-raise``
Close #2853
-rw-r--r--ChangeLog4
-rw-r--r--pylint/checkers/exceptions.py17
-rw-r--r--pylint/test/functional/try_except_raise.py9
-rw-r--r--pylint/test/functional/try_except_raise.txt1
4 files changed, 18 insertions, 13 deletions
diff --git a/ChangeLog b/ChangeLog
index 0634fe387..f5a59538a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -7,6 +7,10 @@ What's New in Pylint 2.4.0?
Release date: TBA
+* When we can't infer bare except handlers, skip ``try-except-raise``
+
+ Close #2853
+
* Handle more `unnecessary-lambda` cases when dealing with additional kwargs in wrapped calls
Close #2845
diff --git a/pylint/checkers/exceptions.py b/pylint/checkers/exceptions.py
index 6521a5bd5..be4d7c9ac 100644
--- a/pylint/checkers/exceptions.py
+++ b/pylint/checkers/exceptions.py
@@ -28,6 +28,7 @@ import sys
import typing
import astroid
+from astroid.node_classes import NodeNG
from pylint import checkers, interfaces
from pylint.checkers import utils
@@ -405,7 +406,9 @@ class ExceptionsChecker(checkers.BaseChecker):
)
def _check_try_except_raise(self, node):
- def gather_exceptions_from_handler(handler):
+ def gather_exceptions_from_handler(
+ handler
+ ) -> typing.Optional[typing.List[NodeNG]]:
exceptions = []
if handler.type:
exceptions_in_handler = utils.safe_infer(handler.type)
@@ -417,6 +420,9 @@ class ExceptionsChecker(checkers.BaseChecker):
}
elif exceptions_in_handler:
exceptions = [exceptions_in_handler]
+ else:
+ # Break when we cannot infer anything reliably.
+ return None
return exceptions
bare_raise = False
@@ -429,9 +435,13 @@ class ExceptionsChecker(checkers.BaseChecker):
# also break early if bare except is followed by bare except.
excs_in_current_handler = gather_exceptions_from_handler(handler)
+
if not excs_in_current_handler:
bare_raise = False
break
+ if excs_in_bare_handler is None:
+ # It can be `None` when the inference failed
+ break
for exc_in_current_handler in excs_in_current_handler:
inferred_current = utils.safe_infer(exc_in_current_handler)
@@ -451,8 +461,9 @@ class ExceptionsChecker(checkers.BaseChecker):
bare_raise = True
handler_having_bare_raise = handler
excs_in_bare_handler = gather_exceptions_from_handler(handler)
- if bare_raise:
- self.add_message("try-except-raise", node=handler_having_bare_raise)
+ else:
+ if bare_raise:
+ self.add_message("try-except-raise", node=handler_having_bare_raise)
@utils.check_messages("wrong-exception-operation")
def visit_binop(self, node):
diff --git a/pylint/test/functional/try_except_raise.py b/pylint/test/functional/try_except_raise.py
index 2a9bed799..c78852b81 100644
--- a/pylint/test/functional/try_except_raise.py
+++ b/pylint/test/functional/try_except_raise.py
@@ -96,14 +96,6 @@ except (FileNotFoundError, PermissionError): # [try-except-raise]
except (OverflowError, ZeroDivisionError):
print("a failure")
-
-try:
- pass
-except invalid_name: # [try-except-raise]
- raise
-except TypeError:
- pass
-
try:
pass
except (FileNotFoundError, PermissionError):
@@ -117,4 +109,3 @@ except (FileNotFoundError, PermissionError):
raise
except (Exception,):
print("a failure")
- \ No newline at end of file
diff --git a/pylint/test/functional/try_except_raise.txt b/pylint/test/functional/try_except_raise.txt
index ccf8f11c6..882fd4007 100644
--- a/pylint/test/functional/try_except_raise.txt
+++ b/pylint/test/functional/try_except_raise.txt
@@ -4,4 +4,3 @@ try-except-raise:53:ddd:The except handler raises immediately
try-except-raise:67::The except handler raises immediately
try-except-raise:72::The except handler raises immediately
try-except-raise:94::The except handler raises immediately
-try-except-raise:102::The except handler raises immediately