python / python/mypy

Generic type variable not unified with specific bound type.

Aperta
#15,872 0 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

Bug Report

In g below, argument types of different versions of x.f from different bounds of T cross-contaminate. When T is A, x.f can only accept A = T. Likewise, when T is B, x.f can only accept B = T. Hence the code is correct. However, mypy seems to believe that T can somehow be both A and B at simultaneously.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.11&gist=a92693b3d619e1a11011b86a7cee1ce2

from typing import TypeVar, Generic

class A:
    def f(self, other: A) -> None:
        ...

class B:
    def f(self, other: B) -> None:
        ...

T = TypeVar('T', bound = A | B)

def g(x: T) -> None:
    x.f(x)

Expected Behavior

g typechecked successfully.

Actual Behavior

g fails typechecking:

main.py:14: error: Argument 1 to "f" of "A" has incompatible type "T"; expected "A"  [arg-type]
main.py:14: error: Argument 1 to "f" of "B" has incompatible type "T"; expected "B"  [arg-type]

Your Environment

  • Mypy version used: 1.5.0
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.11

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 eseguendo la riproduzione collegata di mypy-play con mypy 1.5.0 e i flag predefiniti, quindi confronta gli errori segnalati con il controllo dei tipi riuscito previsto per g. Traccia la gestione della variabile di tipo alla base della chiamata x.f(x); il lavoro è completo quando questo esempio supera il controllo dei tipi senza gli errori di argomenti incompatibili di A e B, con l’aggiunta della copertura di regressione.

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

Valutazione

Stack tecnologico
python
Ambito
compilers, tooling
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.