Skip to content

Weird error with callable accepting non-positional arguments #9810

@sk-

Description

@sk-

Bug Report

When answering this question on StackOverflow, I found a weird issue with mypy, which does not happen with either pyre or pytype.

Furthermore, by changing the identity function below to only accept positional arguments the problem is fixed.

To Reproduce
Check the following code with mypy

import functools
from typing import Iterable, TypeVar, Callable

T1 = TypeVar('T1')
T2 = TypeVar('T2')

def chain(
        functions: Iterable[Callable[[T1], T1]]
    ) -> Callable[[T1], T1]:

    def compose(
            f: Callable[[T1], T1],
            g: Callable[[T1], T1]
        ) -> Callable[[T1], T1]:
        def h(x: T1) -> T1:
            return g(f(x))
        return h

    def identity(x: T1) -> T1:
        return x

    return functools.reduce(compose, functions, identity)

def add_one(x):
    return x + 1

def mul_two(x):
    return x * 2

assert chain([add_one, mul_two, mul_two, add_one])(7) == 33```

Expected Behavior

I expected no errors

Actual Behavior

src/test.py:23: error: Argument 1 to "reduce" has incompatible type "Callable[[Arg(Callable[[T1], T1], 'f'), Arg(Callable[[T1], T1], 'g')], Callable[[T1], T1]]"; expected "Callable[[Callable[[Arg(T1, 'x')], T1], Callable[[T1], T1]], Callable[[Arg(T1, 'x')], T1]]"

Your Environment

  • Mypy version used: mypy 0.790
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.8.6
  • Operating system and version: MacOs Catalina

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions