An alternative to Annotated

If @ has a special syntax in annotations, would it conflict with using __matmul__ to evaluate type annotations with variadic generics (PEP 646)? Expanding the example in PEP 646, the current form of array multiplication would look like:

class Array(Generic[DType, *Shape]):

    def __matmul__(self, other: Array[Dtype, *OtherShape]) -> Array[DType, *Shape[:-1], *OtherShape[1:])

But with a subclass of TypeVarTuple, the following makes intuitive sense:

class ShapeTuple(TypeVarTuple):

    def __matmul__(self, other: ShapeTuple) -> ShapeTuple:
        return TypeVarTuple(self[:-1], other[1:])

class Array(Generic[DType, *Shape]):
    def __matmul__(self, other: Array[Dtype, *OtherShape]) -> Array[Dtype, Shape @ OtherShape]

Granted, currently “TypeVarTuples cannot be split”. But as the following section notes, “We plan to introduce these in a future PEP”.

1 Like