python / python/mypy

[possibly-undefined] Track conditional assignment

Aperta
#14,310 1 commento 2 reazioni 1 assegnatario Vedi su GitHub

@ilinum ci sta già lavorando.

Dal 17/12/2022.

feature topic-possibly-undefined
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Feature

A common false-positive pattern I've seen for possibly-undefined is separating the conditional assignment and usage. It would be awesome if mypy could track the condition and thus recognize that the usage happens with the same condition.

Not sure if it's possible to implement. Even pyright hasn't done it (yet).

/CC: @ilinum

def some_func() -> bool:
    return True

var = some_func()
other_conditional: bool = True

if var:
    description = "Hallo"

... # some other code, which doesn't modify `var`

if var:  # maybe a bit easier
    print(description)  # Name "description" may be undefined

if var and other_conditional:
    print(description)  # Name "description" may be undefined

This will likely only work for local variables as we can't easily be sure instance attributes aren't changed by the code block in between the definition and usage.

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.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.