Skip to content

Finals and Literals: Spec contradiction #2351

Description

@srittau

This came up in python/typeshed#16301. I assume this is mostly a clerical/historical error, but let me know if we should follow the whole spec change process.


The typing spec says about Final:

Example:

ID: Final = 1

The typechecker should apply its usual type inference mechanisms to determine the type of ID (here, likely, int).

And then a bit later:

Type checkers should treat uses of a final name that was initialized with a literal as if it was replaced by the literal.

Both sentences are copied directly from PEP 591, which overlapped with the Literals PEP 586.

I assume that we should change the first sentence to "here, likely, Literal[1]" and change the second sentence to be a bit more concrete along these lines:

Type checkers should infer uses of a final name that was initialized with a literal X as Literal[X] if X is an allowed literal type (cf. to the literals section). Otherwise, they should infer the type of the X.

(And maybe extend the example after a bit.)

In fact, this is what the conformance tests already check:

ID2: Final = 1
assert_type(ID2, Literal[1])

(Although the tests could be extended a bit.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions