`self` annotation not checked in `__index__`?
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Riproduci l’esempio nell’issue con Python 3.12 e mypy 1.16.1, verificando il tipo rivelato per b * a. Traccia come l’annotazione self di Array.index viene confrontata con SupportsIndex e list.mul. Il lavoro è completato quando l’esempio rivela Array[str] come previsto ed è presente una copertura di regressione.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100