From cd4f2cd1c861deb923e7e3a4ce071c4a98107720 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Sat, 30 Dec 2017 12:35:14 +0100 Subject: [PATCH] Replace Maybe class with a function The Any class can be compiled by voluptuous whereas Maybe cannot. Since it's just an alias to Any(None, v), replace it with that directly. --- voluptuous/tests/tests.py | 2 +- voluptuous/validators.py | 13 ++----------- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/voluptuous/tests/tests.py b/voluptuous/tests/tests.py index 98a82ca..2977f35 100644 --- a/voluptuous/tests/tests.py +++ b/voluptuous/tests/tests.py @@ -492,7 +492,7 @@ def test_repr(): ) assert_equal(repr(coerce_), "Coerce(int, msg='moo')") assert_equal(repr(all_), "All('10', Coerce(int, msg=None), msg='all msg')") - assert_equal(repr(maybe_int), "Maybe(%s)" % str(int)) + assert_equal(repr(maybe_int), "Any(None, %s, msg=None)" % str(int)) def test_list_validation_messages(): diff --git a/voluptuous/validators.py b/voluptuous/validators.py index af655c3..e39461c 100644 --- a/voluptuous/validators.py +++ b/voluptuous/validators.py @@ -485,7 +485,7 @@ def PathExists(v): raise PathInvalid("Not a Path") -class Maybe(object): +def Maybe(validator): """Validate that the object matches given validator or is None. :raises Invalid: if the value does not match the given validator and is not @@ -498,16 +498,7 @@ class Maybe(object): ... s("string") """ - - def __init__(self, validator): - self.validator = validator - self.schema = Any(None, validator) - - def __call__(self, v): - return self.schema(v) - - def __repr__(self): - return 'Maybe(%s)' % self.validator + return Any(None, validator) class Range(object):