Skip to content

final TypedDict #7981

Open
Open
@crusaderky

Description

@crusaderky

Reopened from #7845 as requested

mypy seems to ignore the @final decorator when it is applied to a TypedDict. This issue becomes material when one invokes the items() or values() methods, or passes a generic string to get(); then the output will be incorrectly upcast to object since mypy thinks it may be dealing with a subclass.

from typing_extensions import final, TypedDict

@final
class Point(TypedDict):
    x: float
    y: float
    z: float


p: Point
x: str
reveal_type(p.values())
reveal_type(p.items())
reveal_type(p.get(x, 0.0))

Output:

12: note: Revealed type is 'typing.ValuesView[builtins.object*]'
13: note: Revealed type is 'typing.AbstractSet[Tuple[builtins.str*, builtins.object*]]'
14: note: Revealed type is 'builtins.object*'

Expected output:

12: note: Revealed type is 'typing.ValuesView[builtins.float*]'
13: note: Revealed type is 'typing.ItemsView[builtins.str*, builtins.float*]'
14: note: Revealed type is 'builtins.float*'

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions