Permalink
Browse files

Merge pull request #592 from rogalski/bare-except

Warn about bare except clause
  • Loading branch information...
2 parents fcc8f58 + 543f12b commit d7d61542bf579a41dd33f2920f3214c62c49fd47 @sigmavirus24 sigmavirus24 committed on GitHub Jan 1, 2017
Showing with 53 additions and 4 deletions.
  1. +1 −0 CHANGES.txt
  2. +2 −0 docs/intro.rst
  3. +20 −2 pycodestyle.py
  4. +1 −1 testsuite/E30.py
  5. +1 −1 testsuite/E40.py
  6. +28 −0 testsuite/E72.py
View
@@ -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:
View
@@ -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' |
View
@@ -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.
View
@@ -113,7 +113,7 @@ def a():
try:
a()
-except:
+except Exception:
pass
#: E305:5:1
def a():
View
@@ -18,7 +18,7 @@
#: Okay
try:
import foo
-except:
+except ImportError:
pass
else:
print('imported foo')
View
@@ -49,3 +49,31 @@
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

0 comments on commit d7d6154

Please sign in to comment.