typelets.runtime

Utility for asserting never types with more flexibility.

Added in version 1.1.

Functions

raise_invalid_type(value, message[, ...])

Raise an exception far an invalid type.

typelets.runtime.raise_invalid_type(value: Never, message: str, exception_type: type[Exception] = <class 'ValueError'>) Never[source]

Raise an exception far an invalid type.

In many cases, especially for public APIs, we want runtime checking for argument types as well as type checking. This method can be used as a type guard in those cases where that code would be flagged by the type checkers as unreachable.

This works similarly to typing.assert_never(), but allows customization of the exception type and message, since AssertionError is not great from an API standpoint.

Added in version 1.1.

Parameters:
  • value (object) – The value which was not of a usable type.

  • message (str) – The message to use for the exception.

  • exception_type (type, optional) – The exception type to raise.

Raises:

Exception – This method always raises an exception.