Skip to content

Commit

Permalink
Merge pull request #210 from tusharmakkar08/Issue-208
Browse files Browse the repository at this point in the history
Adding Equality between schemas
  • Loading branch information
tusharmakkar08 authored Sep 24, 2016
2 parents a321db5 + 4d88b6b commit 987fd98
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
22 changes: 21 additions & 1 deletion voluptuous/schema_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 "<Schema(%s, extra=%s, required=%s) object at 0x%x>" % (
self.schema, self._extra_to_name.get(self.extra, '??'),
Expand Down Expand Up @@ -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
"""
Expand Down
1 change: 0 additions & 1 deletion voluptuous/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down

0 comments on commit 987fd98

Please sign in to comment.