bjd2385 bjd2385 - 1 month ago 9
Python Question

What does typing.List<~T>[typing.Tuple[int]] mean in Python's typing lib?

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?

Answer

Let's see:

typing.List<~T>[typing.Tuple[int]]
  1. typing.List means a type from the typing library, named List
  2. <~T> means this type is generic, it's defined in terms of another type T
  3. [typing.Tuple[int]] means type T is the type Tuple[int].

In natural language, you can read the definition like this:

typing.List<~T>[typing.Tuple[int]]: a standard List of one-int tuples