Hacktoberfest 2026: die Issues, die Maintainer für den Oktober markiert haben – offen und einsteigerfreundlich. Hacktoberfest-Issues durchsuchen

Consider loosening restrictions of `Final`

Offen
#920 9 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Anfängerfreundlichkeit
25/100
Issue-Typ
Feature
Klarheit
Größtenteils klar
Aktivitätsstatus
Veraltet
Tech-Stack
python
Bereich
tooling

Rechercherichtung

Es werden keine Dateien oder Tests genannt. Beginne damit, die aktuellen Final-Regeln und die in der Issue zitierten PEP-Passagen zu prüfen, und lies dann den Kommentar-Thread zu früheren Einwänden. Als abgeschlossen gilt die Arbeit, wenn eine klare Entscheidung zu Split-Zuweisungen, Schleifendeklarationen und Annotationen von Funktionsargumenten getroffen und das akzeptierte Verhalten in der relevanten typing-Spezifikation festgehalten wurde.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

topic: feature

I think that Final is a very based idea, but I feel that it's current restrictions make using it painful.

I think that split assignments should be allowed:

def foo(cond: bool) -> None:
    if cond:
        a: Final = Funny("spam")
    else:
        a: Final = Funny("eggs")  # this is currently an error

Most languages allow this pattern to initialize constant variables:

def foo(cond: bool) -> None:
    a: Final[Funny]
    if cond:
        a = Funny("spam")
    else:
        a = Funny("eggs")

I think these are convenient and enables Final to be used in more scenarios, but this is currently an error.
But this is allowed:

def foo(cond: bool) -> None:
    a: Final = Funny("spam") if cond else Funny("eggs")

Another issue I've found is the prohibition of Final declarations within loops:

def foo() -> None:
    for i in [1,2,3]:
        a: Final = i * 2  # currently an error
        a = 10  # error
        print(a)
    print(a)
    a = 10  # error

This is defined in the pep:

Note that a type checker need not allow Final declarations inside loops since the runtime will see multiple assignments to the same variable in subsequent iterations.

I understand that at runtime the same variable is reused for each iteration of the loop, but if you ignore that fact and just look at the semantic meaning of the code, it matches exactly to this Kotlin example:

fun foo() {
    val a = listOf(1, 2, 3).map { i ->
        val a = i
        a = 10  // error
        println(a)
        a
    }.last()
    println(a)
    a = 10  // error
}

Disallowing Final in loops doesn't address any of the motivations listed in the pep, any I can't see any reason why it should be disallowed.

Also, why are Final annotations not allowed on functional arguments?

Final may only be used as the outermost type in assignments or variable annotations. Using it in any other position is an error. In particular, Final can't be used in annotations for function arguments:

Why? This is commonly seen in other languages. In Kotlin, function parameters can only be val(Kotlin's form of Final), in Java a function parameter may be marked with final.

Vorherrschende Sprache
Python
Sterne
1.8k
Forks
302
Ø Merge
23 Std.
Gemergte PRs (30 T.)
8

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

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/typing

Alle Issues in python/typing

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

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