Overload ambiguity is ignored in `self`-annotated methods
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- 平均マージ
- 1日 18時間
- マージ済み PR(30日)
- 54
説明
Bug Report
Per the title: mypy does not check for overload ambiguity in self-annotated methods.
Mypy will instead, effectively, ignore self and then return the first matching overload,
thus circumventing the usual Any that is returned when overload ambiguity is involved.
This has lead to a number of false positives in https://github.com/numpy/numpy/issues/20099 and (in part) https://github.com/numpy/numpy/issues/20072.
The conditions for triggering this bug are as following:
- The callable is a method (things work fine for functions).
- The method is overloaded.
selfmust be annotated with a parametrized generic.
To Reproduce
from typing import Any, Generic, overload, TypeVar, TYPE_CHECKING
import numpy as np
_CharType = TypeVar("_CharType", np.str_, np.bytes_)
class CharArray(Generic[_CharType]):
@overload
def strip(self: CharArray[np.str_], chars: str = ...) -> CharArray[np.str_]: ...
@overload
def strip(self: CharArray[np.bytes_], chars: bytes = ...) -> CharArray[np.bytes_]: ...
ar_any: CharArray[Any]
ar_str: CharArray[np.str_]
ar_bytes: CharArray[np.bytes_]
if TYPE_CHECKING:
# The good
reveal_type(ar_str.strip()) # E: Revealed type is "__main__.CharArray[numpy.str_]"
reveal_type(ar_bytes.strip()) # E: Revealed type is "__main__.CharArray[numpy.bytes_]"
# The bad
# "__main__.CharArray[Any]" would be expected here due to overload ambiguity
reveal_type(ar_any.strip()) # E: Revealed type is "__main__.CharArray[numpy.str_]"
# Calling the method directly from the class does do the trick
reveal_type(CharArray.strip(ar_any)) # E: Revealed type is "test.CharArray[Any]"
Your Environment
- Mypy version used: 0.910
- Mypy command-line flags: n.a.
- Mypy configuration options from
mypy.ini(and other config files): n.a. - Python version used: 3.9.6
- Operating system and version: macOS 11.4
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、提供された Python の再現コードを mypy で実行し、オーバーロードされたメソッド呼び出しについて明らかになった型を比較します。self パラメーターがパラメーター化されたジェネリックであるメソッドのオーバーロード解決を追跡します。曖昧な CharArray[Any].strip() 呼び出しが Any を返し、一方で具体的な self 型がそれぞれの固有の結果を保持すれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- numpy, python
- 領域
- compilers, devtools
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100