I used assignment expression (aka walrus operator, defined in PEP 572) to define a type T. It seemed an elegant solution, but apparently mypy does not agree.
For the following code:
# Python 3.10.4
from collections.abc import Sequence
from typing import TypeVar
def foo(seq: Sequence[T := TypeVar('T')]) -> T:
return seq[0]
mypy reports:
error: Invalid type comment or annotation
error: Name "T" is not defined
Is walrus somehow forbidden with TypeVar?