Hi all,
I was hoping to get some opinions on whether something like the following
is valid, and if so, how it should be interpreted:
from typing import MutableSequence, TypeVar
T = TypeVar('T')
def f(seq: MutableSequence[T]) -> int:
return len(seq)
(This is, of course, *not* inside a generic class parameterized on `T`.)
Currently, pytype emits an [invalid-annotation] error for this piece of
code, with the explanation that `T` appears only once in `f`. Neither mypy
nor pyre emits an error. As far as I know, how a single TypeVar should be
treated is not covered in any of the typing PEPs.
pytype's reasons for making this an error are:
* The error messaging helps users avoid mistakes caused by misunderstanding
how to use a TypeVar.
* pytype's behavior when encountering a single TypeVar (especially when a
bound or constraints come into play) is ill-defined, so we don't want
anyone depending on it.
The reasons I've seen for not making it an error are:
* The TypeVar provides a way to annotate `seq` as a sequence of anything,
while still allowing usage errors to be caught in the body of `f`. `Any`
swallows all errors, and `object` does not work for invariant containers.
* There are potential future uses for a single TypeVar. One that came up in
the tensor typing call this morning is using a ListVariadic as a
placeholder to represent an unused part of an annotation.
* PEP 484 does not explicitly say that this should be an error, so it makes
sense to err on the side of allowing it.
* In spirit, `T` is an unused variable; flagging such things is not the job
of a type checker.
Thoughts?
Best,
Rebecca