Skip to content

Commit

Permalink
Merge pull request #12 from ddelange/patch-1
Browse files Browse the repository at this point in the history
Add B902: blind except Exception: statement
  • Loading branch information
elijahandrews authored Jan 7, 2021
2 parents 6faaa55 + d879a31 commit c2c28b3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
23 changes: 19 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
flake8-blind-except
===================

A flake8 extension that checks for blind, catch-all ``except:`` statements.
A flake8 extension that checks for blind, catch-all ``except:`` and ``except Exception:`` statements.

As of `pycodestyle 2.1.0 <https://github.com/PyCQA/pycodestyle/commit/543f12b06592c53e2e60edc4846ee02ab9550e8b/>`_, "E722 do not use bare except, specify exception instead" is built-in. However, bare ``Exception`` and ``BaseException`` are still allowed. This extension flags them as `B902`.

Using ``except`` without explicitly specifying which exceptions to catch is generally considered bad practice, since it catches system signals like ``SIGINT``. You probably want to handle system interrupts differently than exceptions occuring in your code.

Expand Down Expand Up @@ -50,17 +52,30 @@ When a blind except is found, ``flake8`` will output::

B901 blind except: statement

or::

B902 blind except Exception: statement

Contributing
------------

I'm not working on Python these days, so probably won't be making updates anytime soon. PRs are welcome though!

Testing
-------

Tests can be run with ``pytest --doctest-modules flake8_blind_except.py``.

Changes
------
-------

0.2.0 - 2021-01-07
``````````````````
* B902 error added for cases where a blind ``Exception`` is caught.

0.1.1 - 2016-06-27
``````````````````
* ``pep8`` was renamed to ``pycodestyle`` in its 2.0 release. Compatibility update for this change
* ``pep8`` was renamed to ``pycodestyle`` in its 2.0 release. Compatibility update for this change.

0.1.0 - 2014-02-07
``````````````````
Expand All @@ -69,4 +84,4 @@ Changes
Notes
-----

I've tested this package with flake8 2.6.2 and Python 2.7.3. It is untested (but likely compatible) with other software versions.
I've tested this package with flake8 2.6.2 + Python 2.7.3 and flake8 3.7.9 + Python 3.7.5.
30 changes: 25 additions & 5 deletions flake8_blind_except.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@

try:
import pycodestyle
except ImportError:
import pep8 as pycodestyle
import re

__version__ = '0.1.1'

BLIND_EXCEPT_REGEX = re.compile(r'(except:)') # noqa
__version__ = '0.2.0'

BLIND_EXCEPT_REGEX = re.compile(r'(^[ \t]*except(.*\b(Base)?Exception\b.*)?:)') # noqa

def check_blind_except(physical_line):
"""Check for blind except statements.
>>> check_blind_except('except:')
(0, 'B901 blind except: statement')
>>> check_blind_except('except Exception:')
(0, 'B902 blind except Exception: statement')
>>> check_blind_except('except Exception as exc:')
(0, 'B902 blind except Exception: statement')
>>> check_blind_except('except ValueError, Exception as exc:')
(0, 'B902 blind except Exception: statement')
>>> check_blind_except('except Exception, ValueError as exc:')
(0, 'B902 blind except Exception: statement')
>>> check_blind_except('except BaseException as exc:')
(0, 'B902 blind except Exception: statement')
>>> check_blind_except('except GoodException as exc: # except:')
>>> check_blind_except('except ExceptionGood as exc:')
>>> check_blind_except('except Exception') # only trigger with trailing colon
>>> check_blind_except('some code containing except: in string')
"""
if pycodestyle.noqa(physical_line):
return
match = BLIND_EXCEPT_REGEX.search(physical_line)
if match:
return match.start(), 'B901 blind except: statement'
if match.group(2) is None:
return match.start(), 'B901 blind except: statement'
else:
return match.start(), 'B902 blind except Exception: statement'

check_blind_except.name = 'flake8-blind-except'
check_blind_except.version = __version__

0 comments on commit c2c28b3

Please sign in to comment.