Skip to content
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

validate update to manage dict keys being tuple or frozenset #327

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion schema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,11 @@ def validate(self, data: Any, **kwargs: Dict[str, Any]) -> Any:
for skey in sorted_skeys:
svalue = s[skey]
try:
nkey = Schema(skey, error=e).validate(key, **kwargs)
if isinstance(skey, (tuple, frozenset)):
Schema(hash(skey), error=e).validate(hash(key), **kwargs)
nkey = skey
else:
nkey = Schema(skey, error=e).validate(key, **kwargs)
except SchemaError:
pass
else:
Expand Down
22 changes: 22 additions & 0 deletions test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1985,3 +1985,25 @@ def test_callable_error():
except SchemaError as ex:
e = ex
assert e.errors == ["This is the error message"]


def test_tuple_key_of_dict():
# this is a simplified regression test of the bug in github issue #312
assert Schema({('map_point', 'to', 'map_polygon'): {}}).validate(
{('map_point', 'to', 'map_polygon'): {}}
) == {('map_point', 'to', 'map_polygon'): {}}
with SE:
assert Schema({('map_point', 'to', 'map_polygon'): {}}).validate(
{('map_polygon', 'to', 'map_polygon'): {}}
) == {('map_polygon', 'to', 'map_polygon'): {}}


def test_frozenset_key_of_dict():
# this is a simplified regression test of the bug in github issue #312
assert Schema({frozenset(('map_point', 'to', 'map_polygon')): {}}).validate(
{frozenset(('map_point', 'to', 'map_polygon')): {}}
) == {frozenset(('map_point', 'to', 'map_polygon')): {}}
with SE:
assert Schema({frozenset(('map_point', 'to', 'map_polygon')): {}}).validate(
{frozenset(('map_polygon', 'to', 'map_polygon')): {}}
) == {frozenset(('map_polygon', 'to', 'map_polygon')): {}}