From 87581f4cd92fba0050e746f5753fe3d2685112f0 Mon Sep 17 00:00:00 2001
From: Min RK <benjaminrk@gmail.com>
Date: Wed, 21 Jul 2021 14:24:53 +0200
Subject: [PATCH] fixes for empty maps

empty maps never worked with block=False, which was never tested!
---
 ipyparallel/client/asyncresult.py    | 11 ++++++++++-
 ipyparallel/client/map.py            |  4 ++++
 ipyparallel/client/remotefunction.py | 12 +++++++++++-
 3 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/ipyparallel/client/asyncresult.py b/ipyparallel/client/asyncresult.py
index 79b71e016..d264b5b17 100644
--- a/ipyparallel/client/asyncresult.py
+++ b/ipyparallel/client/asyncresult.py
@@ -84,7 +84,7 @@ def __init__(
 
         self._return_exceptions = return_exceptions
 
-        if isinstance(children[0], string_types):
+        if children and isinstance(children[0], string_types):
             self.msg_ids = children
             self._children = []
         else:
@@ -96,6 +96,15 @@ def __init__(
         self._targets = targets
         self.owner = owner
 
+        if not children:
+            # empty result!
+            self._ready = True
+            self._success = True
+            f = Future()
+            f.set_result([])
+            self._resolve_result(f)
+            return
+
         self._ready = False
         self._ready_event = Event()
         self._output_ready = False
diff --git a/ipyparallel/client/map.py b/ipyparallel/client/map.py
index 6d26f3390..e03468bbc 100644
--- a/ipyparallel/client/map.py
+++ b/ipyparallel/client/map.py
@@ -66,6 +66,8 @@ def joinPartitions(self, listOfPartitions):
         return self.concatenate(listOfPartitions)
 
     def concatenate(self, listOfPartitions):
+        if len(listOfPartitions) == 0:
+            return listOfPartitions
         testObject = listOfPartitions[0]
         # First see if we have a known array type
         if is_array(testObject):
@@ -88,6 +90,8 @@ def getPartition(self, seq, p, q, n=None):
         return seq[p:n:q]
 
     def joinPartitions(self, listOfPartitions):
+        if len(listOfPartitions) == 0:
+            return listOfPartitions
         testObject = listOfPartitions[0]
         # First see if we have a known array type
         if is_array(testObject):
diff --git a/ipyparallel/client/remotefunction.py b/ipyparallel/client/remotefunction.py
index e95e15d62..f3c8a19f3 100644
--- a/ipyparallel/client/remotefunction.py
+++ b/ipyparallel/client/remotefunction.py
@@ -244,7 +244,17 @@ def __call__(self, *sequences, **kwargs):
 
         if maxlen == 0:
             # nothing to iterate over
-            return []
+            if self.block:
+                return []
+            else:
+                return AsyncMapResult(
+                    self.view.client,
+                    [],
+                    self.mapObject,
+                    fname=getname(self.func),
+                    ordered=self.ordered,
+                    return_exceptions=self.return_exceptions,
+                )
 
         # check that the length of sequences match
         if not _mapping and minlen != maxlen: