`mypy` widens multiple type parameters even when only the first one is relevant

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

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

評価

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

調査の方向性

まず、リンクされた mypy-play の再現を実行し、推論された型と assert_type の期待値を比較します。次に、add 呼び出しに関係するオーバーロードのマッチングと型パラメーターの拡張ロジックを追跡し、共有されている EA パラメーターを保持したまま、一致しないパラメーターだけが拡張されることを示す回帰テストを追加します。

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

説明

bug topic-inference

Bug Report

When there are two type parameters to widen, mypy widens both to Any, even when the matched overloads agree on the second parameter and only actually disagree on the first.

Related to #19952

To Reproduce

https://mypy-play.net/?gist=6ab5724e788384125e85da02ae0386d3

from collections.abc import Sequence
from typing import Any, Generic, Protocol, assert_type, overload

from typing_extensions import TypeVar

S1 = TypeVar("S1")


class EA: ...


A = TypeVar("A", bound=EA, default=EA, covariant=True)
S2 = TypeVar("S2", bound=EA)
S2_contra = TypeVar("S2_contra", bound=EA, contravariant=True)


class Supports_ProtoAdd(Protocol[S2_contra, S2]):
    def _proto_add(self, other: S2_contra, /) -> "Ser2[S2, EA]": ...


class Ser2(Generic[S1, A]):
    def _proto_add(self, other: Any, /) -> "Ser2[Any, EA]":
        raise NotImplementedError

    @overload
    def __add__(
        self: Supports_ProtoAdd[S2_contra, S2],
        other: "S2_contra | Sequence[S2_contra]",
    ) -> "Ser2[S2]": ...
    @overload
    def __add__(self: "Ser2[bool, EA]", other: Sequence[Any]) -> "Ser2[int]": ...
    def __add__(self, other: Any) -> Any:
        raise NotImplementedError


def f(a2: "Ser2[Any, EA]", seq: "Sequence[Any]") -> None:
    assert_type(a2 + seq, "Ser2[Any, EA]")
    # error: Expression is of type "Ser2[Any, Any]", not "Ser2[Any, EA]"  [assert-type]

Expected Behaviour

Error free, as is the case for pyright, pyrefly and ty

Actual Behaviour

main.py:24: error: Expression is of type "Ser2[Any, Any]", not "Ser2[bool, EA]"  [assert-type]
Found 1 error in 1 file (checked 1 source file)

Your Environment*

mypy 2.3.1 (playground)

主要言語
Python
スター
20.6k
フォーク
3.3k
平均マージ
1日 18時間
マージ済み PR(30日)
54

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

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

はじめの一歩

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

python/mypy のほかの issue

python/mypy の issue をすべて見る

似ている issue

Python の issue をもっと見る

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

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