python / python/mypy

Non-optional send types and the Generator.send() parameter

Aperta
#11,023 1 commento 6 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-runtime-semantics
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

Python doesn’t allow you to send a non-None value to a just-started generator, but if the send type of a generator is not annotated Optional then .send(None) can’t be used.

You could just use next() at the start, but a situation can be created where Python and Mypy don’t agree on a type error. Pyright behaves similarly.

I’m not sure what the solution is here. This issue might require discussion.

To Reproduce
$ cat file1.py
from collections.abc import Generator

def g() -> Generator[int, int, None]:
    sent = 0
    while True:
        sent = yield sent

gen = g()
print(gen.send(0))  # <--
print(gen.send(1))
print(gen.send(2))
$ mypy --strict file1.py
Success: no issues found in 1 source file
$ python3 file1.py
Traceback (most recent call last):
  File "/Users/danpro/Desktop/fold/file1.py", line 10, in <module>
    print(gen.send(0))  # <--
TypeError: can't send non-None value to a just-started generator
$ 
$ 
$ cat file2.py
from collections.abc import Generator

def g() -> Generator[int, int, None]:
    sent = 0
    while True:
        sent = yield sent

gen = g()
print(gen.send(None))  # <--
print(gen.send(1))
print(gen.send(2))
$ mypy --strict file2.py
file2.py:10: error: Argument 1 to "send" of "Generator" has incompatible type "None"; expected "int"
Found 1 error in 1 file (checked 1 source file)
$ python3 file2.py
0
1
2
Expected Behavior

A TypeError should not occur at runtime when Mypy reports successful type checking.

Actual Behavior

A TypeError occurs even though Mypy reports successful type checking.

My Environment
  • Mypy version used: 0.910
  • Python version used: 3.9.6
  • Pyright version used: 1.1.163

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con il comportamento di tipizzazione di Generator.send() descritto nelle due riproduzioni file1.py e file2.py, confrontando i risultati di mypy con il TypeError a runtime di Python. Esamina come il tipo send di Generator gestisce il valore None iniziale e considera l'interazione con il comportamento comparabile di Pyright. Il lavoro è completo quando il type checker non accetta più codice che può fallire immediatamente a runtime, con il comportamento previsto stabilito per entrambi gli esempi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.