Repository: dask/dask (Python)

Issue:
Backwards incompatible change to map_partitions in v1.1.0
Easy fix on the user's end, but I didn't see it in the changelog and so I thought worth pointing out :)

In `v1.0.0`, the following code works:

```
import dask.bag as db

def append_str(partition, s):
    return [x + s for x in partition]

mybag = db.from_sequence(["a", "b", "c"])

tupled_map_partitions_result = mybag.map_partitions(append_str, "foo").compute()
print(f"map_partitions result: {tupled_map_partitions_result}")
```

which prints: `map_partitions result: ['afoo', 'bfoo', 'cfoo']`

However in `v1.1.0`, this raises:`TypeError: can only concatenate str (not "itertools.repeat") to str`

The following code works in `v1.1.0` to achieve the same result (1.0.0 would have different behavior)

```
import dask.bag as db

def append_str(partition, strs):
    return [x + s for x, s in zip(partition, strs)]

mybag = db.from_sequence(["a", "b", "c"])

tupled_map_partitions_result = mybag.map_partitions(append_str, "foo").compute()
print(f"map_partitions result: {tupled_map_partitions_result}")
```

I believe this comes from PR #4092, in particular, this line: https://github.com/dask/dask/blame/d63ad2a4a74ff88e6f7b670e9fb5e4bb4c0d667b/dask/bag/core.py#L1954


Discussion:
Thanks for the excellent issue @joegoldbeck (and sorry for the delay in response).

I agree that this seems to be a regression.  Ideally we would be able to pass simple values like "foo" down through `map_partitions` easily.  Do you have any interest in helping to fix this?  I would probably remove the line you mention, replacing with the simpler `args2.append(a)`, and then see if anything breaks.  Presumably I made that change for some reason (no clue at the moment what it was).  The example you provide here would also make for an excellent test to make sure that we don't regress again in the future.
Sure, I'll take a look!



Patch:
diff --git a/dask/bag/core.py b/dask/bag/core.py
index 16551d3f609..04ce3702ae7 100644
--- a/dask/bag/core.py
+++ b/dask/bag/core.py
@@ -1954,10 +1954,10 @@ def map_partitions(func, *args, **kwargs):
             bags.append(a)
             args2.append(a)
         elif isinstance(a, (Item, Delayed)):
-            args2.append((itertools.repeat, a.key))
+            args2.append(a.key)
             dependencies.append(a)
         else:
-            args2.append((itertools.repeat, a))
+            args2.append(a)
 
     bag_kwargs = {}
     other_kwargs = {}


Test patch:
diff --git a/dask/bag/tests/test_bag.py b/dask/bag/tests/test_bag.py
index c6a46be60f1..e2efb20c37c 100644
--- a/dask/bag/tests/test_bag.py
+++ b/dask/bag/tests/test_bag.py
@@ -1316,6 +1316,18 @@ def test_bag_paths():
     os.remove('foo1')
 
 
+def test_map_partitions_arg():
+    def append_str(partition, s):
+        return [x + s for x in partition]
+
+    mybag = db.from_sequence(["a", "b", "c"])
+
+    assert_eq(mybag.map_partitions(append_str, "foo"),
+              ['afoo', 'bfoo', 'cfoo'])
+    assert_eq(mybag.map_partitions(append_str, dask.delayed("foo")),
+              ['afoo', 'bfoo', 'cfoo'])
+
+
 def test_map_keynames():
     b = db.from_sequence([1, 2, 3])
     d = dict(b.map(inc).__dask_graph__())