Skip to content

Commit 20d8443

Browse files
committed
Add is check to equals check, enabling support for nan
Signed-off-by: Avi Shinnar <[email protected]>
1 parent cdb8db4 commit 20d8443

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

jsonschema/_utils.py

+2
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ def equal(one, two):
131131
Specifically in JSON Schema, evade `bool` inheriting from `int`,
132132
recursing into sequences to do the same.
133133
"""
134+
if one is two:
135+
return True
134136
if isinstance(one, str) or isinstance(two, str):
135137
return one == two
136138
if isinstance(one, Sequence) and isinstance(two, Sequence):

jsonschema/tests/test_utils.py

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from math import nan
12
from unittest import TestCase
23

34
from jsonschema._utils import equal
@@ -7,13 +8,21 @@ class TestEqual(TestCase):
78
def test_none(self):
89
self.assertTrue(equal(None, None))
910

11+
def test_nan(self):
12+
self.assertTrue(equal(nan, nan))
13+
1014

1115
class TestDictEqual(TestCase):
1216
def test_equal_dictionaries(self):
1317
dict_1 = {"a": "b", "c": "d"}
1418
dict_2 = {"c": "d", "a": "b"}
1519
self.assertTrue(equal(dict_1, dict_2))
1620

21+
def test_equal_dictionaries_with_nan(self):
22+
dict_1 = {"a": nan, "c": "d"}
23+
dict_2 = {"c": "d", "a": nan}
24+
self.assertTrue(equal(dict_1, dict_2))
25+
1726
def test_missing_key(self):
1827
dict_1 = {"a": "b", "c": "d"}
1928
dict_2 = {"c": "d", "x": "b"}
@@ -70,6 +79,11 @@ def test_equal_lists(self):
7079
list_2 = ["a", "b", "c"]
7180
self.assertTrue(equal(list_1, list_2))
7281

82+
def test_equal_lists_with_nan(self):
83+
list_1 = ["a", nan, "c"]
84+
list_2 = ["a", nan, "c"]
85+
self.assertTrue(equal(list_1, list_2))
86+
7387
def test_unsorted_lists(self):
7488
list_1 = ["a", "b", "c"]
7589
list_2 = ["b", "b", "a"]

0 commit comments

Comments
 (0)