[IDEA] Generic `Never` for more explicit exception type-hinting

In one sentence: allow Never to be generic, bound to BaseException.

def divide(a, b: Literal[0]) -> Never[ZeroDivisionError]:...
def divide(a: int, b: int) -> float: ...
def divide(a, b) : return a/b

    divide(5, 0)  # ✔ ZeroDivisionError wrapped in correct try-except block
except ZeroDivisionError:

    divide(5, 0)  # ✘ type-checker can produce 2 messages
   # [uncaught-exception] divide(5, 0) raises ZeroDivisionError
   # Note: ValueError does not cover ZeroDivisionError
except ValueError:

divide(5, 0)  # ✘ [uncaught-exception] raises ZeroDivisionError

Non-generic Never should remain unchanged. try-except-blocks act like type: ignore statements. TypingError: type checker will emit error with a message specified in typing stubs · Issue #1043 · python/typing · GitHub

See previous discussions on checked exceptions, the conclusion of which were:

1 Like