torchtyping icon indicating copy to clipboard operation
torchtyping copied to clipboard

Support checks via docstrings instead of type annotations

Open addisonklinke opened this issue 2 years ago • 2 comments

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

addisonklinke avatar Feb 28 '22 04:02 addisonklinke

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?

patrick-kidger avatar Feb 28 '22 11:02 patrick-kidger

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

addisonklinke avatar Feb 28 '22 17:02 addisonklinke