bad-exception-context / E0703#

Message emitted:

Exception context set to something which is not an exception, nor None

Description:

Used when using the syntax "raise ... from ...", where the exception context is not an exception, nor None.

Correct code:

def divide(x, y):
    result = 0
    try:
        result = x / y
    except ZeroDivisionError as exc:
        raise ValueError(f"Division by zero when dividing {x} by {y} !") from exc
    return result

Problematic code:

def divide(x, y):
    result = 0
    try:
        result = x / y
    except ZeroDivisionError:
        raise ValueError(f"Division by zero when dividing {x} by {y} !") from result  # [bad-exception-context]
    return result

Related links:

Created by the exceptions checker.