Add W605: Use 'as' over comma when catching exceptions #177

Closed
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+21 −0
Split
View
16 pep8.py
@@ -93,6 +93,7 @@
INDENT_REGEX = re.compile(r'([ \t]*)')
RAISE_COMMA_REGEX = re.compile(r'raise\s+\w+\s*,')
+EXCEPT_COMMA_TARGET_IDENTIFIER_REGEX = re.compile(r'except\s+\w+\s*,')
RERAISE_COMMA_REGEX = re.compile(r'raise\s+\w+\s*,\s*\w+\s*,\s*\w+')
ERRORCODE_REGEX = re.compile(r'\b[A-Z]\d{3}\b')
DOCSTRING_REGEX = re.compile(r'u?r?["\']')
@@ -1005,6 +1006,21 @@ def python_3000_backticks(logical_line):
yield pos, "W604 backticks are deprecated, use 'repr()'"
+def python_3000_catching_exceptions(logical_line):
+ """
+ Commas to delineate exception identifier targets are removed in Python 3.
+ Use 'as' instead.
+
+ Okay: except DummyError as exc:
+ Okay: except (DummyError, AnotherError):
+ W605: except DummyError, exc:
+ """
+ match = EXCEPT_COMMA_TARGET_IDENTIFIER_REGEX.match(logical_line)
+ if match:
+ yield (match.end() - 1, "W605 commas to delineate exception "
+ " identifier targets are deprecated, use 'as'")
+
+
##############################################################################
# Helper functions
##############################################################################
View
@@ -12,3 +12,8 @@
x = 0
#: W604
val = `1 + 2`
+#: W605
+try:
+ pass
+except KeyError, exc:
+ pass