Thursday 28 October 2021

Typechecking inline type hints in Python

Currently I'm using typeguard.typechecked to decorate my functions and check inputs and outputs from important functions.

from typeguard import typechecked
@typechecked  # Gives me an error if argument/return types don't match the hints
def takes_int_makes_float(x: int) -> float:
    return float(x)

But what if I want to check types coming from functions I cannot control, and error when the type isn't what I want?

@proposed_decorator
def does_something_interesting():
    # Implicitly assert that this is the right type.
    # Note that this is valid Python code already, and works as a hint to my IDE.
    x: float = makes_float_or_tuple()
    print(x)

Is there some way to do this? Surely it's possible to implement with a decorator using ast.

I'm aware that I can assert isinstance or something similar, but an automatic implicit approach would be a lot nicer.

Edit:

As a response to the suggestion of wrapping methods with typing information I'll share my actual use case.

I'm using torchtyping to document and ensure the shapes of my PyTorch tensors.

from torchtyping import TensorType
import torch

def interesting_reshaping_method(x: TensorType['batch', 'num_points', 'point_dim']):
    lengths: TensorType['batch', 'num_points', 1] = torch.norm(x, dim=2, keepdim=True)
    # ... do something that requires this exact shape to do what I want,
    # but will fail silently if the shape isn't what I want.

In this case I need to explicitly check the type of the tensor, and its shape would be different if for example I had used keepdim=False or some different dim. It also needs to be brief so I can use it as documentation as well as to catch bugs where they really are happening.



from Typechecking inline type hints in Python

No comments:

Post a Comment