Skip to content

zip_broadcast(strict=True) fails if the leftmost iterables are exactly 1 item longer than the others #561

@kalekundert

Description

@kalekundert

Here's the bug:

>>> import more_itertools as mi
>>> mi.__version__
'8.10.0'
>>> list(mi.zip_broadcast([1, 2], [3], strict=True))
[(1, 3)]
>>> list(mi.zip_broadcast([1, 2], [3], [4], strict=True))
[(1, 3, 4)]
>>> list(mi.zip_broadcast([1, 2], [3, 4], [5], strict=True))
[(1, 3, 5)]

Here are some cases where the expected exception is raised:

>>> list(mi.zip_broadcast([1], [2, 3], strict=True))
Traceback (most recent call last):
  ...
more_itertools.more.UnequalIterablesError: Iterables have different lengths
>>> list(mi.zip_broadcast([1], [2, 3], [4], strict=True))
Traceback (most recent call last):
  ...
more_itertools.more.UnequalIterablesError: Iterables have different lengths
>>> list(mi.zip_broadcast([1], [2], [3, 4], strict=True))
Traceback (most recent call last):
  ...
more_itertools.more.UnequalIterablesError: Iterables have different lengths
>>> list(mi.zip_broadcast([1], [2, 3], [4, 5], strict=True))
Traceback (most recent call last):
  ...
more_itertools.more.UnequalIterablesError: Iterables have different lengths
>>> list(mi.zip_broadcast([1, 2], [3], [4, 5], strict=True))
Traceback (most recent call last):
  ...
more_itertools.more.UnequalIterablesError: Iterables have different lengths

I think the problem is that zip() consumes extra elements from the iterables given to it, so checking which iterables have been exhausted after calling zip() is unreliable. I don't see an easy way to fix this, but the implementation I provided in #543 does handle these cases correctly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugpr-welcomeWe are open to PRs that fix this issue - leave a note if you're working on it!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions