Skip to content

Commit bbbc8f8

Browse files
committed
Don't mix unicode and bytes when loading schemas.
Get rid of stupid from __future__ import unicode_literals in places, which was causing these to be unicode.
1 parent 3de5b17 commit bbbc8f8

3 files changed

Lines changed: 107 additions & 113 deletions

File tree

jsonschema/_utils.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,12 @@ def load_schema(name):
5353
Load a schema from ./schemas/``name``.json and return it.
5454
5555
"""
56-
schemadir = os.path.join(
57-
os.path.dirname(os.path.abspath(__file__)),
58-
'schemas'
56+
57+
schema_dir = os.path.join(
58+
os.path.dirname(os.path.abspath(__file__)), "schemas",
5959
)
60-
schemapath = os.path.join(schemadir, '%s.json' % (name,))
61-
with open(schemapath) as f:
62-
return json.load(f)
60+
with open(os.path.join(schema_dir, name + ".json")) as schema_file:
61+
return json.load(schema_file)
6362

6463

6564
def indent(string, times=1):

jsonschema/tests/test_validators.py

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import unicode_literals
21
import contextlib
32
import json
43
import pprint
@@ -15,18 +14,18 @@
1514

1615
class TestCreateAndExtend(unittest.TestCase):
1716
def setUp(self):
18-
self.meta_schema = {"properties" : {"smelly" : {}}}
17+
self.meta_schema = {u"properties" : {u"smelly" : {}}}
1918
self.smelly = mock.MagicMock()
20-
self.validators = {"smelly" : self.smelly}
21-
self.types = {"dict" : dict}
19+
self.validators = {u"smelly" : self.smelly}
20+
self.types = {u"dict" : dict}
2221
self.Validator = create(
2322
meta_schema=self.meta_schema,
2423
validators=self.validators,
2524
default_types=self.types,
2625
)
2726

2827
self.validator_value = 12
29-
self.schema = {"smelly" : self.validator_value}
28+
self.schema = {u"smelly" : self.validator_value}
3029
self.validator = self.Validator(self.schema)
3130

3231
def test_attrs(self):
@@ -54,24 +53,24 @@ def test_iter_errors(self):
5453
def test_if_a_version_is_provided_it_is_registered(self):
5554
with mock.patch("jsonschema.validators.validates") as validates:
5655
validates.side_effect = lambda version : lambda cls : cls
57-
Validator = create(meta_schema={"id" : "id"}, version="my version")
56+
Validator = create(meta_schema={u"id" : ""}, version="my version")
5857
validates.assert_called_once_with("my version")
5958
self.assertEqual(Validator.__name__, "MyVersionValidator")
6059

6160
def test_if_a_version_is_not_provided_it_is_not_registered(self):
6261
with mock.patch("jsonschema.validators.validates") as validates:
63-
create(meta_schema={"id" : "id"})
62+
create(meta_schema={u"id" : "id"})
6463
self.assertFalse(validates.called)
6564

6665
def test_extend(self):
6766
validators = dict(self.Validator.VALIDATORS)
6867
new = mock.Mock()
6968

70-
Extended = extend(self.Validator, validators={"a new one" : new})
69+
Extended = extend(self.Validator, validators={u"a new one" : new})
7170

72-
validators.update([("a new one", new)])
71+
validators.update([(u"a new one", new)])
7372
self.assertEqual(Extended.VALIDATORS, validators)
74-
self.assertNotIn("a new one", self.Validator.VALIDATORS)
73+
self.assertNotIn(u"a new one", self.Validator.VALIDATORS)
7574

7675
self.assertEqual(Extended.META_SCHEMA, self.Validator.META_SCHEMA)
7776
self.assertEqual(Extended.DEFAULT_TYPES, self.Validator.DEFAULT_TYPES)
@@ -84,9 +83,9 @@ def setUp(self):
8483
def test_iter_errors(self):
8584
instance = [1, 2]
8685
schema = {
87-
"disallow" : "array",
88-
"enum" : [["a", "b", "c"], ["d", "e", "f"]],
89-
"minItems" : 3
86+
u"disallow" : u"array",
87+
u"enum" : [["a", "b", "c"], ["d", "e", "f"]],
88+
u"minItems" : 3
9089
}
9190

9291
got = (e.message for e in self.validator.iter_errors(instance, schema))
@@ -100,10 +99,10 @@ def test_iter_errors(self):
10099
def test_iter_errors_multiple_failures_one_validator(self):
101100
instance = {"foo" : 2, "bar" : [1], "baz" : 15, "quux" : "spam"}
102101
schema = {
103-
"properties" : {
104-
"foo" : {"type" : "string"},
105-
"bar" : {"minItems" : 2},
106-
"baz" : {"maximum" : 10, "enum" : [2, 4, 6, 8]},
102+
u"properties" : {
103+
"foo" : {u"type" : "string"},
104+
"bar" : {u"minItems" : 2},
105+
"baz" : {u"maximum" : 10, u"enum" : [2, 4, 6, 8]},
107106
}
108107
}
109108

@@ -119,55 +118,55 @@ def message_for(self, instance, schema, *args, **kwargs):
119118
return e.exception.message
120119

121120
def test_single_type_failure(self):
122-
message = self.message_for(instance=1, schema={"type" : "string"})
123-
self.assertEqual(message, "1 is not of type %r" % "string")
121+
message = self.message_for(instance=1, schema={u"type" : u"string"})
122+
self.assertEqual(message, "1 is not of type %r" % u"string")
124123

125124
def test_single_type_list_failure(self):
126-
message = self.message_for(instance=1, schema={"type" : ["string"]})
127-
self.assertEqual(message, "1 is not of type %r" % "string")
125+
message = self.message_for(instance=1, schema={u"type" : [u"string"]})
126+
self.assertEqual(message, "1 is not of type %r" % u"string")
128127

129128
def test_multiple_type_failure(self):
130-
types = ("string", "object")
131-
message = self.message_for(instance=1, schema={"type" : list(types)})
129+
types = u"string", u"object"
130+
message = self.message_for(instance=1, schema={u"type" : list(types)})
132131
self.assertEqual(message, "1 is not of type %r, %r" % types)
133132

134133
def test_object_without_title_type_failure(self):
135-
type = {"type" : [{"minimum" : 3}]}
136-
message = self.message_for(instance=1, schema={"type" : [type]})
134+
type = {u"type" : [{u"minimum" : 3}]}
135+
message = self.message_for(instance=1, schema={u"type" : [type]})
137136
self.assertEqual(message, "1 is not of type %r" % (type,))
138137

139138
def test_object_with_name_type_failure(self):
140139
name = "Foo"
141-
schema = {"type" : [{"name" : name, "minimum" : 3}]}
140+
schema = {u"type" : [{u"name" : name, u"minimum" : 3}]}
142141
message = self.message_for(instance=1, schema=schema)
143142
self.assertEqual(message, "1 is not of type %r" % (name,))
144143

145144
def test_dependencies_failure_has_single_element_not_list(self):
146145
depend, on = "bar", "foo"
147-
schema = {"dependencies" : {depend : on}}
146+
schema = {u"dependencies" : {depend : on}}
148147
message = self.message_for({"bar" : 2}, schema)
149148
self.assertEqual(message, "%r is a dependency of %r" % (on, depend))
150149

151150
def test_additionalItems_single_failure(self):
152151
message = self.message_for(
153-
[2], {"items" : [], "additionalItems" : False},
152+
[2], {u"items" : [], u"additionalItems" : False},
154153
)
155154
self.assertIn("(2 was unexpected)", message)
156155

157156
def test_additionalItems_multiple_failures(self):
158157
message = self.message_for(
159-
[1, 2, 3], {"items" : [], "additionalItems" : False}
158+
[1, 2, 3], {u"items" : [], u"additionalItems" : False}
160159
)
161160
self.assertIn("(1, 2, 3 were unexpected)", message)
162161

163162
def test_additionalProperties_single_failure(self):
164163
additional = "foo"
165-
schema = {"additionalProperties" : False}
164+
schema = {u"additionalProperties" : False}
166165
message = self.message_for({additional : 2}, schema)
167166
self.assertIn("(%r was unexpected)" % (additional,), message)
168167

169168
def test_additionalProperties_multiple_failures(self):
170-
schema = {"additionalProperties" : False}
169+
schema = {u"additionalProperties" : False}
171170
message = self.message_for(dict.fromkeys(["foo", "bar"]), schema)
172171

173172
self.assertIn(repr("foo"), message)
@@ -177,9 +176,9 @@ def test_additionalProperties_multiple_failures(self):
177176
def test_invalid_format_default_message(self):
178177
checker = FormatChecker(formats=())
179178
check_fn = mock.Mock(return_value=False)
180-
checker.checks("thing")(check_fn)
179+
checker.checks(u"thing")(check_fn)
181180

182-
schema = {"format" : "thing"}
181+
schema = {u"format" : u"thing"}
183182
message = self.message_for("bla", schema, format_checker=checker)
184183

185184
self.assertIn(repr("bla"), message)
@@ -194,10 +193,10 @@ class TestErrorReprStr(unittest.TestCase):
194193
def setUp(self):
195194
self.error = ValidationError(
196195
message=self.message,
197-
validator="type",
198-
validator_value="string",
196+
validator=u"type",
197+
validator_value=u"string",
199198
instance=5,
200-
schema={"type" : "string"},
199+
schema={u"type" : u"string"},
201200
)
202201

203202
def assertShows(self, message):
@@ -258,8 +257,8 @@ def test_one_item_paths(self):
258257
)
259258

260259
def test_multiple_item_paths(self):
261-
self.error.path = [0, "a"]
262-
self.error.schema_path = ["items", 0, 1]
260+
self.error.path = [0, u"a"]
261+
self.error.schema_path = [u"items", 0, 1]
263262
self.assertShows(
264263
"""
265264
Failed validating u'type' in schema[u'items'][0]:

0 commit comments

Comments
 (0)