python / python/mypy

Type inference in high level function

Open
#8,117 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug false-positive priority-1-normal
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

For fully valid code got:
error: Argument 1 to "map" has incompatible type overloaded function; expected "Callable[[Iterator[_T], int, None], Iterator[_T]].

In foo.py:

from typing import Generic, Iterable, Iterator, Sequence, TypeVar
_T = TypeVar('_T')

class Windowed(Generic[_T]):
    def __init__(self, it: Iterable[_T], size: int) -> None:
        self.it = it
        self.size = size

    def __iter__(self) -> Iterator[Sequence[_T]]:
        iters = tee(self.it, self.size)
        slices = map(islice, iters, count(0), repeat(None))
        return zip(*slices)

However islice has overload:

def islice(iterable: Iterable[_T], start: Optional[int], stop: Optional[int],
           step: Optional[int] = ...) -> Iterator[_T]: ...

Using mypy 0.750 from PyPI, on python 3.7.5. Running it via mypy foo.py

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the diagnostic from foo.py with mypy 0.750 on Python 3.7.5, focusing on the map(islice, iters, count(0), repeat(None)) call and the islice overload shown in the issue. Trace the type inference for the overloaded function and verify that the valid example no longer reports an incompatible argument error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.