Covariant type variable error in nested function
オープン
まだ誰も着手していません。
bug
false-positive
priority-1-normal
topic-type-variables
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
I believe the below code should be allowed:
from typing import *
T_co = TypeVar('T_co', covariant=True)
class Col(Generic[T_co]):
def __init__(self, items: Iterable[T_co]) -> None:
self.items = items
def any(self, pred: Callable[[T_co], bool]) -> bool:
return any(map(pred, self.items))
def any_zero(self, fn: Callable[[T_co], int]) -> bool:
def pred(x: T_co) -> bool: # error: Cannot use type variable as a parameter
return fn(x) == 0
return self.any(pred)
I believe the purpose of this error is to disallow breaking variance, e.g.:
def unsafe(self) -> Callable[[T_co], int]:
def fn(t: T_co) -> int:
return id(t)
return fn
However, restricting variance on nested functions has both false positives (first example), and false negatives:
class Unsafe(Generic[T_co]):
def __init__(self, fn: Callable[[T_co], int]) -> None:
self.fn = fn
def get_fn(self) -> Callable[[T_co], int]:
return self.fn
The above code has no error, but is not type safe. I believe the proper way to check for this is to look at the signatures of each exposed method and verify that the type parameter is not used with the wrong parity.
mypy 0.740, python 3.7.5
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、mypy 0.740 でネストされた関数の例を再現し、報告された変性エラーを安全でない公開メソッドの例と比較します。ネストされた関数と公開メソッドシグネチャに対する変性チェックを追跡します。安全な例が受け入れられ、安全でないケースが偽陰性を導入せずに拒否されれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 25/100