Consider loosening restrictions of `Final`
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 1.8k
- Fork
- 302
- Merge medio
- 23h
- PR unite (30g)
- 8
Descrizione
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?
Finalmay only be used as the outermost type in assignments or variable annotations. Using it in any other position is an error. In particular,Finalcan'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.
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Non vengono nominati file né test. Inizia esaminando le regole attuali di Final e i passaggi dei PEP citati nell’issue, quindi leggi il thread dei commenti per conoscere le obiezioni precedenti. Il lavoro è completato quando è stata presa una decisione chiara sulle assegnazioni separate, le dichiarazioni dei cicli e le annotazioni degli argomenti delle funzioni, e il comportamento accettato è stato registrato nella specifica typing pertinente.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- tooling
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100