Skip to content

Teach unbool about deeply nested objects/arrays #697

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
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
34 changes: 12 additions & 22 deletions jsonschema/_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import itertools
import json
import pkgutil
import re
Expand Down Expand Up @@ -175,38 +174,29 @@ def equal(one, two):
def unbool(element, true=object(), false=object()):
"""
A hack to make True and 1 and False and 0 unique for ``uniq``.

Recurses through an arbitrarily deep object and unique-ifies True/1 and
False/0. Also converts arrays/objects into immutable/hashable alternatives.
We have to copy the items anyway to avoid unexpected mutations, so we might
as well make the imminent equality check simpler.
"""

if element is True:
return true
elif element is False:
return false
elif isinstance(element, list) or isinstance(element, tuple):
return tuple(map(unbool, element))
elif isinstance(element, dict):
# Convert dicts to frozensets of 2-tuples. For equality checks, that's
# a reasonable hashable alternative to dicts.
return frozenset(map(unbool, element.items()))
return element


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.
"""

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:
return False
seen.append(e)
return True
return len(set(unbool(i) for i in container)) == len(container)
80 changes: 0 additions & 80 deletions jsonschema/tests/test_jsonschema_test_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,6 @@ def narrow_unicode_build(test): # pragma: no cover
subject="host-name",
description="ends with hyphen",
)(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)
),
)

Expand Down Expand Up @@ -161,26 +141,6 @@ def narrow_unicode_build(test): # pragma: no cover
subject="hostname",
description="ends with hyphen",
)(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)
),
)

Expand Down Expand Up @@ -234,26 +194,6 @@ def narrow_unicode_build(test): # pragma: no cover
subject="hostname",
description="ends with hyphen",
)(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)
),
)

Expand Down Expand Up @@ -327,26 +267,6 @@ def narrow_unicode_build(test): # pragma: no cover
"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)
),
)

Expand Down