diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ee5487..222b846 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ Treat Python 2 old-style classes like types when validating. - [324](https://github.com/alecthomas/voluptuous/pull/324): Default values MUST now pass validation just as any regular value. This is a backward incompatible change if a schema uses default values that don't pass validation against the specified schema. +- [328](https://github.com/alecthomas/voluptuous/pull/328): + Modified __lt__ in Marker class to allow comparison with non Marker objects, such as str and int ## [0.10.5] diff --git a/voluptuous/schema_builder.py b/voluptuous/schema_builder.py index 88a0203..9f3a022 100644 --- a/voluptuous/schema_builder.py +++ b/voluptuous/schema_builder.py @@ -953,7 +953,9 @@ def __repr__(self): return repr(self.schema) def __lt__(self, other): - return self.schema < other.schema + if isinstance(other, Marker): + return self.schema < other.schema + return self.schema < other def __hash__(self): return hash(self.schema) diff --git a/voluptuous/tests/tests.md b/voluptuous/tests/tests.md index 18f6fba..5ba97ab 100644 --- a/voluptuous/tests/tests.md +++ b/voluptuous/tests/tests.md @@ -266,3 +266,8 @@ Ensure that subclasses of Invalid of are raised as is. ... exc = e >>> exc.errors[0].__class__.__name__ 'SpecialInvalid' + +Ensure that Optional('Classification') < 'Name' will return True instead of throwing an AttributeError + + >>> Optional('Classification') < 'Name' + True diff --git a/voluptuous/tests/tests.py b/voluptuous/tests/tests.py index 2977f35..bb799c9 100644 --- a/voluptuous/tests/tests.py +++ b/voluptuous/tests/tests.py @@ -1137,3 +1137,22 @@ def test_self_all(): def test_SomeOf_on_bounds_assertion(): with raises(AssertionError, 'when using "SomeOf" you should specify at least one of min_valid and max_valid'): SomeOf(validators=[]) + + +def test_sorting_dict_with_voluptuous_objects_and_str(): + schema_with_voluptuous_object = { + 'Name': 'Mo', + 'ExecutionTimestamp': 'Today', + Optional('Classification'): [Any(['A+', 'B+'])] + } + schema_without_voluptuous_object = { + 'Name': 'Mo', + 'ExecutionTimestamp': 'Today', + 'Classification': [Any(['A+', 'B+'])] + } + + assert_true(sorted(schema_with_voluptuous_object), sorted(schema_without_voluptuous_object)) + + +def test_comparing_voluptuous_object_to_str(): + assert_true(Optional('Classification') < 'Name')