Skip to content

Reimplement zip_broadcast() to fix #561#565

Merged
bbayles merged 4 commits intomore-itertools:masterfrom
kalekundert:fix-strict
Oct 9, 2021
Merged

Reimplement zip_broadcast() to fix #561#565
bbayles merged 4 commits intomore-itertools:masterfrom
kalekundert:fix-strict

Conversation

@kalekundert
Copy link
Copy Markdown
Contributor

Issue reference

Fixes #561

Changes

This implementation delegates the strict=True checks to zip_equal(), rather than making those checks itself. This avoids the need to account for how zip() consumes items.

@kalekundert
Copy link
Copy Markdown
Contributor Author

While trying to get the tests to pass, I ran into a small complexity regarding what kind of exception to raise if the iterables don't match lengths. As mentioned above, the implementation delegates to zip_equal(), which raises UnequalIterablesError. However, for python>=3.10, zip_equal() also emits a DeprecationWarning. To avoid this warning, the implementation delegates directly to zip() if possible. However, zip(strict=True) raises ValueError instead of UnequalIterablesError.

I decided to handle this by updating the documentation to state that zip_broadcast() raises ValueError. UnequalIterablesError is a subclass of ValueError, so this is true for all versions of python. I also note that the specific exception raised in older versions of python is UnequalIterablesError.

I think this approach is consistent with the idea of phasing out zip_equal() for python >=3.10, but it also breaks backwards compatibility very slightly. Let me know if you'd rather just raise UnequalIterablesError for all versions of python.

@bbayles bbayles merged commit bff7cbe into more-itertools:master Oct 9, 2021
@bbayles
Copy link
Copy Markdown
Collaborator

bbayles commented Oct 9, 2021

Thanks for the PR. I merged a combination of your version and mine that passes tests. I also worked around the deprecation warning such that 3.10 can raise UnequalIterablesError without version detection.

@Masynchin Masynchin mentioned this pull request Jul 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants