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

Conversation

patrick-kidger
Copy link
Owner

@patrick-kidger patrick-kidger commented Feb 12, 2024

Recall that jaxtyping will currently generate rich error messages in precisely one scenario: about the arguments and return types when doing:

@jaxtyped(typechecker=beartype)
def foo(...): ...

With this commit we add support for beartype 0.17.0's pseudo-standard __instancecheck_str__, which allows us to report exactly why an instancecheck failed -- was the shape of the array wrong, was the dtype wrong, etc?

This 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):

    @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. (will runtime type checking go beyond function parameters and return type? #153) We'll need to test + document that though. (@jeezrick interested?)

  3. For those using plain assert isinstance(...) statements without beartype (Provide a way to get error diagnostics out of isinstance checks #167, tagging @reinerp), then they can also get rich error messages by doing

    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. Also CC @Qwlouse @Conchylicultor as I believe you're interested in this functionality.)

@leycec
Copy link

leycec commented Feb 12, 2024

You build excitement. Thanks so much for the 0-day turnaround on this. Yet, I think to myself: "There's just no way he did this so fast. Is he even still human? I refuse to believe!" 😄

Let me know if you hit any snags with @beartype's __instancecheck_str__() implementation. In theory, everything works. In practice, I sigh.

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 patrick-kidger merged commit 9c80aa6 into dev Feb 17, 2024
1 check passed
@patrick-kidger patrick-kidger deleted the instancecheck_str branch February 17, 2024 01:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants