python / python/mypy

Type issues with Callables

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

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

false-positive feature priority-1-normal
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

  • Are you reporting a bug, or opening a feature request?
    bug

  • Please insert below the code you are checking with mypy

from typing import Optional, Iterable, TypeVar, Callable

T = TypeVar('T')


def is_even(n: int) -> bool:
    return n % 2 == 0


# equivalent to https://ruby-doc.org/core-2.5.0/Enumerable.html#method-i-detect
def detect(function: Callable[[T], bool],
           iterable: Iterable[T]) -> Optional[T]:
    for element in iterable:
        if function(element):
            return element
    return None


numbers = [1, 2, 3, 4, 5]

# using a lambda
print(detect(lambda n: n % 2 == 0, numbers))

# using a function
print(detect(is_even, numbers))

  • What is the actual behavior/output?
$ mypy detect.py 
detect.py:22: error: Unsupported operand types for % ("object" and "int")
detect.py:25: error: Argument 1 to "detect" has incompatible type "Callable[[int], bool]"; expected "Callable[[object], bool]"
Found 2 errors in 1 file (checked 1 source file)
  • What is the behavior/output you expect?
    I was expecting the types to be inferred and the above code to typecheck.

  • What are the versions of mypy and Python you are using?

$ mypy --version
mypy 0.770
$ python --version
Python 3.7.7
  • Do you see the same issue after installing mypy from Git master?
    yes
$ mypy detect.py
detect.py:22: error: Unsupported operand types for % ("object" and "int")
detect.py:25: error: Argument 1 to "detect" has incompatible type "Callable[[int], bool]"; expected "Callable[[object], bool]"
Found 2 errors in 1 file (checked 1 source file)
$ mypy --version
mypy 0.770+dev.22c67daac7d2b28f33d4e9c7bee4f1a06e61a3e5
  • What are the mypy flags you are using? (For example --strict-optional)
    None.

  • If mypy crashed with a traceback, please paste the full traceback below.
    It didn't crash

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

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

はじめの一歩

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

調査の方向性

まず、提供された detect.py の再現コードを報告された mypy のバージョンで実行し、Callable と TypeVar の推論が lambda および is_even 引数をどのように処理するかを調べます。関連する型チェックの動作を追跡し、適切な場合はリグレッションテストのカバレッジを追加します。例が報告された 2 つのエラーなしで型チェックを通過すれば完了です。

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

評価

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

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

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