Skip to content

Commit 01f047e

Browse files
Merge pull request #279 from ngaya-ll/master
Better old-style class support
2 parents 2a690a8 + 25da46e commit 01f047e

3 files changed

Lines changed: 39 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## [Unreleased]
44

5+
- [#279](https://github.com/alecthomas/voluptuous/pull/279):
6+
Treat Python 2 old-style classes like types when validating.
7+
58
## [0.10.5]
69

710
- [#278](https://github.com/alecthomas/voluptuous/pull/278): Unicode

voluptuous/schema_builder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def _compile(self, schema):
237237
elif isinstance(schema, tuple):
238238
return self._compile_tuple(schema)
239239
type_ = type(schema)
240-
if type_ is type:
240+
if inspect.isclass(schema):
241241
type_ = schema
242242
if type_ in (bool, bytes, int, long, str, unicode, float, complex, object,
243243
list, dict, type(None)) or callable(schema):
@@ -700,7 +700,7 @@ def _compile_scalar(schema):
700700
>>> with raises(er.Invalid, 'not a valid value'):
701701
... _compile_scalar(lambda v: float(v))([], 'a')
702702
"""
703-
if isinstance(schema, type):
703+
if inspect.isclass(schema):
704704
def validate_instance(path, data):
705705
if isinstance(data, schema):
706706
return data

voluptuous/tests/tests.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from voluptuous import (
77
Schema, Required, Optional, Extra, Invalid, In, Remove, Literal,
8-
Url, MultipleInvalid, LiteralInvalid, NotIn, Match, Email,
8+
Url, MultipleInvalid, LiteralInvalid, TypeInvalid, NotIn, Match, Email,
99
Replace, Range, Coerce, All, Any, Length, FqdnUrl, ALLOW_EXTRA, PREVENT_EXTRA,
1010
validate, ExactSequence, Equal, Unordered, Number, Maybe, Datetime, Date,
1111
Contains, Marker)
@@ -153,6 +153,39 @@ def test_literal():
153153
assert False, "Did not raise Invalid"
154154

155155

156+
def test_class():
157+
class C1(object):
158+
pass
159+
160+
schema = Schema(C1)
161+
schema(C1())
162+
163+
try:
164+
schema(None)
165+
except MultipleInvalid as e:
166+
assert_equal(str(e), "expected C1")
167+
assert_equal(len(e.errors), 1)
168+
assert_equal(type(e.errors[0]), TypeInvalid)
169+
else:
170+
assert False, "Did not raise Invalid"
171+
172+
# In Python 2, this will be an old-style class (classobj instance)
173+
class C2:
174+
pass
175+
176+
schema = Schema(C2)
177+
schema(C2())
178+
179+
try:
180+
schema(None)
181+
except MultipleInvalid as e:
182+
assert_equal(str(e), "expected C2")
183+
assert_equal(len(e.errors), 1)
184+
assert_equal(type(e.errors[0]), TypeInvalid)
185+
else:
186+
assert False, "Did not raise Invalid"
187+
188+
156189
def test_email_validation():
157190
""" test with valid email """
158191
schema = Schema({"email": Email()})

0 commit comments

Comments
 (0)