Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for beartype 0.17.0's __instancecheck_str__. #169

Merged
merged 1 commit into from
Feb 17, 2024

Commits on Feb 17, 2024

  1. Added support for beartype 0.17.0's __instancecheck_str__.

    Recall that jaxtyping will currently generate rich error messages in precisely one scenario: about the arguments and return types when doing:
    ```python
    @jaxtyped(typechecker=beartype)
    def foo(...): ...
    ```
    
    With this commit we add support for beartype 0.17.0's pseudo-standard `__instancecheck_str__`, which means the following:
    
    1. For those using beartype decorators, the following will *also* generate an informative error message, and moreover it will state exactly why (shape mismatch, dtype mismatch etc):
        ```python
        @jaxtyped(typechecker=None)
        @beartype
        def foo(...): ...
        ```
        (In practice we probably won't recommend the above combination in the docs just to keep things simple.)
    
    2. For those using the beartype import hook together with the jaxtyping import hook, we can probably also check `assert isinstance(x, Float[Array, "foo"])` statements with rich error messages. (#153) We'll need to test + document that though. (@jeezrick interested?)
    
    3. For those using plain `assert isinstance(...)` statements without beartype (#167, tagging @reinerp), then they can *also* get rich error messages by doing
        ```python
        tt = Float[Array, "foo"]
        assert isinstance(x, tt), tt.__instancecheck_str__(x) + "\n" + print_bindings()
        ```
        which is still a bit long-winded right now but is a step in the right direction.
    
    (CC @leycec for interest.)
    patrick-kidger committed Feb 17, 2024
    Configuration menu
    Copy the full SHA
    be0c58e View commit details
    Browse the repository at this point in the history