python / python/mypy

Unexpected order dependence when assigning to multiple targets

Aperta
#19,553 0 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug Report

When assigning an empty generic type to both a typed target and a new not-yet-typed target, typed targets to the left of the not-yet-typed target do not appear to be considered when determining whether or not the not-yet-typed target needs an explicit type annotation.

To Reproduce

typed_list: list[int]

untyped_target = typed_list = [] # All good

typed_list = another_untyped_target = [] # var-annotated error

https://mypy-play.net/?mypy=latest&python=3.12&gist=7d4ec0816e1ea43a3bb6def3f55b3296

The same problem occurs when the typed target is a structured target like self.attr_name or container[key].

Expected Behavior

When assigning to both a structured target and a new local variable name, I prefer to put the structured target first.
I expected to be able to do that without having to put an explicit annotation on the local variable name.

Actual Behavior

Mypy reports the following error when the untyped target is to the right of

error: Need type annotation for "another_untyped_target" (hint: "another_untyped_target: list[<type>] = ...")  [var-annotated]

Your Environment

Local environment where the issue was originally noted:

  • Mypy version used: 1.16.1
  • Mypy command-line flags: --strict
  • Python version used: 3.11

Simplified example comes from the MyPy Playground link above (Python 3.12, MyPy 1.17.0)

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

Riproduci gli esempi sull’ordine delle assegnazioni nel caso collegato di mypy-playground usando mypy 1.16.1 o versione successiva con --strict. Traccia il modo in cui le assegnazioni a più destinazioni inferiscono i tipi per le destinazioni locali e strutturate; il lavoro è completato quando la destinazione non tipizzata viene accettata se la destinazione tipizzata compare per prima, senza introdurre regressioni nell’ordine opposto.

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
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.