`self` annotation not checked in `__index__`?
オープン
まだ誰も着手していません。
bug
topic-calls
topic-protocols
topic-self-types
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
Bug Report
To Reproduce
from __future__ import annotations
from typing import Callable, TypeVar, Any, Protocol, Self, Any
T = TypeVar('T', covariant=True)
class Array(Protocol[T]):
def __index__(self: Array[int], /) -> int: ...
def dtype(self) -> T: ...
def __rmul__(self, other: Any) -> Self: ...
def func(a: Array[str], b: list[bool]) -> None:
result = b * a
reveal_type(result)
https://mypy-play.net/?mypy=latest&python=3.12&gist=596075fa56c1bd714ec44b224f5ed97c
Expected Behavior
t.py:13: note: Revealed type is "Array[str]"
(which is what Pyright does)
This is because list.__mul__ is defined as
def __mul__(self, value: SupportsIndex, /) -> list[_T]: ...
and SupportsIndex checks for __index__(self) -> int, and if value is Array[str], then Array.__index__ is not matched
Actual Behavior
t.py:13: note: Revealed type is "builtins.list[builtins.bool]"
Your Environment
- Mypy version used: 1.16.1
- Mypy command-line flags:
- Mypy configuration options from
mypy.ini(and other config files): - Python version used: 3.12
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
Python 3.12 と mypy 1.16.1 で issue の例を再現し、b * a に対して明らかになる型を確認します。Array.index の self アノテーションが SupportsIndex および list.mul とどのように照合されるかを追跡します。例で期待どおり Array[str] が明らかになり、回帰テストのカバレッジがあることを完了条件とします。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- devtools
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100