python / python/mypy

`mypy` does not infer correctly with a `__class__` override

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

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

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

説明

Bug Report

mypy does not infer correctly with a __class__ override.

To Reproduce

https://mypy-play.net/?gist=9e3ffebda54d02c5e68f45fe7cd33eb0

from typing import Any, Protocol, overload, reveal_type
from collections.abc import Iterator, Sequence
from typing_extensions import TypeVar

_T_co = TypeVar("_T_co", covariant=True)

class ReducedCovariantList(Protocol[_T_co]):
    @property
    def __class__(self) -> type[list[Any]]: ...
    def __iter__(self) -> Iterator[_T_co]: ...

@overload
def foo(x: ReducedCovariantList[str]) -> str: ...
@overload
def foo(x: Sequence[int]) -> int: ...

# ty gives Unknown; mypy gives str; pyright and pyrefly give int
reveal_type(foo([1]))
# if we remove the __class__ override, both ty and mypy give int

Expected Behaviour

reveal_type gives int

Actual Behavior

reveal_type gives str

Your Environment

  • Mypy version used: 2.3.0
  • Mypy command-line flags: -
  • Mypy configuration options from mypy.ini (and other config files): -
  • Python version used: 3.12

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

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

はじめの一歩

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

調査の方向性

issue にリンクされている mypy-play の再現例を実行し、reveal_type(foo([1])) の結果を期待される int と比較します。Protocol が __class__ プロパティを定義している場合に、オーバーロード解決が ReducedCovariantList をどのように処理するかを追跡します。例が int を推論し、回帰テストが override のケースをカバーすれば完了です。

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

評価

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

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

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