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

Allow for overrides in deep_dict_merge #6715

Open
hannes-ucsc opened this issue Nov 19, 2024 · 0 comments
Open

Allow for overrides in deep_dict_merge #6715

hannes-ucsc opened this issue Nov 19, 2024 · 0 comments
Assignees
Labels
orange [process] Done by the Azul team

Comments

@hannes-ucsc
Copy link
Member

Index: src/azul/collections.py
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/azul/collections.py b/src/azul/collections.py
--- a/src/azul/collections.py	(revision 3c97698e6421dd74eda0b9c2b031825ecdd51254)
+++ b/src/azul/collections.py	(date 1731976825362)
@@ -85,20 +85,27 @@
     {}
     """
 
-    def __new__(cls, *dicts: Mapping) -> dict:
-        return cls.from_iterable(dicts)
+    def __new__(cls, *dicts: Mapping, override: bool | None = None) -> dict:
+        return cls.from_iterable(dicts, override=override)
 
     @classmethod
-    def from_iterable(cls, dicts: Iterable[Mapping], /) -> dict:
+    def from_iterable(cls, dicts: Iterable[Mapping], /, override: bool | None = None) -> dict:
         merged = {}
         for m in dicts:
             for k, v2 in m.items():
                 v1 = merged.setdefault(k, v2)
                 if v1 != v2:
                     if isinstance(v1, Mapping) and isinstance(v2, Mapping):
-                        merged[k] = deep_dict_merge(v1, v2)
+                        merged[k] = deep_dict_merge(v1, v2, override=override)
                     else:
-                        raise ValueError(f'{v1!r} != {v2!r}')
+                        if override is True:
+                            merged[k] = v2
+                        elif override is False:
+                            pass
+                        elif override is None:
+                            raise ValueError(f'{v1!r} != {v2!r}')
+                        else:
+                            raise ValueError('Expected boolean or None', override)
         return merged
 
 

Needs doctests and doc updates.

Also consider eliminating dict_merge and replacing the sole call site with one to deep_dict_merge. Lastly, consider renaming deep_dict_merge to dict_merge.

@hannes-ucsc hannes-ucsc added the orange [process] Done by the Azul team label Nov 19, 2024
@hannes-ucsc hannes-ucsc changed the title deep_dict_merge improvements Allow for overrides in deep_dict_merge Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
orange [process] Done by the Azul team
Projects
None yet
Development

No branches or pull requests

1 participant