Skip to content

Commit

Permalink
warn about bare except clause
Browse files Browse the repository at this point in the history
  • Loading branch information
rogalski committed Dec 28, 2016
1 parent fcc8f58 commit 543f12b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
2 changes: 2 additions & 0 deletions docs/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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' |
Expand Down
22 changes: 20 additions & 2 deletions pycodestyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion testsuite/E30.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def a():

try:
a()
except:
except Exception:
pass
#: E305:5:1
def a():
Expand Down
2 changes: 1 addition & 1 deletion testsuite/E40.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#: Okay
try:
import foo
except:
except ImportError:
pass
else:
print('imported foo')
Expand Down
28 changes: 28 additions & 0 deletions testsuite/E72.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 543f12b

Please sign in to comment.