summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Cordasco <sigmavirus24@users.noreply.github.com>2017-01-01 07:53:17 -0600
committerGitHub <noreply@github.com>2017-01-01 07:53:17 -0600
commitd7d61542bf579a41dd33f2920f3214c62c49fd47 (patch)
treea7d2d60e5a73646c023f993a7b257a775136a426
parentfcc8f58f3d805e7529cfef1005285fdef1107917 (diff)
parent543f12b06592c53e2e60edc4846ee02ab9550e8b (diff)
downloadpep8-d7d61542bf579a41dd33f2920f3214c62c49fd47.tar.gz
Merge pull request #592 from rogalski/bare-except
Warn about bare except clause
-rw-r--r--CHANGES.txt1
-rw-r--r--docs/intro.rst2
-rwxr-xr-xpycodestyle.py22
-rw-r--r--testsuite/E30.py2
-rw-r--r--testsuite/E40.py2
-rw-r--r--testsuite/E72.py28
6 files changed, 53 insertions, 4 deletions
diff --git a/CHANGES.txt b/CHANGES.txt
index aeb5d63..9c43531 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -28,6 +28,7 @@ Changes:
* Report E741 on 'global' and 'nonlocal' statements, as well as prohibited
single-letter variables.
* Deprecated use of `[pep8]` section name in favor of `[pycodestyle]`; #591
+* Report E722 when bare except clause is used; #579
Bugs:
diff --git a/docs/intro.rst b/docs/intro.rst
index b1dc689..fcdcf72 100644
--- a/docs/intro.rst
+++ b/docs/intro.rst
@@ -361,6 +361,8 @@ This is the current list of error and warning codes:
+------------+----------------------------------------------------------------------+
| E721 (^) | do not compare types, use 'isinstance()' |
+------------+----------------------------------------------------------------------+
+| E722 | do not use bare except, specify exception instead |
++------------+----------------------------------------------------------------------+
| E731 | do not assign a lambda expression, use a def |
+------------+----------------------------------------------------------------------+
| E741 | do not use variables named 'l', 'O', or 'I' |
diff --git a/pycodestyle.py b/pycodestyle.py
index e308177..559048e 100755
--- a/pycodestyle.py
+++ b/pycodestyle.py
@@ -912,8 +912,10 @@ def module_imports_on_top_of_file(
Okay: # this is a comment\nimport os
Okay: '''this is a module docstring'''\nimport os
Okay: r'''this is a module docstring'''\nimport os
- Okay: try:\n import x\nexcept:\n pass\nelse:\n pass\nimport y
- Okay: try:\n import x\nexcept:\n pass\nfinally:\n pass\nimport y
+ Okay:
+ try:\n\timport x\nexcept ImportError:\n\tpass\nelse:\n\tpass\nimport y
+ Okay:
+ try:\n\timport x\nexcept ImportError:\n\tpass\nfinally:\n\tpass\nimport y
E402: a=1\nimport os
E402: 'One string'\n"Two string"\nimport os
E402: a=1\nfrom sys import x
@@ -1179,6 +1181,22 @@ def comparison_type(logical_line, noqa):
yield match.start(), "E721 do not compare types, use 'isinstance()'"
+def bare_except(logical_line, noqa):
+ r"""When catching exceptions, mention specific exceptions whenever possible.
+
+ Okay: except Exception:
+ Okay: except BaseException:
+ E722: except:
+ """
+ if noqa:
+ return
+
+ regex = re.compile(r"except\s*:")
+ match = regex.match(logical_line)
+ if match:
+ yield match.start(), "E722 do not use bare except'"
+
+
def ambiguous_identifier(logical_line, tokens):
r"""Never use the characters 'l', 'O', or 'I' as variable names.
diff --git a/testsuite/E30.py b/testsuite/E30.py
index 105d368..aaa77b7 100644
--- a/testsuite/E30.py
+++ b/testsuite/E30.py
@@ -113,7 +113,7 @@ def a():
try:
a()
-except:
+except Exception:
pass
#: E305:5:1
def a():
diff --git a/testsuite/E40.py b/testsuite/E40.py
index 1051e32..18ac73a 100644
--- a/testsuite/E40.py
+++ b/testsuite/E40.py
@@ -18,7 +18,7 @@ import foo
#: Okay
try:
import foo
-except:
+except ImportError:
pass
else:
print('imported foo')
diff --git a/testsuite/E72.py b/testsuite/E72.py
index 8eb34cb..c18527f 100644
--- a/testsuite/E72.py
+++ b/testsuite/E72.py
@@ -49,3 +49,31 @@ if isinstance(res, types.MethodType):
pass
if type(a) != type(b) or type(a) == type(ccc):
pass
+#: E722
+try:
+ pass
+except:
+ pass
+#: E722
+try:
+ pass
+except Exception:
+ pass
+except:
+ pass
+#: E722 E203 E271
+try:
+ pass
+except :
+ pass
+#: Okay
+fake_code = """"
+try:
+ do_something()
+except:
+ pass
+"""
+try:
+ pass
+except Exception:
+ pass