Skip to content

Type specification of zip_broadcast too narrow #882

@nathanielmanistaatgoogle

Description

The type specification of zip_broadcast is too narrow; it disallows zip_broadcast calls with iterables of different element types.

Looking at zip_broadcast's own example usage, if we run mypy 1.11.0 on

import more_itertools

iterable_1 = [1, 2, 3]
iterable_2 = ['a', 'b', 'c']
scalar = '_'
list(more_itertools.zip_broadcast(iterable_1, iterable_2, scalar))

list(more_itertools.zip_broadcast('abc', 0, 'xyz', scalar_types=None))

we get an output of

sample.py:6: error: Argument 1 to "zip_broadcast" has incompatible type "list[int]"; expected "str | Iterable[str]"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

Reviewing the discussion in 525, 526, and 556, it looks likely that if a bunch of @overloads were tossed into the .pyi content it would improve the common cases of just a few iterables, but that a robust solution may only come with variadic generics.

Metadata

Metadata

Assignees

No one assigned

    Labels

    pr-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