Skip to content

Commit

Permalink
validate update to manage dict keys being tuple or frozenset, resolves
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurent Mutricy committed Aug 27, 2024
1 parent 24a3045 commit f1fd834
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
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')): {}}

0 comments on commit f1fd834

Please sign in to comment.