Regression since mypy 1.7 with functions that return a generic protocol
オープン
まだ誰も着手していません。
bug
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
EDIT: I found a simpler example reproducing the regression, see below
The following code used to pass with mypy 1.6.1
from typing import (
Protocol,
TypeVar,
Callable,
Concatenate,
ParamSpec,
reveal_type,
)
X = TypeVar("X")
Y = TypeVar("Y")
P = ParamSpec("P")
I = TypeVar("I", contravariant=True)
O = TypeVar("O", covariant=True)
# A callable converting I to O with parameters P
ConvertorCallable = Callable[Concatenate[I, P], O]
# An object that can convert I to O with parameters P
class ConvertorProtocol(Protocol[I, P, O]):
def convert(self, __source: I, *args: P.args, **kwargs: P.kwargs) -> O:
...
# Decorator to convert a callable to a convertor
def convertor(
func: ConvertorCallable[X, P, Y],
) -> ConvertorProtocol[X, P, Y]:
class Convertor:
def convert(self, source: X, /, *args: P.args, **kwargs: P.kwargs) -> Y:
return func(source, *args, **kwargs)
return Convertor()
# A convertor that converts X to a list of X
@convertor
def as_list(source: X, repeat: int = 1) -> list[X]:
return [source] * repeat
if __name__ == "__main__":
result = as_list.convert(1, repeat=3)
reveal_type(result)
print(result)
However, it no longer passes with mypy 1.7.0 and later:
error: Argument 1 to "convert" of "ConvertorProtocol" has incompatible type "int"; expected Never [arg-type]
Note that this sample works in the pyright playground.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
issue 内の自己完結型の再現例から始め、それを mypy 1.6.1 および 1.7.0 以降で実行してリグレッションを確認してください。ParamSpec、TypeVar、Concatenate が関与するジェネリックな Protocol の推論を追跡してください。完了条件は、例が引数に対して Never を報告しなくなり、リグレッションがカバーされることです。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- devtools
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 38/100