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

Support checks via docstrings instead of type annotations #34

Open
addisonklinke opened this issue Feb 28, 2022 · 2 comments
Open

Support checks via docstrings instead of type annotations #34

addisonklinke opened this issue Feb 28, 2022 · 2 comments

Comments

@addisonklinke
Copy link

First off - I've been thinking of almost the same idea as this library for a while because I see runtime errors from tensor shape/dtype mismatches all the time, so glad that there's already something in place!

My initial approach was going to be parsing docstrings of various formats (with an existing library like docstring_parser) and performing validation on these, rather than type annotations. Is that a feature you'd consider accepting into your library? I'd be interested in writing a PR for it with some guidance

For example, I currently write Sphinx style docstrings like this

def forward(self, imgs, tokens):
    """Combine multimodal input features

    :param torch.FloatTensor[N, C, H, W] imgs: Batch of image pixels
        normalized in range of 0-1
    :param torch.LongTensor[N, L] tokens: Vocabularly tokens in sequence
        of length ``L``
    :return torch.FloatTensor[N, C] pred: Predicted probabilities for
        each class
    """

I realize the [N, C, H, W] notation is not quite as rigid as what this project proposes, and that's one reason I've been looking for a more structured approach. But regardless, I do find it nice sometimes to have this information in the docstrings instead of type annotations, particularly for functions with many parameters

@patrick-kidger
Copy link
Owner

Hmm. Unless there's a big outcry from lots of people wanting this feature, then I'm inclined to keep things as-is just for simplicity.

However I expect it should be possible for you to piggy-back off of torchtyping and write your own library that does this. Parse the docstring, and then set __annotations__ appropriately, and then torchtyping/typeguard will take it from there. WDYT?

@addisonklinke
Copy link
Author

Sure I'll take a look into customizing via your suggested approach - thanks!

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

No branches or pull requests

2 participants