python / python/mypy

Generics is not supported with some operators

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

Nessuno ha ancora preso questa issue.

bug priority-1-normal topic-type-variables topic-union-types
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

  • Are you reporting a bug, or opening a feature request?
    Bug
  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.

Supporting operators with union does not work with generics.

S = TypeVar('S', bound=Union[int, float])
T = TypeVar('T', bound=Union[int, float])

def some_function(a:S, b:T) -> S:
    // do some operation and returns the type based on first parameter.
     c = a + b
    // if there is an addition involved in the computation, then we would get the below error. 
    // some_computed_variable would be finally computed and has the value based on first parameter type.
    return some_computed_variable

  • What is the actual behavior/output?
    error: Unsupported operand types for + ("S" and "T")

    We can use alias (Union[int, float] as type) instead of generics (S) as a workaround but it loses generic support.

  • What is the behavior/output you expect?
    Should not report error

  • What are the versions of mypy and Python you are using?
    Python: 3.6 mypy: 0.650

  • What are the mypy flags you are using? (For example --strict-optional)
    --ignore-missing-imports

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 riproducendo il caso dell'operatore generico dell'issue con un checkout aggiornato di mypy, quindi esamina la modifica supporting-operators-with-union in PR #5545. Traccia il modo in cui le variabili di tipo con bound vengono verificate per l'addizione e aggiungi test che mostrino che gli operandi generici compatibili vengono accettati senza perdere il tipo restituito basato sul primo parametro.

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.