Skip to content

Commit

Permalink
Merge pull request #100 from sonerayberk/merge-dicts-with-ignore-any
Browse files Browse the repository at this point in the history
Merge dicts with ignore_any attribute
  • Loading branch information
Deepwalker authored Dec 12, 2019
2 parents 6918489 + 29850d8 commit 217a5db
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
12 changes: 12 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,18 @@ def test_add_kwargs_ignore(self):
third.check({'bar': 4, 'bar1': 41})
third.check({'bar': 4, 'bar1': 41, 'eggs': 'blabla'})

def test_add_kwargs_ignore_any(self):
first = t.Dict(
t.Key('bip', trafaret=t.String()), ignore_extra='*'
)
second = t.Dict(
t.Key('bop', trafaret=t.Int())
)

third = first + second
third.check({'bip': u'bam', 'bop': 17, 'matter': False})
assert third.ignore_any

def test_add_kwargs_extra(self):
first = t.Dict(
t.Key('bar', trafaret=t.Int()), allow_extra=['eggs']
Expand Down
4 changes: 2 additions & 2 deletions trafaret/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1123,14 +1123,14 @@ def merge(self, other):
Extends one Dict with other Dict Key`s or Key`s list,
or dict instance supposed for Dict
"""
ignore = self.ignore
extra = self.extras
if isinstance(other, Dict):
other_keys = other.keys
ignore += other.ignore
extra += other.extras
ignore = '*' if (self.ignore_any or other.ignore_any) else self.ignore + other.ignore
elif isinstance(other, (list, tuple)):
other_keys = list(other)
ignore = self.ignore
elif isinstance(other, dict):
return self.__class__(other, *self.keys)
else:
Expand Down

0 comments on commit 217a5db

Please sign in to comment.