Search code examples
pythonpython-typing

What does ~T mean in the runtime repr of a type?


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?


Solution

  • 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