diff options
author | Tim Hatch <tim@timhatch.com> | 2012-08-29 09:05:26 +0200 |
---|---|---|
committer | Tim Hatch <tim@timhatch.com> | 2012-08-29 09:05:26 +0200 |
commit | 9df0acbb8a43b749b9f267dcf97521830965d39b (patch) | |
tree | e545c234e306dc6bac291db2b65c217dce9e77e2 | |
parent | bfc5ea47c96b3c91ff4a6e81a3dba93278bf60a1 (diff) | |
download | pylint-git-9df0acbb8a43b749b9f267dcf97521830965d39b.tar.gz |
Adds check for boolop being used as exception class. Closes #100707.
---
.../third_party/py/pylint/checkers/exceptions.py | 7 +++++++
.../third_party/py/pylint/test/input/func_w0711.py | 15 +++++++++++++++
.../py/pylint/test/messages/func_w0711.txt | 4 ++++
3 files changed, 26 insertions(+), 0 deletions(-)
create mode 100644 /test/input/func_w0711.py
create mode 100644 /test/messages/func_w0711.txt
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | checkers/exceptions.py | 7 | ||||
-rw-r--r-- | test/input/func_w0711.py | 15 | ||||
-rw-r--r-- | test/messages/func_w0711.txt | 4 |
4 files changed, 31 insertions, 0 deletions
@@ -1,6 +1,11 @@ ChangeLog for PyLint ==================== +-- + * #100707: check for boolop being used as exception class, introducing + new W0711 message (patch by Tim Hatch) + + 2012-07-17 -- 0.25.2 * #93591: Correctly emit warnings about clobbered variable names when an except handler contains a tuple of names instead of a single name. diff --git a/checkers/exceptions.py b/checkers/exceptions.py index 08f4334c3..e94fa65f4 100644 --- a/checkers/exceptions.py +++ b/checkers/exceptions.py @@ -58,6 +58,10 @@ MSGS = { 'W0710': ('Exception doesn\'t inherit from standard "Exception" class', 'Used when a custom exception class is raised but doesn\'t \ inherit from the builtin "Exception" class.'), + 'W0711': ('Exception to catch is the result of a binary "%s" operation', + 'Used when the exception to catch is of the form \ + "except A or B:". If intending to catch multiple, \ + rewrite as "except (A, B):"'), } @@ -157,6 +161,9 @@ class ExceptionsChecker(BaseChecker): elif index < (nb_handlers - 1): msg = 'empty except clause should always appear last' self.add_message('E0701', node=node, args=msg) + + elif isinstance(handler.type, astng.BoolOp): + self.add_message('W0711', node=handler, args=handler.type.op) else: try: excs = list(unpack_infer(handler.type)) diff --git a/test/input/func_w0711.py b/test/input/func_w0711.py new file mode 100644 index 000000000..9cc791ebc --- /dev/null +++ b/test/input/func_w0711.py @@ -0,0 +1,15 @@ +"""find binary operations used as exceptions +""" + +__revision__ = 1 + +try: + __revision__ += 1 +except Exception or StandardError: + print "caught1" +except Exception and StandardError: + print "caught2" +except (Exception or StandardError): + print "caught3" +except (Exception or StandardError), exc: + print "caught4" diff --git a/test/messages/func_w0711.txt b/test/messages/func_w0711.txt new file mode 100644 index 000000000..a15881872 --- /dev/null +++ b/test/messages/func_w0711.txt @@ -0,0 +1,4 @@ +W: 8: Exception to catch is the result of a binary "or" operation +W: 10: Exception to catch is the result of a binary "and" operation +W: 12: Exception to catch is the result of a binary "or" operation +W: 14: Exception to catch is the result of a binary "or" operation |