Skip to content

incorrect inferred type accessing typed dict with static keys #19961

@yabirgb

Description

@yabirgb

Bug Report

mypy does not infer the correct type of a dictionary when constructing a subset of a TypedDict using a constant subset of keys.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.12&gist=82fc1eb24628e1ba2aed8294b81e2912

from typing import TypedDict, reveal_type


class MyDict(TypedDict):
    key_0: bool
    key_1: int
    key_2: bool


limits: MyDict = {
    'key_0': True,
    'key_1': 1,
    'key_2': False
}

def test_func() -> dict[str, bool]:
    result = {
        label: limits.get(label, False)
        for label in ('key_0', 'key_2')
    }
    reveal_type(limits.get('key_0', False))
    reveal_type(result)
    return result

test_func()

Expected Behavior

In the example above, I expect mypy to accept the return type since both key_0 and key_2 are annotated as bool

Actual Behavior

mypy complains about the return type with

Incompatible return value type (got "dict[str, object]", expected "dict[str, bool]")  [return-value]

After mentioning it in my team chat @LefterisJP noticed that this version works https://mypy-play.net/?mypy=latest&python=3.12&gist=63bece9a183493b170f6f55527fbd7d3

from typing import TypedDict, reveal_type, Literal


class MyDict(TypedDict):
    key_0: bool
    key_1: int
    key_2: bool


limits: MyDict = {
    'key_0': True,
    'key_1': 1,
    'key_2': False
}

def test_func() -> dict[str, bool]:
    keys: tuple[Literal['key_0', 'key_2'], ...] = ('key_0', 'key_2')
    result: dict[str, bool] = {
        label: limits.get(label, False)
        for label in keys
    }
    reveal_type(limits.get('key_0', False))
    reveal_type(result)
    return result

test_func()

In this case, because the keys are explicitly typed as Literals, mypy infers the correct type.
This suggests that mypy is not analyzing constant tuples deeply enough to recognize the accessed keys.

Your Environment

  • Mypy version used: mypy 1.18.2 (compiled: yes)
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): I can reproduce it in an environment without any extra configuration.
  • Python version used: 3.11.13

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions