From 20d84439ac203d342f8fb42be068632710c38d0a Mon Sep 17 00:00:00 2001 From: Avi Shinnar Date: Fri, 16 Feb 2024 09:21:30 -0500 Subject: [PATCH] Add is check to equals check, enabling support for nan Signed-off-by: Avi Shinnar --- jsonschema/_utils.py | 2 ++ jsonschema/tests/test_utils.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/jsonschema/_utils.py b/jsonschema/_utils.py index 8451bb839..54d28c041 100644 --- a/jsonschema/_utils.py +++ b/jsonschema/_utils.py @@ -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): diff --git a/jsonschema/tests/test_utils.py b/jsonschema/tests/test_utils.py index 4e542b962..d9764b0f9 100644 --- a/jsonschema/tests/test_utils.py +++ b/jsonschema/tests/test_utils.py @@ -1,3 +1,4 @@ +from math import nan from unittest import TestCase from jsonschema._utils import equal @@ -7,6 +8,9 @@ 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): @@ -14,6 +18,11 @@ def test_equal_dictionaries(self): 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"} @@ -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"]