Suppose I have a function that filters out the data like so:
from typing import Dict, Union
def filterData(data: Dict[str, str], fields: Union[list, tuple]) -> dict:
filteredData = {}
for field in fields:
if field in data:
filteredData[field] = data[field]
return filteredData
And I use this function like so:
data = filterData({"a": "b", "c": "d"}, ("a")) # This should return {"a": "b"}.
Now, when I try to access a
property like data.a
, I get an error which says Instance of dict has no member a
. How do I define and apply a type which defines all the members of that dictionary?
I tried to create a class,
class ReqData():
a: str
And then applied to variable like so,
data: ReqData = filterData({"a": "b", "c": "d"}, ("a")
But this didn't work either.