What is the most appropriate way to handle typing when modifying the type of values stored in a dictionary?
For example, if I run the following code through mypy, it throws an error '... has no attribute "size" [attr-defined]'.
from typing import Dict
import numpy as np
import numpy.typing as npt
import torch
import torchvision.transforms as T
def to_tensor(my_dict: Dict[str, npt.NDArray[np.uint8]]) -> Dict[str, torch.FloatTensor]:
for key, val in my_dict.items():
my_dict[key] = T.functional.to_tensor(val)
return my_dict
test_dict = {"foo": np.random.random((3,10,10)), "bar": np.random.random((3, 10, 10))}
test_dict = to_tensor(test_dict)
print(test_dict['foo'].size())
Thanks!