While playing with Python's typing
module I came across something curious:
>>> from typing import List, Tuple
>>> List[Tuple[int]]
typing.List<~T>[typing.Tuple[int]]
What's this Java-like syntax List<~T>
? What does it mean?
That's not actual Python syntax, so don't try to use it in a program. That said, this is how they chose to represent a generic type's type parameters. In a generic type's repr
, the declared type parameters are listed in Java-like <>
angle brackets, with a +
, -
, or ~
before each type parameter depending on whether that parameter is covariant, contravariant, or neither.
typing.List
takes a single non-covariant, non-contravariant type parameter named T
, so it gets a <~T>
after the name.
You'll notice that typing.Tuple
doesn't have any <>
stuff after its name. Tuple
is a weird special case, since it takes a variable number of type parameters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With