Skip to content

more_itertools.consume iterator is annotated as object, causing mypy to expect only object type inside #745

@obaltian

Description

@obaltian

more_itertools.consume argument iterator is annotated as Iterable[object], not Iterable[_T].
Because of that, mypy thinks all type variables inside iterator (like _T from itertools.takewhile) must be object, not allowing it's subclasses.

For a solution we should replace iterator: Iterator[object] with iterator: Iterator[_T]. This is enough to fix this case, but I didn't dig if it's OK for other cases, so your thoughts are welcome :)

Here is a bug demonstation:

# test.py
# more-itertools==10.0.0, mypy==1.4.1
import itertools

import more_itertools

more_itertools.consume(
    itertools.takewhile(
        lambda label: label.startswith("VB"),
        ["VBZ", "VBG", "VBN", "NP", "NN"],
    ),
)
mypy test.py
test.py:7: error: "object" has no attribute "startswith"  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

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