Skip to content

Commit

Permalink
#686: Fixes unique and equal checks
Browse files Browse the repository at this point in the history
  • Loading branch information
nezhar authored and Julian committed Jul 9, 2021
1 parent 31426f6 commit 36810ff
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 162 deletions.
48 changes: 46 additions & 2 deletions jsonschema/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,46 @@ 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 equal(one, two):
"""
Check if two things are equal, but evade booleans and ints being equal.
"""
if isinstance(one, list) and isinstance(two, list):
return list_equal(one, two)

if isinstance(one, dict) and isinstance(two, dict):
return dict_equal(one, two)

return unbool(one) == unbool(two)


Expand Down Expand Up @@ -192,14 +228,22 @@ def uniq(container):
try:
sort = sorted(unbool(i) for i in container)
sliced = itertools.islice(sort, 1, None)

for i, j in zip(sort, sliced):
if isinstance(i, list) and isinstance(j, list):
return not list_equal(i, j)
if i == j:
return False

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

for i in seen:
if isinstance(i, dict) and isinstance(e, dict):
if dict_equal(i, e):
return False

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)
),
)

0 comments on commit 36810ff

Please sign in to comment.