ashb commented on code in PR #22679:
URL: https://github.com/apache/airflow/pull/22679#discussion_r841731148


##########
airflow/models/mappedoperator.py:
##########
@@ -516,6 +515,34 @@ def _get_map_lengths(self, run_id: str, *, session: 
Session) -> Dict[str, int]:
 
         return map_lengths
 
+    def get_mapped_tis_to_create(self, run_id: str, *, mappings=True) -> Any:
+        """Creates a list of TaskInstances to create for the mapped task"""
+        from airflow.models.taskinstance import TaskInstance
+        from airflow.models.xcom_arg import XComArg
+
+        mapped_kwargs = self._get_expansion_kwargs()
+        map_lengths = [len(v) for _, v in mapped_kwargs.items() if not 
isinstance(v, XComArg)]
+        total_length = sum(map_lengths)
+        indexes_to_map = range(total_length)
+        if mappings:
+            tis_mappings: List[Dict[str, Any]] = []
+            if total_length == 0:
+                task_instance = TaskInstance.insert_mapping(run_id, self, 
map_index=-1)
+                return [task_instance]
+            for map_index in indexes_to_map:
+                ti_map: Dict[str, Any] = TaskInstance.insert_mapping(run_id, 
self, map_index=map_index)
+                tis_mappings.extend([ti_map])
+            return tis_mappings
+        else:
+            if total_length == 0:
+                task_instance = TaskInstance(self, run_id=run_id, map_index=-1)
+                return [task_instance]
+            tis: List[TaskInstance] = []
+            for map_index in indexes_to_map:
+                ti: TaskInstance = TaskInstance(self, run_id=run_id, 
map_index=map_index)
+                tis.extend([ti])
+            return tis

Review Comment:
   Having to pass `mappings` in to here makes this function a lot more complex 
than it needs to be, and makes it very specific to the one place where it is 
currently called.
   
   Instead I would have this function return the number of mapped tis to create 
(or maybe a cached property instead of a function?)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to