Search code examples
pythonpython-typing

Type hint for a dict gives TypeError: 'type' object is not subscriptable


memo: dict[int, int] = {0: 0, 1: 1} # *our base cases*

returns the following error:

TypeError: 'type' object is not subscriptable

Solution

  • I guess you should use Dict, e.g.:

    from typing import Dict
    
    memo: Dict[int, int] = {0: 0, 1: 1}
    

    In your case, you were using dict which is of type type

    >>> type(dict)
    <class 'type'>
    >>> type(Dict)
    <class 'typing._GenericAlias'>