Skip to content

itertools is no longer copyable/picklable #12042

Closed
dask/distributed
#9169

Description

@QuLogic

Describe the issue:

As of Python 3.12, pickling an itertools object has been deprecated, and in Python 3.14, it will no longer work. This causes a failure in test_multiple_repartition_partition_size:

___________________ test_multiple_repartition_partition_size ___________________
[gw2] linux -- Python 3.14.0 /usr/bin/python3
    def test_multiple_repartition_partition_size():
        b = db.from_sequence(range(1, 100), npartitions=1)
        total_mem = sum(b.map_partitions(total_mem_usage).compute())
    
        c = b.repartition(partition_size=(total_mem // 2))
        assert c.npartitions >= 2
        assert_eq(b, c)
    
>       d = c.repartition(partition_size=(total_mem // 5))
dask/bag/tests/test_bag.py:1305: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
dask/bag/core.py:1694: in repartition
    return repartition_size(self, partition_size)
dask/bag/core.py:2666: in repartition_size
    mem_usages = bag.map_partitions(total_mem_usage).compute()
../BUILDROOT/usr/lib/python3.14/site-packages/dask/base.py:373: in compute
    (result,) = compute(self, traverse=False, **kwargs)
../BUILDROOT/usr/lib/python3.14/site-packages/dask/base.py:681: in compute
    results = schedule(expr, keys, **kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
exc = TypeError(TypeError("cannot pickle 'itertools.chain' object"), '  File "/builddir/build/BUILD/python-dask-2025.7.0-bui...           ~~~~~~~~^^^^^^^^^^^\n  File "/usr/lib64/python3.14/copy.py", line 146, in deepcopy\n    rv = reductor(4)\n')
tb = '  File "/builddir/build/BUILD/python-dask-2025.7.0-build/BUILDROOT/usr/lib/python3.14/site-packages/dask/local.py", l...            ~~~~~~~~^^^^^^^^^^^\n  File "/usr/lib64/python3.14/copy.py", line 146, in deepcopy\n    rv = reductor(4)\n'
    def reraise(exc, tb=None):
        exc = remote_exception(exc, tb)
>       raise exc
E       dask.multiprocessing.TypeError: cannot pickle 'itertools.chain' object
E       
E       Traceback
E       ---------
E         File "/builddir/build/BUILD/python-dask-2025.7.0-build/BUILDROOT/usr/lib/python3.14/site-packages/dask/local.py", line 258, in execute_task
E           result = task(data)
E         File "/builddir/build/BUILD/python-dask-2025.7.0-build/BUILDROOT/usr/lib/python3.14/site-packages/dask/_task_spec.py", line 759, in __call__
E           return self.func(*new_argspec)
E                  ~~~~~~~~~^^^^^^^^^^^^^^
E         File "/builddir/build/BUILD/python-dask-2025.7.0-build/BUILDROOT/usr/lib/python3.14/site-packages/dask/_task_spec.py", line 199, in _execute_subgraph
E           res = execute_graph(final, keys=[outkey])
E         File "/builddir/build/BUILD/python-dask-2025.7.0-build/BUILDROOT/usr/lib/python3.14/site-packages/dask/_task_spec.py", line 1078, in execute_graph
E           cache[key] = node(cache)
E                        ~~~~^^^^^^^
E         File "/builddir/build/BUILD/python-dask-2025.7.0-build/BUILDROOT/usr/lib/python3.14/site-packages/dask/_task_spec.py", line 759, in __call__
E           return self.func(*new_argspec)
E                  ~~~~~~~~~^^^^^^^^^^^^^^
E         File "/builddir/build/BUILD/python-dask-2025.7.0-build/BUILDROOT/usr/lib/python3.14/site-packages/dask/bag/core.py", line 2655, in total_mem_usage
E           partition = reify(deepcopy(partition))
E                             ~~~~~~~~^^^^^^^^^^^
E         File "/usr/lib64/python3.14/copy.py", line 146, in deepcopy
E           rv = reductor(4)
../BUILDROOT/usr/lib/python3.14/site-packages/dask/multiprocessing.py:112: TypeError

Minimal Complete Verifiable Example:

pytest

Environment:

  • Dask version: 2025.7.0
  • Python version: 3.14
  • Operating System: Fedora Rawhide
  • Install method (conda, pip, source): source

Activity

  1. added
    needs triage
    on Aug 5, 2025
  2. QuLogic commented on Aug 5, 2025

    @QuLogic
    ContributorAuthor
  3. changed the title [-]itertools is no longer picklable[/-] [+]itertools is no longer copyable/picklable[/+] on Aug 5, 2025
  4. jrbourbeau commented on Aug 5, 2025

    @jrbourbeau
    Member

    Thanks for flagging @QuLogic. I've been able to reproduce with

    uv run --python 3.14 --with "pytest" --with "pytest-cov" python -m pytest dask/bag/tests/test_bag.py::test_multiple_repartition_partition_size
    

    But haven't yet been able to track down where the itertools.chain object is coming from. I think a reasonable thing to do here would be to replace problematic itertools routines with custom utility functions.

  5. added
    bug
    and removed
    needs triage
    on Aug 5, 2025
  6. mrocklin commented on Aug 5, 2025

    @mrocklin
    Member

    toolz.concat is probably a serializable drop-in replacement FWIW.

  7. jrbourbeau commented on Aug 5, 2025

    @jrbourbeau
    Member

    Ah, nice. Even better. It looks like toolz.concat uses itertools internally

    In [1]: import toolz
    
    In [2]: toolz.concat([[], [1], [2, 3]])
    Out[2]: <itertools.chain at 0x10647eef0>

    so we'll want to make sure that whatever we shift to avoids the need to serialize itertools objects. But yeah, if we can pull in existing utilities elsewhere (like from toolz) instead of rolling our own, that'd be great.

  8. mrocklin commented on Aug 5, 2025

    @mrocklin
    Member

    Ah, maybe maybe toolz isn't appropriate then. Sorry if my comment led us astray.

  9. QuLogic commented on Aug 14, 2025

    @QuLogic
    ContributorAuthor

    But haven't yet been able to track down where the itertools.chain object is coming from.

    I could not find anything either; I think it must be coming from toolz, actually. Since it occurs for something repartitioned, my guess might be this one?

    dask/dask/bag/core.py

    Lines 2722 to 2747 in d0a9d15

    def _repartition_from_boundaries(bag, new_partitions_boundaries, new_name):
    if not isinstance(new_partitions_boundaries, list):
    new_partitions_boundaries = list(new_partitions_boundaries)
    if new_partitions_boundaries[0] > 0:
    new_partitions_boundaries.insert(0, 0)
    if new_partitions_boundaries[-1] < bag.npartitions:
    new_partitions_boundaries.append(bag.npartitions)
    num_new_partitions = len(new_partitions_boundaries) - 1
    dsk = {}
    for new_partition_index in range(num_new_partitions):
    value = (
    list,
    (
    toolz.concat,
    [
    (bag.name, old_partition_index)
    for old_partition_index in range(
    new_partitions_boundaries[new_partition_index],
    new_partitions_boundaries[new_partition_index + 1],
    )
    ],
    ),
    )
    dsk[new_name, new_partition_index] = value
    graph = HighLevelGraph.from_collections(new_name, dsk, dependencies=[bag])
    return Bag(graph, name=new_name, npartitions=num_new_partitions)

  10. added
    needs attention
    on Sep 15, 2025
  11. added
    p1
    p2
    and removed
    p1
    on Dec 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugneeds attentionp2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions