diff --git a/voluptuous/schema_builder.py b/voluptuous/schema_builder.py index 8f93863..042a713 100644 --- a/voluptuous/schema_builder.py +++ b/voluptuous/schema_builder.py @@ -153,6 +153,17 @@ class Schema(object): Nodes can be values, in which case a direct comparison is used, types, in which case an isinstance() check is performed, or callables, which will validate and optionally convert the value. + + We can equate schemas also. + + For Example: + + >>> v = Schema({Required('a'): unicode}) + >>> v1 = Schema({Required('a'): unicode}) + >>> v2 = Schema({Required('b'): unicode}) + >>> assert v == v1 + >>> assert v != v2 + """ _extra_to_name = { @@ -181,6 +192,15 @@ def __init__(self, schema, required=False, extra=PREVENT_EXTRA): self.extra = int(extra) # ensure the value is an integer self._compiled = self._compile(schema) + def __eq__(self, other): + if str(other) == str(self.schema): + # Because repr is combination mixture of object and schema + return True + return False + + def __str__(self): + return str(self.schema) + def __repr__(self): return "" % ( self.schema, self._extra_to_name.get(self.extra, '??'), @@ -1053,7 +1073,7 @@ def validate(*a, **kw): Set restriction for returned value: >>> @validate(arg=int, __return__=int) - ... def foo(arg1): + ... def bar(arg1): ... return arg1 * 2 """ diff --git a/voluptuous/tests/tests.py b/voluptuous/tests/tests.py index 86a43ce..922d62b 100644 --- a/voluptuous/tests/tests.py +++ b/voluptuous/tests/tests.py @@ -423,7 +423,6 @@ def test_fix_157(): assert_raises(MultipleInvalid, s, ['four']) - def test_range_exlcudes_nan(): s = Schema(Range(min=0, max=10)) assert_raises(MultipleInvalid, s, float('nan'))