• 
      

    Add a raise_invalid_type helper for completeness checking.

    Review Request #14588 — Created Sept. 4, 2025 and submitted

    Information

    typelets
    main

    Reviewers

    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 as assert_never, but defaults to
    raising a ValueError (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.

    Summary ID
    Add a raise_invalid_type helper for completeness checking.
    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 as `assert_never`, but defaults to raising a `ValueError` (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. Testing Done: Used this in some other code.
    lxzzuunlmunktlyoysxnxooumqzulpsp
    Description From Last Updated

    Can you add this to docs/coderef/index.rst, docs/index.rst, and README.md?

    chipx86chipx86

    We should lock this behind a TYPE_CHECKING import.

    chipx86chipx86
    chipx86
    1. 
        
    2. Show all issues

      Can you add this to docs/coderef/index.rst, docs/index.rst, and README.md?

    3. typelets/runtime.py (Diff revision 1)
       
       
      Show all issues

      We should lock this behind a TYPE_CHECKING import.

    4. 
        
    david
    maubin
    1. Ship It!
    2. 
        
    chipx86
    1. Ship It!
    2. 
        
    david
    Review request changed
    Status:
    Completed
    Change Summary:
    Pushed to main (ed34f67)