Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Equality between schemas #210

Merged
merged 3 commits into from
Sep 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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