From 543f12b06592c53e2e60edc4846ee02ab9550e8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Rogalski?= Date: Wed, 28 Dec 2016 19:31:49 +0100 Subject: warn about bare except clause --- CHANGES.txt | 1 + docs/intro.rst | 2 ++ pycodestyle.py | 22 ++++++++++++++++++++-- testsuite/E30.py | 2 +- testsuite/E40.py | 2 +- testsuite/E72.py | 28 ++++++++++++++++++++++++++++ 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 -- cgit v1.2.1