Skip to content

TypedDict keys(), items(), and values() #7845

Closed
@crusaderky

Description

@crusaderky

mypy 0.750+dev.78f2df9d46198563106dd32cb4494678e97d71eb

mypy thinks that the values of a TypedDict yielded by items() or values() are of type object, instead of the Union of all possible values.
As for the keys yielded by keys() or items(), they are upcast to str whereas a Union of Literal would be more precise.
Finally, keys() and items() are incorrectly reported as an AbstractSet instead of KeysView and ItemsView respectively.

from typing_extensions import TypedDict

class C(TypedDict):
    x: float
    y: int
    z: str


c: C
reveal_type(c.keys())
reveal_type(c.items())
reveal_type(c.values())

Actual output:

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

Expected output:

note: Revealed type is 'typing.KeysView[Literal["x", "y", "z"]]'
note: Revealed type is 'typing.ItemsView[Literal["x", "y", "z"], Union[float, int, str]]'
note: Revealed type is 'typing.ValuesView[Union[float, int, str]]'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions