Skip to content

Commit

Permalink
Fixed issue with profile data not merging fully.
Browse files Browse the repository at this point in the history
  • Loading branch information
jayclassless committed Sep 12, 2014
1 parent 1d67804 commit 389e3e2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
21 changes: 13 additions & 8 deletions prospector/profiles/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,36 +87,41 @@ def parse_profile(name, contents):
# parse_profile, for example
data = dict(_EMPTY_DATA)
else:
data = _merge_dict(_EMPTY_DATA, data, dict1_priority=False)
data = _merge_dict(_EMPTY_DATA, data)
return StrictnessProfile(name, data)


def _merge_dict(dict1, dict2, dedup_lists=False, dict1_priority=True):
def _merge_dict(dict1, dict2):
newdict = {}
newdict.update(dict1)

for key, value in dict2.items():
if key not in dict1:
newdict[key] = value
elif value is None and dict1[key] is not None:
newdict[key] = dict1[key]
elif dict1[key] is None and value is not None:

elif dict1[key] is None:
newdict[key] = value

elif value is None:
# Don't blast away existing values with None
pass

elif type(value) != type(dict1[key]):
raise ValueError("Could not merge conflicting types %s and %s" % (
type(value),
type(dict1[key]),
))

elif isinstance(value, dict):
newdict[key] = _merge_dict(
dict1[key],
value,
dedup_lists,
dict1_priority,
)

elif isinstance(value, (list, tuple)):
newdict[key] = list(set(dict1[key]) | set(value))
elif not dict1_priority:

else:
newdict[key] = value

return newdict
Expand Down
14 changes: 1 addition & 13 deletions tests/profiles/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,5 @@ def test_dict_merge(self):
'c': 4
}
}
self.assertEqual(expected, _merge_dict(a, b, dedup_lists=True, dict1_priority=False))
self.assertEqual(expected, _merge_dict(a, b))

expected = {
'int': 1,
'str': 'fish',
'bool': True,
'list': [1, 2, 3],
'dict': {
'a': 1,
'b': 2,
'c': 4
}
}
self.assertEqual(expected, _merge_dict(a, b, dedup_lists=True, dict1_priority=True))

0 comments on commit 389e3e2

Please sign in to comment.