Deepwalker / trafaret

Ultimate transformation library that supports validation, contexts and aiohttp.
http://trafaret.readthedocs.org/en/latest/
BSD 2-Clause "Simplified" License
177 stars 31 forks source link

Allow extra keys ignored after dict merge #24

Open playpauseandstop opened 6 years ago

playpauseandstop commented 6 years ago

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.

Deepwalker commented 6 years ago

Honestly this is a moment when I don't know. What behaviour is right?