summaryrefslogtreecommitdiff
path: root/jsonschema/_utils.py
diff options
context:
space:
mode:
authorJulian Berman <Julian@GrayVines.com>2021-07-09 15:24:59 +0200
committerJulian Berman <Julian@GrayVines.com>2021-07-09 17:53:32 +0200
commitbe4287cbce85d9089decee3eeb8744fc4d3a79fd (patch)
tree0f24a75d8a381bdc699527fcd7575e9be84543c6 /jsonschema/_utils.py
parent4398265201fe2375dfeba773fbe5c13fe33397ff (diff)
downloadjsonschema-be4287cbce85d9089decee3eeb8744fc4d3a79fd.tar.gz
Test only through _util.equal.
All of this is private, so the unit tests are for our own benefit -- we may as well stop before getting too deep into the helper function stack.
Diffstat (limited to 'jsonschema/_utils.py')
-rw-r--r--jsonschema/_utils.py32
1 files changed, 14 insertions, 18 deletions
diff --git a/jsonschema/_utils.py b/jsonschema/_utils.py
index f026c33..6c29dcf 100644
--- a/jsonschema/_utils.py
+++ b/jsonschema/_utils.py
@@ -158,9 +158,9 @@ def ensure_list(thing):
return thing
-def dict_equal(one, two):
+def _mapping_equal(one, two):
"""
- Check if two dicts are the same using `equal`
+ Check if two mappings are equal using the semantics of `equal`.
"""
if len(one.keys()) != len(two.keys()):
return False
@@ -174,9 +174,9 @@ def dict_equal(one, two):
return True
-def list_equal(one, two):
+def _sequence_equal(one, two):
"""
- Check if two lists are the same using `equal`
+ Check if two sequences are equal using the semantics of `equal`.
"""
if len(one) != len(two):
return False
@@ -188,23 +188,19 @@ def list_equal(one, two):
return True
-def is_sequence(instance):
- """
- Checks if an instance is a sequence but not a string
- """
- return isinstance(instance, Sequence) and not isinstance(instance, str)
-
-
def equal(one, two):
"""
- Check if two things are equal, but evade booleans and ints being equal.
- """
- if is_sequence(one) and is_sequence(two):
- return list_equal(one, two)
+ Check if two things are equal evading some Python type hierarchy semantics.
+ Specifically in JSON Schema, evade `bool` inheriting from `int`,
+ recursing into sequences to do the same.
+ """
+ if isinstance(one, str) or isinstance(two, str):
+ return one == two
+ if isinstance(one, Sequence) and isinstance(two, Sequence):
+ return _sequence_equal(one, two)
if isinstance(one, Mapping) and isinstance(two, Mapping):
- return dict_equal(one, two)
-
+ return _mapping_equal(one, two)
return unbool(one) == unbool(two)
@@ -232,7 +228,7 @@ def uniq(container):
sliced = itertools.islice(sort, 1, None)
for i, j in zip(sort, sliced):
- return not list_equal(i, j)
+ return not _sequence_equal(i, j)
except (NotImplementedError, TypeError):
seen = []