[Generics] Multiple callable arguments to function generate incompatible type error

Open
#3,568 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

Research direction

Start by reproducing the supplied generic and Callable example with the stated mypy command, comparing the two-argument and one-argument signatures. Trace the type-checking path for callable argument inference; done means the two-argument form no longer reports the misleading incompatible-type error while the behavior remains covered by a regression test.

Written by the indexing model from the issue text.

Description

from typing import TypeVar, Generic, Callable

A = TypeVar('A')
B = TypeVar('B')
E = TypeVar('E', bound=BaseException)


class Child(Generic[A]):
    def __init__(self, x):
        # type: (E) -> None
        self.x = x

    def fun(self, s, f):
        # type: (Callable[[A], Child[B]], Callable[[E], Child[B]]) -> Child[B]
        return f(self.x)  # <----- error here
test_py.py:15: error: Argument 1 has incompatible type "E"; expected "E"

Kind of misleading, and I think it is a bug.
Interesting thing is that when you remove the 's' argument and it's part in type comment so it becomes like this :

    def fun(self, f):
        # type: (Callable[[E], Child[B]]) -> Child[B]
        return f(self.x)

then there is no error.

command: <file_path> --strict-optional --warn-no-return --warn-redundant-casts --check-untyped-defs --disallow-untyped-defs --py2
mypy version: 0.511
os: Ubuntu 16.10

Dominant language
Python
Stars
20.6k
Forks
3.3k
Avg merge
1d 18h
Merged PRs (30d)
54

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.

More from python/mypy

All issues in python/mypy

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.