python / python/mypy

Regression since mypy 1.7 with functions that return a generic protocol

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

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

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]

Try it in the playground.

Note that this sample works in the pyright playground.

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

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

はじめの一歩

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

調査の方向性

issue 内の自己完結型の再現例から始め、それを mypy 1.6.1 および 1.7.0 以降で実行してリグレッションを確認してください。ParamSpec、TypeVar、Concatenate が関与するジェネリックな Protocol の推論を追跡してください。完了条件は、例が引数に対して Never を報告しなくなり、リグレッションがカバーされることです。

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

評価

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

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

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