python / python/mypy

New semantic analyzer: some assignments aren't recognized as types if incomplete

Aperta
#6,985 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

false-positive priority-1-normal semantic-analyzer
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

In the following test case the forward reference to In in the class body doesn't work:

[case testNewAnalyzerNamedTupleCallNested]
from typing import NamedTuple
o: C.Out
i: C.In
reveal_type(o)  # E: Revealed type is 'Tuple[Tuple[builtins.str, __main__.C.Other, fallback=__main__.C.In], __main__.C.Other, fallback=__main__.C.Out]'
reveal_type(o.x)  # E: Revealed type is 'Tuple[builtins.str, __main__.C.Other, fallback=__main__.C.In]'
reveal_type(o.y)  # E: Revealed type is '__main__.C.Other'
reveal_type(o.x.t)  # E: Revealed type is '__main__.C.Other'
reveal_type(i.t)  # E: Revealed type is '__main__.C.Other'

class C:
    Out = NamedTuple('Out', [('x', In), ('y', Other)])   # 'In' undefined  <<--- should be ok?
    In = NamedTuple('In', [('s', str), ('t', Other)])
    class Other: pass

The root cause is that if the rvalue of an assignment is incomplete, we add a PlaceholderNode that doesn't have becomes_typeinfo set. This may also affect other things such as assignment-based TypedDicts, type aliases, type variables and NewTypes (haven't checked).

Not sure what would be the best way to fix this. One idea would be to treat every placeholder as a possible type, at least if it's generated from an incomplete assignment. A second idea would be to recognize possible types by looking at an incomplete rvalue, but this doesn't seem always possible.

This is follow-up to #6983.

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 il caso di test fornito per testNewAnalyzerNamedTupleCallNested e verifica in che modo l’assegnazione incompleta crea un PlaceholderNode. Controlla se lo stesso comportamento interessa i TypedDict basati su assegnazioni, gli alias di tipo, le variabili di tipo o i NewTypes, come indicato nell’issue. Il lavoro è completato quando il riferimento in avanti viene risolto e le aspettative reveal_type elencate vengono soddisfatte senza introdurre regressioni nei casi correlati.

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

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.