python / python/mypy

TypeVar declared at class scope with generic bound specialized on an in-scope generic parameter TypeVar doesn't give an error

Aperta
#15,758 4 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug Report

I don't think that TypeVars at class scope have any particular meaning, but if they reject bound=T they ought to also reject bound=G[T] in the same way.

To Reproduce

from typing import TypeVar, Generic

T = TypeVar("T")

class A:
    pass

class B(A):
    pass

class G(Generic[T]):
    pass

anA = TypeVar("anA", bound=A)

class X(Generic[anA]):
    # error, as expected
    # AnInnerA = TypeVar("AnInnerA", bound=anA)
    AnInnerG = TypeVar("AnInnerG", bound=G[anA])

    def method(self, val: AnInnerG) -> None:
        pass

x: X[A] = X()
g: G[A] = G()

x.method(g)

playground

Expected Behavior

I would have expected to see an error on the line where AnInnerG was defined.

Actual Behavior

No error on AnInnerG's definition, but I cannot find any type I can pass to X.method without provoking an error.

main.py:27: error: Value of type variable "AnInnerG" of "method" of "X" cannot be "G[A]"  [type-var]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.4.1
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files): n/a
  • 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 Python fornita con mypy 1.4.1 e --strict, quindi confrontala con il playground collegato. Analizza come vengono convalidati i TypeVars a livello di classe e i bound generici; il lavoro è completato quando la dichiarazione non valida AnInnerG produce la diagnostica prevista invece di fallire solo alla chiamata del metodo.

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

Valutazione

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