Skip to content

Invalid type inference for generic typed dicts #18804

Open
@rnv812

Description

@rnv812

Bug Report

When we try to assert the type of an object obtained from get with default value from generic object bound to typed dict, then mypy shows next error:

error: Incompatible types in assignment (expression has type "object", variable has type "int")  [assignment]

To Reproduce

from typing import NotRequired, TypedDict, TypeVar


class A(TypedDict):
    i: NotRequired[int]


ParamsT = TypeVar('ParamsT', bound=A)


def foo(params: ParamsT) -> None:
    i: int = params.get('i', 0)


foo(A())

Gist: https://gist.github.com/mypy-play/effc3f0d297540be8ca1bad7b08c5902
Play: https://mypy-play.net/?mypy=latest&python=3.12&gist=effc3f0d297540be8ca1bad7b08c5902

Expected Behavior

It is expected that result of expression params.get('i', 0) would be of type int since typed dict attribute is annotated as <int> and the default value type is <int> as well.

Actual Behavior

Mypy reports that result of expression params.get('i', 0) has type <object>.

Your Environment

  • Mypy version used: mypy 1.15.0 (compiled: yes)
  • Python version used: 3.13

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions