summaryrefslogtreecommitdiff
path: root/pycodestyle.py
diff options
context:
space:
mode:
authorŁukasz Rogalski <rogalski.91@gmail.com>2016-12-28 19:31:49 +0100
committerŁukasz Rogalski <rogalski.91@gmail.com>2016-12-28 19:31:49 +0100
commit543f12b06592c53e2e60edc4846ee02ab9550e8b (patch)
treea7d2d60e5a73646c023f993a7b257a775136a426 /pycodestyle.py
parentfcc8f58f3d805e7529cfef1005285fdef1107917 (diff)
downloadpep8-543f12b06592c53e2e60edc4846ee02ab9550e8b.tar.gz
warn about bare except clause
Diffstat (limited to 'pycodestyle.py')
-rwxr-xr-xpycodestyle.py22
1 files changed, 20 insertions, 2 deletions
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.