Skip to content

flatten doesn't work with iterables without defined length #462

@ml31415

Description

@ml31415

The documentation of flatten states, that it works with any iterable. A python iterable in general isn't required to have a predefined length. So a call of len(iterable) should be handled with the necessary care. Unfortunately, this doesn't happen in flatten and fails with a TypeError.

Reproducer:

from streamz import Stream
stream = Stream()
lst = stream.flatten().sink_to_list()
gen = (r for r in range(10))
stream.emit(gen)
File ~/.local/lib/python3.10/site-packages/streamz/core.py:1632, in flatten.update(self, x, who, metadata)
   1630 L = []
   1631 for i, item in enumerate(x):
-> 1632     if i == len(x) - 1:
   1633         y = self._emit(item, metadata=metadata)
   1634     else:

TypeError: object of type 'generator' has no len()

The loop should probably just emit the previous item, until it runs in a StopIteration and then emit the last item together with the metadata. Or maybe even just sending the metadata together with the first item?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy