python / python/mypy

Weird error with callable accepting non-positional arguments

オープン
#9,810 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

bug
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

issue の再現コードから始め、identity callable が位置引数を受け取る場合と、非位置引数を受け取る場合で、mypy が functools.reduce をどのように処理するかを比較します。提示された例がエラーなしで受け入れられ、報告された callable の型付け動作が維持されていることを確認して、修正を検証します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。