Is there an easy way to trick freely defined dict objects in Python? For example, how can I easily express that given dict input , I want to check if each value in it corresponds to a certain meta-definition, for example, minimum and maximum values, lengths and types?
The ability to do this can come in handy, for example, when writing tests.
In mock ( unittest.mock in Python versions 3.3+), you can specify that the value can be ANY , as in
>>> mock = Mock(return_value=None) >>> mock('foo', bar=object()) >>> mock.assert_called_once_with('foo', bar=ANY)
However, what if bar above should be a dictate-like object, for example
>>> {'baz': <an integer between -3 and 14>, 'qux': <'yes' or 'no'>}
source share