Skip to content

Commit

Permalink
Change flake8 code to BLE
Browse files Browse the repository at this point in the history
B901 and B902 are currently used by flake8-bugbear and set to ignore by
default.
Changing the codes will avoid conflict with flake8-bugbear.

noqa detection is also removed as it is now handled natively by flake8
itself.

Fixes elijahandrews#16
  • Loading branch information
wwuck committed Jan 18, 2023
1 parent 0c50047 commit 9ca359f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
24 changes: 10 additions & 14 deletions flake8_blind_except.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,37 @@
try:
import pycodestyle
except ImportError:
import pep8 as pycodestyle
import re

__version__ = '0.2.1'

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')
(0, 'BLE901 blind except: statement')
>>> check_blind_except('except Exception:')
(0, 'B902 blind except Exception: statement')
(0, 'BLE902 blind except Exception: statement')
>>> check_blind_except('except Exception as exc:')
(0, 'B902 blind except Exception: statement')
(0, 'BLE902 blind except Exception: statement')
>>> check_blind_except('except ValueError, Exception as exc:')
(0, 'B902 blind except Exception: statement')
(0, 'BLE902 blind except Exception: statement')
>>> check_blind_except('except Exception, ValueError as exc:')
(0, 'B902 blind except Exception: statement')
(0, 'BLE902 blind except Exception: statement')
>>> check_blind_except('except BaseException as exc:')
(0, 'B902 blind except Exception: statement')
(0, 'BLE902 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:
if match.group(2) is None:
return match.start(), 'B901 blind except: statement'
return match.start(), 'BLE901 blind except: statement'
else:
return match.start(), 'B902 blind except Exception: statement'
return match.start(), 'BLE902 blind except Exception: statement'


check_blind_except.name = 'flake8-blind-except'
check_blind_except.version = __version__
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def get_long_description():
author_email='[email protected]',
entry_points={
'flake8.extension': [
'B90 = flake8_blind_except:check_blind_except'
'BLE90 = flake8_blind_except:check_blind_except'
],
},
url='https://github.com/elijahandrews/flake8-blind-except',
Expand Down

0 comments on commit 9ca359f

Please sign in to comment.