Add a raise_invalid_type helper for completeness checking.
Review Request #14588 — Created Sept. 4, 2025 and updated
Type checkers for Python are great, but one of the things they do is
they'll flag code as unreachable if there are branches which are
checking the type of arguments, even if that branch exists in order to
provide runtime type-checking of arguments for public APIs.
typing.assert_never
is designed for this case, but annoyingly it
always raises an AssertionError, and does not allow customization of the
message.This change adds a new method,
typelets.runtime.raise_invalid_type
,
which can be used in the same way asassert_never
, but defaults to
raising aValueError
(and allows different exception types if
desired), as well as taking in a message to include in the exception.
This allows us to write APIs which perform runtime type checking on
arguments without having the type checkers flag said checking as
unreachable based on the type hints.
Used this in some other code.