Repository: AgnostiqHQ/covalent
Pull request #1448 (2022-12-12 17:41:09)

Set any lattice metadata not explicitly set by the user
<!--
⚠️ Your pull request title should be short detailed and understandable for all.
⚠️ Increment the version number in the /VERSION file. If this is a bugfix, increment the patch version (the rightmost number), for example 0.18.2 becomes 0.18.3. If it's a new feature, increment the minor version (the middle number), for example 0.18.2 becomes 0.19.0.
⚠️ Add a note to /CHANGELOG.md with your version number and the date, summarizing the changes.
⚠️ If your pull request fixes an open issue, please link to the issue.
⚠️ Rebase the latest changes from the develop branch. Assuming origin points to https://github.com/AgnostiqHQ/covalent, you should run git rebase origin/develop.
-->

- [x] I have added the tests to cover my changes.
- [x] I have updated the documentation and CHANGELOG accordingly.
- [x] I have read the CONTRIBUTING document.



Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 26856659..fa1c58b7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ### Fixed
 
+- Build graph now sets all unset lattice constraints from defaults
 - Fixed all failing functional tests
 
 ### Changed
diff --git a/covalent/_workflow/lattice.py b/covalent/_workflow/lattice.py
index be4dd304..66e176db 100644
--- a/covalent/_workflow/lattice.py
+++ b/covalent/_workflow/lattice.py
@@ -221,6 +221,17 @@ class Lattice:
         new_args = [v.get_deserialized() for _, v in named_args.items()]
         new_kwargs = {k: v.get_deserialized() for k, v in named_kwargs.items()}
 
+        # Set any lattice metadata not explicitly set by the user
+        constraint_names = {"executor", "workflow_executor", "deps", "call_before", "call_after"}
+        new_metadata = {}
+        for name in constraint_names:
+            if not self.metadata[name]:
+                new_metadata[name] = DEFAULT_METADATA_VALUES[name]
+        new_metadata = encode_metadata(new_metadata)
+
+        for k, v in new_metadata.items():
+            self.metadata[k] = v
+
         with redirect_stdout(open(os.devnull, "w")):
             with active_lattice_manager.claim(self):
                 try:
@@ -231,15 +242,6 @@ class Lattice:
                     )
                     raise
 
-        # Set workflow executor if not set by user
-        postprocessor = self.metadata["workflow_executor"]
-        if not postprocessor:
-            pp = {}
-            pp["workflow_executor"] = DEFAULT_METADATA_VALUES["workflow_executor"]
-            pp = encode_metadata(pp)
-            self.metadata["workflow_executor"] = pp["workflow_executor"]
-            self.metadata["workflow_executor_data"] = pp["workflow_executor_data"]
-
     def draw(self, *args, **kwargs) -> None:
         """
         Generate lattice graph and display in UI taking into account passed in


Test patch:
diff --git a/tests/covalent_tests/workflow/lattice_test.py b/tests/covalent_tests/workflow/lattice_test.py
index a19d3d05..348b5c17 100644
--- a/tests/covalent_tests/workflow/lattice_test.py
+++ b/tests/covalent_tests/workflow/lattice_test.py
@@ -64,3 +64,25 @@ def test_lattice_workflow_executor_settings():
     assert workflow.metadata["workflow_executor"] == DEFAULT_METADATA_VALUES["workflow_executor"]
     workflow_2.build_graph(1)
     assert workflow_2.metadata["workflow_executor"] == "custom_postprocessor"
+
+
+def test_lattice_executor_settings():
+    """Check that executor is set from defaults if not set explicitly"""
+
+    @ct.electron
+    def task(x):
+        return x
+
+    @ct.lattice
+    def workflow(x):
+        return task(x)
+
+    @ct.lattice(executor="custom_executor")
+    def workflow_2(x):
+        return task(x)
+
+    assert not workflow.metadata["executor"]
+    workflow.build_graph(1)
+    assert workflow.metadata["executor"] == DEFAULT_METADATA_VALUES["executor"]
+    workflow_2.build_graph(1)
+    assert workflow_2.metadata["executor"] == "custom_executor"