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

Prevent arguments to submission variations being changed by deepcopy-ing them. #107

Merged
merged 1 commit into from
Nov 9, 2023
Merged
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
2 changes: 1 addition & 1 deletion alea/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ def convert_variations(variations: dict, iteration) -> list:
if not isinstance(v, list):
raise ValueError(f"variations {k} must be a list, not {v} with {type(v)}")
variations[k] = expand_grid_dict(v)
result = [dict(zip(variations, t)) for t in iteration(*variations.values())]
result = [dict(zip(variations, deepcopy(t))) for t in iteration(*variations.values())]
if result:
return result
else:
Expand Down
16 changes: 16 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
clip_limits,
can_expand_grid,
expand_grid_dict,
convert_to_vary,
deterministic_hash,
)

Expand Down Expand Up @@ -74,6 +75,21 @@ def test_expand_grid_dict(self):
],
)

def test_convert_to_vary(self):
"""Test of the convert_to_zip function."""
varied = convert_to_vary({"a": [1, 2], "b": [{"c": 3}, {"c": 4}]})
self.assertNotEqual(id(varied[0]["b"]), id(varied[2]["b"]))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only checks the basic sanity and not the issue where if you change one you dont change the other, right? I wonder if I would change this to "

btest = dict(c=4)
varied = convert_to_vary({"a": [1, 2], "b": [{"c": 3}, btest]})
btest["c"] = 5
self assertEqual(varied[1]["b"], dict(c=4))

?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.assertNotEqual(id( is checking the address of dictionaries, not just a sanity check.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right, thanks, @dachengx

self.assertNotEqual(id(varied[1]["b"]), id(varied[3]["b"]))
self.assertEqual(
varied,
[
{"a": 1, "b": {"c": 3}},
{"a": 1, "b": {"c": 4}},
{"a": 2, "b": {"c": 3}},
{"a": 2, "b": {"c": 4}},
],
)

def test_deterministic_hash(self):
"""Test of the deterministic_hash function."""
self.assertEqual(deterministic_hash([0, 1]), "si3ifpvg2u")
Expand Down
Loading