-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from ddelange/patch-1
Add B902: blind except Exception: statement
- Loading branch information
Showing
2 changed files
with
44 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__ |