annotations on `self` are not understood with `Protocol`s
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
When interpreting a given type as a given Protocol, annotations on self in the Protocol are apparently ignored.
To Reproduce
from __future__ import annotations
from typing import *
#━━━━━━━━━━━━━━━━━#
# EXISTING CODE #
#━━━━━━━━━━━━━━━━━#
class ConcreteClass:
# here 'transform()' is used, but we might use '__neg__()' or
# '__invert__()' in a similar way (so many built-in/third-party
# classes would qualify as 'TransformAware')
def transform(self) -> int:
return id(self)
#━━━━━━━━━━━━━━━━#
# LIBRARY CODE #
#━━━━━━━━━━━━━━━━#
# this can be changed
Ico = TypeVar('Ico', covariant=True) # input, covariant
Oco = TypeVar('Oco', covariant=True) # output, covariant
class TransformAware(Protocol[Ico, Oco]):
def transform(self: Ico) -> Oco: ...
#━━━━━━━━━━━━━━━━━━━━━━━━━#
# DEMONSTRATE PROBLEMS: #
#━━━━━━━━━━━━━━━━━━━━━━━━━#
I = TypeVar('I') # input
O = TypeVar('O') # output
def interpret_transform_aware(_: Type[TransformAware[I, O]]) -> Tuple[I, O]:
i: I
o: O
return i, o
reveal_type(interpret_transform_aware(ConcreteClass))
# actual output:
# note: Revealed type is "Tuple[<nothing>, builtins.int]"
# expected output:
# note: Revealed type is "Tuple[ConcreteClass, int]"
#───────────────#
# MOTIVATION: #
#───────────────#
class TransformDriver(Generic[I, O]):
def __init__(self, klass: Type[TransformAware[I, O]]): ...
def transform_value(self, value: I) -> O:
use_value = cast(TransformAware[I, O], value)
return use_value.transform()
Expected Behavior
(See: inline comments.)
Interpreting ConcreteClass as TransformAware should correctly infer both type variables.
Actual Behavior
(See: inline comments.)
Mypy gives the type of Ico as <nothing>.
Your Environment
- Mypy version used: 1.6.1
- Mypy command-line flags: (none necessary)
- Mypy configuration options from
mypy.ini(and other config files): (none necessary) - Python version used: 3.8, 3.11
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
Inizia con il riproduttore di mypy-play.net e il codice di gestione di Protocol; il payload non indica alcun file del repository né alcun test. Traccia come vengono usate le annotazioni su self nell’interpretazione di ConcreteClass come TransformAware, quindi aggiungi copertura che mostri che entrambe le variabili di tipo vengono inferite come ConcreteClass e int.
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
- 38/100