Search code examples
pythonpython-3.xtype-hinting

Type hinting produces "TypeError: type object is not subscriptable"


w: dict[int, int] = {1:2, 2:3, 69: 420} produces the error message in the title. Why won't specific type hints work for my dictionaries and lists?


Solution

  • As of python3.8, you need to import their respective classes from the typing module.

    from typing import Dict, List, Set
    
    w: Dict[int, int] = {1:2, 2:3, 69:420}
    

    Note the capital D in Dict.