python / python/mypy

Analysis time binding of restricted `Generic`s

Aperta
#11,565 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Feature

A generic type defined with reference to a restricted type variable only makes sense when containing a value of type within the restrictions of the type variable.

It would then make sense to assume that when the user does not bind the generic type (that is, they do not provide a type argument), the implied type is the Union of the possible bound types.

To Reproduce

from typing import Any
from typing import AnyStr
from typing import Generic


class Message(Generic[AnyStr]):
    header: AnyStr
    content: AnyStr

    def __init__(self, header: AnyStr, content: AnyStr) -> None:
        self.header = header
        self.content = content

# This feels wrong to me, and I'd rather see this as Union[Message[str], Message[bytes]]
m1: Message = Message(header="foo", content="bar")
reveal_type(m1)  # Message[Any]...

# This is perhaps ok, since the user explicitly passed the `Any` parameter. But if this behavior is kept, 
# one shouldn't be forced to provide a type arg to restricted generics in strict mode, since that 
# would imply having to enumerate the restriction to get the same behavior as above.
m2: Message[Any] = Message(header="foo", content="bar")
reveal_type(m2)  # Message[Any]...

Pitch

Say I have a function that accepts any valid Message as an arg:

def read(msg: Message) -> None:
    msg.content.method_in_neither_str_nor_bytes()

If I annotate it like I just did, I get no hand-holding from the type checker. msg.content is understood as Any, so everything goes. We know, however, that this has to be one of AnyStr. If I want type checker assistance, I now have to manually list the possible Message types, like so:

def read(msg: Union[Message[str], Message[bytes]]) -> None:
    msg.content.method_in_neither_str_nor_bytes()

This is fine when the restriction consists of two types, but it should be easy to imagine examples with a more verbose restriction.

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

L’issue non indica file di implementazione, test o punti di ingresso. Inizia dalla riproduzione e dalla gestione esistente dei generics con vincoli non associati; il lavoro è completo quando viene inferita l’unione dei tipi associati consentiti invece di Any, viene preservato il comportamento di Any fornito esplicitamente e viene aggiunta la copertura per i casi mostrati.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.