Skip to content

Commit

Permalink
On further review, I noticed that filter(None) would cause some non-T…
Browse files Browse the repository at this point in the history
…rue elements in the revisions to not be merged, and would even cause a crash. Added test and corrected implementation.
  • Loading branch information
jaraco committed Jan 10, 2019
1 parent be08cb5 commit 851b9eb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
4.4.1
=====

Fixed issue in ``collate_revs`` when objects being merged were
non-True.

4.4
===

Expand Down
17 changes: 15 additions & 2 deletions jaraco/itertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1112,12 +1112,25 @@ def collate_revs(old, new, key=lambda x: x, merge=lambda old, new: new):
a a
b b
c c
The merge should not suppress non-True items:
>>> consume(collate_revs([0, 1, 2, None, ''], [0, None, ''], merge=print))
None None
<BLANKLINE>
0 0
"""
missing = object()

def maybe_merge(*items):
"""
Merge any non-null items
"""
return functools.reduce(merge, filter(None, items))
def not_missing(ob):
return ob is not missing

return functools.reduce(merge, filter(not_missing, items))

new_items = collections.OrderedDict(
(key(el), el)
Expand All @@ -1140,7 +1153,7 @@ def maybe_merge(*items):
for new_key, new_item in before.items():
# ensure any new keys are merged with previous items if
# they exist
yield maybe_merge(new_item, old_items.pop(new_key, None))
yield maybe_merge(new_item, old_items.pop(new_key, missing))
yield merge(old_item, match_new)

# finally, yield whatever is leftover
Expand Down

0 comments on commit 851b9eb

Please sign in to comment.