Skip to content

Commit 543f12b

Browse files
committed
warn about bare except clause
1 parent fcc8f58 commit 543f12b

File tree

6 files changed

+53
-4
lines changed

6 files changed

+53
-4
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Changes:
2828
* Report E741 on 'global' and 'nonlocal' statements, as well as prohibited
2929
single-letter variables.
3030
* Deprecated use of `[pep8]` section name in favor of `[pycodestyle]`; #591
31+
* Report E722 when bare except clause is used; #579
3132

3233
Bugs:
3334

docs/intro.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,8 @@ This is the current list of error and warning codes:
361361
+------------+----------------------------------------------------------------------+
362362
| E721 (^) | do not compare types, use 'isinstance()' |
363363
+------------+----------------------------------------------------------------------+
364+
| E722 | do not use bare except, specify exception instead |
365+
+------------+----------------------------------------------------------------------+
364366
| E731 | do not assign a lambda expression, use a def |
365367
+------------+----------------------------------------------------------------------+
366368
| E741 | do not use variables named 'l', 'O', or 'I' |

pycodestyle.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -912,8 +912,10 @@ def module_imports_on_top_of_file(
912912
Okay: # this is a comment\nimport os
913913
Okay: '''this is a module docstring'''\nimport os
914914
Okay: r'''this is a module docstring'''\nimport os
915-
Okay: try:\n import x\nexcept:\n pass\nelse:\n pass\nimport y
916-
Okay: try:\n import x\nexcept:\n pass\nfinally:\n pass\nimport y
915+
Okay:
916+
try:\n\timport x\nexcept ImportError:\n\tpass\nelse:\n\tpass\nimport y
917+
Okay:
918+
try:\n\timport x\nexcept ImportError:\n\tpass\nfinally:\n\tpass\nimport y
917919
E402: a=1\nimport os
918920
E402: 'One string'\n"Two string"\nimport os
919921
E402: a=1\nfrom sys import x
@@ -1179,6 +1181,22 @@ def comparison_type(logical_line, noqa):
11791181
yield match.start(), "E721 do not compare types, use 'isinstance()'"
11801182

11811183

1184+
def bare_except(logical_line, noqa):
1185+
r"""When catching exceptions, mention specific exceptions whenever possible.
1186+
1187+
Okay: except Exception:
1188+
Okay: except BaseException:
1189+
E722: except:
1190+
"""
1191+
if noqa:
1192+
return
1193+
1194+
regex = re.compile(r"except\s*:")
1195+
match = regex.match(logical_line)
1196+
if match:
1197+
yield match.start(), "E722 do not use bare except'"
1198+
1199+
11821200
def ambiguous_identifier(logical_line, tokens):
11831201
r"""Never use the characters 'l', 'O', or 'I' as variable names.
11841202

testsuite/E30.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def a():
113113

114114
try:
115115
a()
116-
except:
116+
except Exception:
117117
pass
118118
#: E305:5:1
119119
def a():

testsuite/E40.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#: Okay
1919
try:
2020
import foo
21-
except:
21+
except ImportError:
2222
pass
2323
else:
2424
print('imported foo')

testsuite/E72.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,31 @@
4949
pass
5050
if type(a) != type(b) or type(a) == type(ccc):
5151
pass
52+
#: E722
53+
try:
54+
pass
55+
except:
56+
pass
57+
#: E722
58+
try:
59+
pass
60+
except Exception:
61+
pass
62+
except:
63+
pass
64+
#: E722 E203 E271
65+
try:
66+
pass
67+
except :
68+
pass
69+
#: Okay
70+
fake_code = """"
71+
try:
72+
do_something()
73+
except:
74+
pass
75+
"""
76+
try:
77+
pass
78+
except Exception:
79+
pass

0 commit comments

Comments
 (0)