I'm having trouble getting a correct type hint to satisfy mypy.
The function takes a dict[str, List], such as:
input = {"group_1": ['a', 'b', 'c'],
"group_2":['d', 'e', 'f'],
"group_3":['g', 'h', 'i']}
The function creates an object that will be used by another function to fill the empty lists according to some other logic.
def create_output_object(input: dict[str, List]) -> ?:
output = {}
groups = [k for k in input.keys()]
for group in groups:
if group not in output:
output[group] = {'from': {}, 'to': {}}
for k in output.keys():
for group in groups:
if group != k:
if group not in output[k]['from']:
output[k]['from'][group] = []
if group not in output[k]['to']:
output[k]['to'][group] = []
return output
The output of create_output_object(input)
looks like:
{
'group_1': {
'from': {'group_2': [], 'group_3': []},
'to': {'group_2': [], 'group_3': []}
},
'group_2': {
'from': {'group_1': [], 'group_3': []},
'to': {'group_1': [], 'group_3': []}
},
'group_3': {
'from':{'group_1': [], 'group_2': []},
'to': {'group_1': [], 'group_2': []}
},
'new_products': [],
'removed_products': []
}
A solution like the one in this looks promising.
However, the output will not always have the same number of groups or group names. For example, with one less group it would look like this:
{
'group_1': {
'from': {'group_2': []},
'to': {'group_2': []}
},
'group_2': {
'from': {'group_1': []},
'to': {'group_1': []}
},
'new_products': [],
'removed_products': []
}
The error I'm getting from mypy is
Need type annotation for "output" (hint: "output: Dict[<type>, <type>] = ...")