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

Bug with default argument binding #206

Closed
jaraujo98 opened this issue May 18, 2024 · 2 comments · Fixed by #208
Closed

Bug with default argument binding #206

jaraujo98 opened this issue May 18, 2024 · 2 comments · Fixed by #208

Comments

@jaraujo98
Copy link
Contributor

jaraujo98 commented May 18, 2024

jaxtyping fails to detect default arguments when they are not provided explicitly. See the following MRE:

import numpy as np
from jaxtyping import Float, jaxtyped
from typeguard import typechecked as typechecker


class DefaultFactory:
    def __init__(self, n=1):
        self.n = n


@jaxtyped(typechecker=typechecker)
def function_with_default(
    positional_argument, default_argument: DefaultFactory = DefaultFactory(5)
) -> Float[np.ndarray, "positional_argument {default_argument.n}"]:
    return np.random.rand(positional_argument, default_argument.n)


function_with_default(3)

"""
Errors with
jaxtyping.AnnotationError: Cannot process symbolic axis 'default_argument.n' as some axis names have not been processed. In practice you should usually only use symbolic axes in annotations for return types, referring only to axes annotated for arguments.
"""

After digging a bit through the jaxtyping code, I found that the issue comes from the fact that the call to bound = param_signature.bind(*args, **kwargs) in jaxtyping._decorator.py:411 ignores default arguments. Manually patching bound with the default values in a debugger fixes the issue.

@jaraujo98 jaraujo98 changed the title Bug with (default) argument binding Bug with default argument binding May 18, 2024
@patrick-kidger
Copy link
Owner

Ah! Perhaps we need a bound.apply_defaults() call here. I'd be happy to take a PR on this.

@jaraujo98
Copy link
Contributor Author

Oh, wow, I knew it should be something simple, but not this simple ahahah. I'll test it and submit a PR.

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 a pull request may close this issue.

2 participants