python / python/typing

Consider loosening restrictions of `Final`

Open
#920 9 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

topic: feature
Dominant language
Python
Stars
1.8k
Forks
302
Avg merge
23h
Merged PRs (30d)
8

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No files or tests are named. Start by reviewing the current Final rules and the PEP passages quoted in the issue, then read the comment thread for prior objections. Done means reaching a clear decision on split assignments, loop declarations, and function-argument annotations and recording the accepted behavior in the relevant typing specification.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.