Skip to content

SCR115: except-tuple-without-parentheses

except A, B: binds B instead of catching it

Code SCR115
Name except-tuple-without-parentheses
Category Scripts (SCR)
Default severity error
Enabled by default yes
Fix available always
Fix safety unsafe
ign-lint equivalent none

Reports except ValueError, TypeError: where the name after the comma is an exception class: a builtin exception, a class the script defines, or a capitalized name it imports with from m import Name. The name is looked up like Python does (the handler's function, enclosing functions, the module, the builtins), ignoring the handler itself. A module (except ValueError, sys: after import sys), any other variable, and a name the handler body reads are variables, as written.

In Python 2, except A, B: means "catch A and store the exception in the variable B". TypeError is not caught, and worse, the name TypeError is rebound to the caught exception for the rest of the function.

try:
value = int(text)
except ValueError, TypeError:
value = 0

Use instead:

try:
value = int(text)
except (ValueError, TypeError):
value = 0

SCR103 unused-variable and SCR117 redefined-builtin do not report the name of such a handler: this rule explains the mistake.

Adds the parentheses. Unsafe: the fixed handler catches a second exception class and no longer rebinds the name, so the program behaves differently (that is the point, but iglint cannot prove the author meant the class). No fix when a comment sits between the two names.


Generated from the rule metadata by iglint generate-docs; iglint rule SCR115 prints the same page in the terminal. Source: docs/rules/SCR115.md.