[possibly-undefined] Track conditional assignment

Offen
#14,310 1 Kommentar 2 Reaktionen 1 zugewiesene Person Auf GitHub ansehen

@ilinum arbeitet bereits daran.

Seit 17.12.2022.

Bewertung

Dieses Issue wurde noch nicht bewertet.

Beschreibung

feature topic-possibly-undefined

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.

Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
Ø Merge
1 T. 18 Std.
Gemergte PRs (30 T.)
54

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus python/mypy

Alle Issues in python/mypy

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.