On Tue, Feb 2, 2021 at 1:08 PM Matthew Rahtz via Typing-sig <typing-sig@python.org> wrote:
**Tensor[Any, ...]**

Ah, I'd missed Guido's comment that this is not valid syntax. Damn.

There's no need for profanity. :-)

It's valid syntax to *Python* (which parses it no different than `Tuple[int, ...]`), but it's  not given a valid meaning in PEP 484 or any of the follow-up PEPs, so mypy and pyright (AFAIK) don't allow it.
`Tensor[*Tuple[Any, ...]]` is certainly one option, but for the reasons in the previous section I don't like that it forces us into allowing unboundedness.

Could we perhaps just say that an unparameterized `Tensor` *behaves* as if it were `Tensor[Any, ...]` despite this not being valid syntax? How ugly a special-case would it be?

I'm fine with that.
**Arbitrary-rank tensors**

Oh, man, super well-caught! You're right, committing to invariance by default does put us in a tricky situation.

But then - trying this with a regular `TypeVar`, mypy seems to be happy with the following:

from typing import Generic, TypeVar

T = TypeVar('T')

class Tensor(Generic[T]):

def expects_arbitrary_tensor(x: Tensor):

def expects_concrete_tensor(x: Tensor[int]):

x: Tensor = Tensor()

y: Tensor[int] = Tensor()

Any idea why that works?

Because Any is special. It acts as if it is both a superclass and a subclass of every type, so whenever types are compared, Any is *always* allowed. It is defined this way in PEP 484.

--Guido van Rossum (python.org/~guido)