Assignability of overloaded implementation to `Proto[**P](Protocol)` is overload-order-sensitive

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

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

評価

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

調査の方向性

myPy Playground にある提供された Python 3.12 の再現コードから始め、Impl と Impl2 が Callback に対してどのようにチェックされるかを比較してください。例を実行し、両方の呼び出しでエラーが発生せず、明らかになった型が一貫したままであることを確認して、期待される結果を検証してください。

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

説明

bug topic-protocols

Bug Report, To Reproduce & Actual Behaviour

When matching a method signature in a protocol with an overloaded implementation, the implementation's overload order appears to matter, even when only one of the overloads match.

In the following example, only the first overload of the implementation is tried for compatibility with the protocol (which looks similar to the issue in #14070). See mypy Playground:

# mypy: disable-error-code=no-overload-impl

from typing import *

_A_contra = TypeVar("_A_contra", bound=Literal["a", "b"], contravariant=True)
_P = ParamSpec("_P")

class Callback(Protocol[_A_contra, _P]):
    def method(self, a: _A_contra, *args: _P.args, **kwargs: _P.kwargs) -> None: ...
    
class Impl:
    @overload
    def method(self, a: Literal["a"], b: int) -> None: ...
    @overload
    def method(self, a: int, b: str) -> None: ...

class Impl2:  # Overload signature order is reversed compared with `Impl`
    @overload
    def method(self, a: int, b: str) -> None: ...
    @overload
    def method(self, a: Literal["a"], b: int) -> None: ...

def accepts_callback(cb: Callback[Literal["a"], _P], *args: _P.args, **kwargs: _P.kwargs) -> int: 
    return 1

a = accepts_callback(Impl(), 1)
b = accepts_callback(Impl2(), 1)  # Error
reveal_type(a)
reveal_type(b)

Expected Behavior

No errors

Your Environment

  • Mypy version used: 1.16.1, master
  • Python version used: 3.12
主要言語
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 を短くまとめたダイジェスト。