python / python/mypy

Forward-referencing type application with type variable in a value expression in stubs emits confusing error

Aperta
#19,601 0 commenti 0 reazioni 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, To Reproduce, & Actual Behaviour

In .pyi stub files, the spec mandates type-checkers to recognise forward references in annotation expressions without string-quoting. It is however silent about value expressions, but in mypy I've used symbols defined later in a stub file in a value context before without any issues, except recently when hitting the following example:

# stub.pyi
from typing_extensions import Generic, TypeVar

R = TypeVar("R")
T = TypeVar("T")

class A(Generic[R]):
    var = B[R]()  # E: "R" is a type variable and only valid in type context

class B(Generic[T]): ...

Expected Behavior

Either:

  1. no errors, or
  2. mypy doesn't support forward references in value contexts for stub files, and this emits a used-before-def error at B[R]().

Your Environment

  • Mypy version used: 1.17
  • Python version used: 3.11, 3.12

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 riproduttore stub.pyi e verifica come mypy gestisce i riferimenti in avanti nelle espressioni di valore nei file stub. Confronta la diagnostica attuale con i due risultati attesi descritti nell’issue; il lavoro è completato quando il comportamento viene accettato oppure segnalato come used-before-def in B[R]().

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

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.