Skip to content

Commit

Permalink
Merge pull request #1224 from shinnar/fix_nan
Browse files Browse the repository at this point in the history
Add is check to equals check, enabling support for nan
  • Loading branch information
Julian authored Feb 16, 2024
2 parents cdb8db4 + 20d8443 commit 3c7a1b6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions jsonschema/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ def equal(one, two):
Specifically in JSON Schema, evade `bool` inheriting from `int`,
recursing into sequences to do the same.
"""
if one is two:
return True
if isinstance(one, str) or isinstance(two, str):
return one == two
if isinstance(one, Sequence) and isinstance(two, Sequence):
Expand Down
14 changes: 14 additions & 0 deletions jsonschema/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from math import nan
from unittest import TestCase

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

def test_nan(self):
self.assertTrue(equal(nan, nan))


class TestDictEqual(TestCase):
def test_equal_dictionaries(self):
dict_1 = {"a": "b", "c": "d"}
dict_2 = {"c": "d", "a": "b"}
self.assertTrue(equal(dict_1, dict_2))

def test_equal_dictionaries_with_nan(self):
dict_1 = {"a": nan, "c": "d"}
dict_2 = {"c": "d", "a": nan}
self.assertTrue(equal(dict_1, dict_2))

def test_missing_key(self):
dict_1 = {"a": "b", "c": "d"}
dict_2 = {"c": "d", "x": "b"}
Expand Down Expand Up @@ -70,6 +79,11 @@ def test_equal_lists(self):
list_2 = ["a", "b", "c"]
self.assertTrue(equal(list_1, list_2))

def test_equal_lists_with_nan(self):
list_1 = ["a", nan, "c"]
list_2 = ["a", nan, "c"]
self.assertTrue(equal(list_1, list_2))

def test_unsorted_lists(self):
list_1 = ["a", "b", "c"]
list_2 = ["b", "b", "a"]
Expand Down

0 comments on commit 3c7a1b6

Please sign in to comment.