trafaret
trafaret copied to clipboard
Allow extra keys ignored after dict merge
Let me start with code,
In [1]: import trafaret as t
In [2]: td = t.Dict({'key': t.String()}).allow_extra('extra_key')
In [3]: td.merge(t.Dict({'other_key': t.String()})).check({'key': '1', 'extra_key': '2', 'other_key': '3'})
---------------------------------------------------------------------------
DataError Traceback (most recent call last)
<ipython-input-3-84cf57110770> in <module>()
----> 1 td.merge(t.Dict({'other_key': t.String()})).check({'key': '1', 'extra_key': '2', 'other_key': '3'})
/usr/local/lib/python3.6/dist-packages/trafaret/base.py in check(self, value, context)
116 """
117 if hasattr(self, 'transform'):
--> 118 return self.transform(value, context=context)
119 elif hasattr(self, 'check_value'):
120 self.check_value(value)
/usr/local/lib/python3.6/dist-packages/trafaret/base.py in transform(self, value, context)
990 errors[key] = de
991 if errors:
--> 992 raise DataError(error=errors, trafaret=self)
993 return collect
994
DataError: {'extra_key': DataError(extra_key is not allowed key)}
From my point of view DataError is unexpected, but am I right it is raised due to logic reasons, that on merge you ignore all extra t.Dict rules to avoid errors on startup? E.g., when I'll try to merge some dict with key from .allow_extra?
If no and this is not intentional, I'd say it is a bug.
Honestly this is a moment when I don't know. What behaviour is right?