dict 'type' object is not subscriptable when declaring a dict type variable

Question:

I am trying to define a dictionary that will hold a list:

import numpy as np

def processing_stitch_visual_outputs(
    saved_visual_outputs: dict[str, np.ndarray], 
    nrow: int, 
    ncol: int
)

But I am getting an error indicating the dictionary is not subscriptable:

Exception has occurred: TypeError
'type' object is not subscriptable
  File "functions.py", line 168, in <module>
    saved_visual_outputs: dict[str, np.ndarray],

My guess is that the dict variable can not contain an array, but I don’t see a reason why not.

Asked By: Hector Edu Nseng

||

Answers:

To solve this error you want to make use of the Dict class from the typing module as follows:

from typing import Dict
import numpy as np

def processing_stitch_visual_outputs(
    saved_visual_outputs: Dict[str, np.ndarray], 
    nrow: int, 
    ncol: int
)

I haven’t tried it myself, but the way you have used type hintin might work for python version 3.9 and up, so trying to upgrade your python version might also solve the issue.

Answered By: Oxbowerce