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

Remove safe dict copy #2388

Merged
merged 2 commits into from
Aug 5, 2024
Merged
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
26 changes: 2 additions & 24 deletions src/quacc/utils/dicts.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def recursive_dict_merge(
old_dict = dicts[0]
for i in range(len(dicts) - 1):
merged = _recursive_dict_pair_merge(old_dict, dicts[i + 1], verbose=verbose)
old_dict = safe_dict_copy(merged)
old_dict = deepcopy(merged)
return remove_dict_entries(merged, remove_trigger=remove_trigger)


Expand Down Expand Up @@ -94,8 +94,7 @@ def _recursive_dict_pair_merge(
"""
dict1 = dict1 or ({} if dict1 is None else dict1.__class__())
dict2 = dict2 or ({} if dict2 is None else dict2.__class__())
merged = safe_dict_copy(dict1)

merged = deepcopy(dict1)
for key, value in dict2.items():
if key in merged:
if isinstance(merged[key], MutableMapping) and isinstance(
Expand All @@ -114,27 +113,6 @@ def _recursive_dict_pair_merge(
return merged


def safe_dict_copy(d: MutableMapping[str, Any] | dict[str, Any]) -> dict:
"""
Safely copy a dictionary to account for deepcopy errors.

Parameters
----------
d
Dictionary to copy

Returns
-------
dict
Copied dictionary
"""

try:
return deepcopy(dict(d))
except Exception:
return dict(d).copy()


def remove_dict_entries(
start_dict: MutableMapping[str, Any], remove_trigger: Any
) -> MutableMapping[str, Any]:
Expand Down
Loading