Skip to content

Fixes unique and equal checks #820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 72 additions & 18 deletions jsonschema/_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections.abc import MutableMapping
from urllib.parse import urlsplit
import collections
import itertools
import json
import pkgutil
Expand Down Expand Up @@ -158,10 +159,64 @@ def ensure_list(thing):
return thing


def dict_equal(one, two):
"""
Check if two dicts are the same using `equal`
"""
if len(one.keys()) != len(two.keys()):
return False

for key in one:
if key not in two:
return False
if not equal(one[key], two[key]):
return False

return True


def list_equal(one, two):
"""
Check if two lists are the same using `equal`
"""
if len(one) != len(two):
return False

for i in range(0, len(one)):
if not equal(one[i], two[i]):
return False

return True


def is_sequence(instance):
"""
Checks if an instance is a sequence but not a string
"""
return isinstance(
instance, collections.Sequence
) and not isinstance(
instance, str
)


def is_mapping(instance):
"""
Checks if an instance is a mapping
"""
return isinstance(instance, collections.Mapping)


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)

if is_mapping(one) and is_mapping(two):
return dict_equal(one, two)

return unbool(one) == unbool(two)


Expand All @@ -181,25 +236,24 @@ def uniq(container):
"""
Check if all of a container's elements are unique.
Successively tries first to rely that the elements are hashable, then
falls back on them being sortable, and finally falls back on brute
force.
Successively tries first to rely that the elements are being sortable
and finally falls back on brute force.
"""

try:
return len(set(unbool(i) for i in container)) == len(container)
except TypeError:
try:
sort = sorted(unbool(i) for i in container)
sliced = itertools.islice(sort, 1, None)
for i, j in zip(sort, sliced):
if i == j:
return False
except (NotImplementedError, TypeError):
seen = []
for e in container:
e = unbool(e)
if e in seen:
sort = sorted(unbool(i) for i in container)
sliced = itertools.islice(sort, 1, None)

for i, j in zip(sort, sliced):
return not list_equal(i, j)

except (NotImplementedError, TypeError):
seen = []
for e in container:
e = unbool(e)

for i in seen:
if equal(i, e):
return False
seen.append(e)

seen.append(e)
return True
160 changes: 0 additions & 160 deletions jsonschema/tests/test_jsonschema_test_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,36 +145,6 @@ def leap_second(test):
"$ref prevents a sibling $id from changing the base uri"
),
)(test)
or skip(
message=bug(686),
subject="uniqueItems",
description="[0] and [false] are unique",
)(test)
or skip(
message=bug(686),
subject="uniqueItems",
description="[1] and [true] are unique",
)(test)
or skip(
message=bug(686),
subject="uniqueItems",
description="nested [0] and [false] are unique",
)(test)
or skip(
message=bug(686),
subject="uniqueItems",
description="nested [1] and [true] are unique",
)(test)
or skip(
message=bug(686),
subject="uniqueItems",
description='{"a": false} and {"a": 0} are unique',
)(test)
or skip(
message=bug(686),
subject="uniqueItems",
description='{"a": true} and {"a": 1} are unique',
)(test)
),
)

Expand Down Expand Up @@ -240,36 +210,6 @@ def leap_second(test):
subject="refRemote",
case_description="base URI change - change folder in subschema",
)(test)
or skip(
message=bug(686),
subject="uniqueItems",
description="[0] and [false] are unique",
)(test)
or skip(
message=bug(686),
subject="uniqueItems",
description="[1] and [true] are unique",
)(test)
or skip(
message=bug(686),
subject="uniqueItems",
description="nested [0] and [false] are unique",
)(test)
or skip(
message=bug(686),
subject="uniqueItems",
description="nested [1] and [true] are unique",
)(test)
or skip(
message=bug(686),
subject="uniqueItems",
description='{"a": false} and {"a": 0} are unique',
)(test)
or skip(
message=bug(686),
subject="uniqueItems",
description='{"a": true} and {"a": 1} are unique',
)(test)
),
)

Expand Down Expand Up @@ -351,56 +291,6 @@ def leap_second(test):
subject="refRemote",
case_description="base URI change - change folder in subschema",
)(test)
or skip(
message=bug(686),
subject="uniqueItems",
description="[0] and [false] are unique",
)(test)
or skip(
message=bug(686),
subject="uniqueItems",
description="[1] and [true] are unique",
)(test)
or skip(
message=bug(686),
subject="uniqueItems",
description="nested [0] and [false] are unique",
)(test)
or skip(
message=bug(686),
subject="uniqueItems",
description="nested [1] and [true] are unique",
)(test)
or skip(
message=bug(686),
subject="uniqueItems",
description='{"a": false} and {"a": 0} are unique',
)(test)
or skip(
message=bug(686),
subject="uniqueItems",
description='{"a": true} and {"a": 1} are unique',
)(test)
or skip(
message=bug(686),
subject="const",
case_description="const with [false] does not match [0]",
)(test)
or skip(
message=bug(686),
subject="const",
case_description="const with [true] does not match [1]",
)(test)
or skip(
message=bug(686),
subject="const",
case_description='const with {"a": false} does not match {"a": 0}',
)(test)
or skip(
message=bug(686),
subject="const",
case_description='const with {"a": true} does not match {"a": 1}',
)(test)
),
)

Expand Down Expand Up @@ -506,55 +396,5 @@ def leap_second(test):
"validation of binary-encoded media type documents"
),
)(test)
or skip(
message=bug(686),
subject="uniqueItems",
description="[0] and [false] are unique",
)(test)
or skip(
message=bug(686),
subject="uniqueItems",
description="[1] and [true] are unique",
)(test)
or skip(
message=bug(686),
subject="uniqueItems",
description="nested [0] and [false] are unique",
)(test)
or skip(
message=bug(686),
subject="uniqueItems",
description="nested [1] and [true] are unique",
)(test)
or skip(
message=bug(686),
subject="uniqueItems",
description='{"a": false} and {"a": 0} are unique',
)(test)
or skip(
message=bug(686),
subject="uniqueItems",
description='{"a": true} and {"a": 1} are unique',
)(test)
or skip(
message=bug(686),
subject="const",
case_description="const with [false] does not match [0]",
)(test)
or skip(
message=bug(686),
subject="const",
case_description="const with [true] does not match [1]",
)(test)
or skip(
message=bug(686),
subject="const",
case_description='const with {"a": false} does not match {"a": 0}',
)(test)
or skip(
message=bug(686),
subject="const",
case_description='const with {"a": true} does not match {"a": 1}',
)(test)
),
)
Loading