Skip to content

Commit 5c4102b

Browse files
committed
Be slightly less strict about what an object is.
1 parent 565dbcb commit 5c4102b

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

jsonschema.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
import urlparse
3939
iteritems = operator.methodcaller("iteritems")
4040

41+
try:
42+
from collections import abc
43+
except ImportError:
44+
import collections as abc
45+
4146

4247
FLOAT_TOLERANCE = 10 ** -15
4348
validators = {}
@@ -87,7 +92,7 @@ class Draft3Validator(object):
8792

8893
DEFAULT_TYPES = {
8994
"array" : list, "boolean" : bool, "integer" : numbers.Integral,
90-
"null" : type(None), "number" : numbers.Number, "object" : dict,
95+
"null" : type(None), "number" : numbers.Number, "object" : abc.Mapping,
9196
"string" : basestring,
9297
}
9398

tests.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import mock
1818

1919
from jsonschema import (
20-
PY3, SchemaError, UnknownType, ValidationError, ErrorTree,
20+
PY3, abc, SchemaError, UnknownType, ValidationError, ErrorTree,
2121
Draft3Validator, FormatChecker, RefResolver, validate
2222
)
2323

@@ -71,11 +71,18 @@ def add_test_methods(test_class):
7171
return add_test_methods
7272

7373

74-
class BytesMixin(object):
74+
class TypesMixin(object):
7575
@unittest.skipIf(PY3, "In Python 3 json.load always produces unicode")
7676
def test_string_a_bytestring_is_a_string(self):
7777
self.validator_class({"type" : "string"}).validate(b"foo")
7878

79+
def test_mappings_are_objects(self):
80+
class Mapping(abc.Mapping):
81+
def __getitem__(self): return 12
82+
def __iter__(self): return iter([])
83+
def __len__(self): return 12
84+
self.validator_class({"type" : "object"}).validate(Mapping())
85+
7986

8087
class DecimalMixin(object):
8188
def test_it_can_validate_with_decimals(self):
@@ -123,7 +130,7 @@ def test_it_validates_formats_if_a_checker_is_provided(self):
123130

124131
@load_json_cases("json/tests/draft3/*.json")
125132
@load_json_cases("json/tests/draft3/optional/bignum.json")
126-
class TestDraft3(unittest.TestCase, BytesMixin, DecimalMixin, FormatMixin):
133+
class TestDraft3(unittest.TestCase, TypesMixin, DecimalMixin, FormatMixin):
127134

128135
validator_class = Draft3Validator
129136

0 commit comments

Comments
 (0)