This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix client hashing in nested client params case (#373)
- Loading branch information
Showing
5 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"""Utilities for working with AWS services.""" | ||
|
||
from prefect.utilities.collections import visit_collection | ||
|
||
|
||
def hash_collection(collection) -> int: | ||
"""Use visit_collection to transform and hash a collection. | ||
Args: | ||
collection (Any): The collection to hash. | ||
Returns: | ||
int: The hash of the transformed collection. | ||
Example: | ||
```python | ||
from prefect_aws.utilities import hash_collection | ||
hash_collection({"a": 1, "b": 2}) | ||
``` | ||
""" | ||
|
||
def make_hashable(item): | ||
"""Make an item hashable by converting it to a tuple.""" | ||
if isinstance(item, dict): | ||
return tuple(sorted((k, make_hashable(v)) for k, v in item.items())) | ||
elif isinstance(item, list): | ||
return tuple(make_hashable(v) for v in item) | ||
return item | ||
|
||
hashable_collection = visit_collection( | ||
collection, visit_fn=make_hashable, return_data=True | ||
) | ||
return hash(hashable_collection) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import pytest | ||
|
||
from prefect_aws.utilities import hash_collection | ||
|
||
|
||
class TestHashCollection: | ||
def test_simple_dict(self): | ||
simple_dict = {"key1": "value1", "key2": "value2"} | ||
assert hash_collection(simple_dict) == hash_collection( | ||
simple_dict | ||
), "Simple dictionary hashing failed" | ||
|
||
def test_nested_dict(self): | ||
nested_dict = {"key1": {"subkey1": "subvalue1"}, "key2": "value2"} | ||
assert hash_collection(nested_dict) == hash_collection( | ||
nested_dict | ||
), "Nested dictionary hashing failed" | ||
|
||
def test_complex_structure(self): | ||
complex_structure = { | ||
"key1": [1, 2, 3], | ||
"key2": {"subkey1": {"subsubkey1": "value"}}, | ||
} | ||
assert hash_collection(complex_structure) == hash_collection( | ||
complex_structure | ||
), "Complex structure hashing failed" | ||
|
||
def test_unhashable_structure(self): | ||
typically_unhashable_structure = dict(key=dict(subkey=[1, 2, 3])) | ||
with pytest.raises(TypeError): | ||
hash(typically_unhashable_structure) | ||
assert hash_collection(typically_unhashable_structure) == hash_collection( | ||
typically_unhashable_structure | ||
), "Unhashable structure hashing failed after transformation" |