Skip to content

Commit ad1ce37

Browse files
authored
Merge pull request #46 from cwacek/fix/8-string-representations
Fix #8. Python 3 removes support for __cmp__.
2 parents fadc781 + c946e84 commit ad1ce37

File tree

3 files changed

+62
-10
lines changed

3 files changed

+62
-10
lines changed

python_jsonschema_objects/classbuilder.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,9 @@ def __repr__(self):
355355
str(self._value)
356356
)
357357

358+
def __str__(self):
359+
return str(self._value)
360+
358361
def validate(self):
359362
info = self.propinfo('__literal__')
360363

@@ -364,16 +367,14 @@ def validate(self):
364367
if validator is not None:
365368
validator(paramval, self._value, info)
366369

370+
def __eq__(self, other):
371+
return self._value == other
372+
373+
def __hash__(self):
374+
return hash(self._value)
367375

368-
def __cmp__(self, other):
369-
if isinstance(other, six.integer_types):
370-
return cmp(int(self), other)
371-
elif isinstance(other, six.string_types):
372-
return cmp(str(self), other)
373-
elif isinstance(other, float):
374-
return cmp(float(self), other)
375-
else:
376-
return cmp(id(self), id(other))
376+
def __lt__(self, other):
377+
return self._value < other
377378

378379
def __int__(self):
379380
return int(self._value)

test/test_regression_8.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import pytest
2+
3+
import python_jsonschema_objects as pjo
4+
5+
6+
@pytest.fixture
7+
def test_instance():
8+
schema = {
9+
'title': 'Example',
10+
'properties': {
11+
'stringProp': {'type': 'string'},
12+
'arrayProp': {
13+
'type': 'array',
14+
'items': {
15+
'type': 'string',
16+
}
17+
}
18+
}
19+
}
20+
21+
builder = pjo.ObjectBuilder(schema)
22+
ns = builder.build_classes()
23+
instance = ns.Example(
24+
stringProp='This seems fine',
25+
arrayProp=['these', 'are', 'problematic']
26+
)
27+
return instance
28+
29+
30+
def test_string_properties_compare_to_strings(test_instance):
31+
test = test_instance.stringProp == "This seems fine"
32+
assert test
33+
34+
35+
def test_arrays_of_strings_compare_to_strings(test_instance):
36+
test = test_instance.arrayProp == ['these', 'are', 'problematic']
37+
assert test
38+
39+
40+
def test_array_elements_compare_to_types(test_instance):
41+
elem = test_instance.arrayProp[0]
42+
test = elem == 'these'
43+
assert test
44+
45+
def test_repr_shows_property_values(test_instance):
46+
expected = "<example/arrayProp_<anonymous_field> these>"
47+
assert repr(test_instance.arrayProp[0]) == expected
48+
49+
def test_str_shows_just_strings(test_instance):
50+
test = str(test_instance.arrayProp[0])
51+
assert test == 'these'

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
[tox]
3-
envlist = py27, py34
3+
envlist = py27, py35
44

55
[testenv]
66
;install_command = pip install {opts} {packages}

0 commit comments

Comments
 (0)