Skip to content

Function with generic class and generic value arguments are not inferred when generic is a TypedDict #19201

Open
@frankie567

Description

@frankie567

Bug Report

When defining a function expecting a generic class and a generic value — tied to the same TypeVar —, mypy is not able to infer the type of the argument if the generic type is a TypedDict.

It works well for scalar types, if the variable is explicitly defined as the typed dict type, or even more surprising, if the first generic argument is binded via a functools.partial.

To Reproduce

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

import typing
import functools

T = typing.TypeVar("T")


class A(typing.Generic[T]): ...


def f(a: A[T], t: T) -> None: ...


class D(typing.TypedDict):
    x: int


a_typed_dict = A[D]()
f(a_typed_dict, {"x": 1})  # error: Cannot infer type argument 1 of "f"  [misc]

Expected Behavior

The t argument is correctly validated as a D typed dict.

Actual Behavior

The following error message is returned:

error: Cannot infer type argument 1 of "f"  [misc]

Your Environment

  • Mypy version used: 1.15
  • Python version used: 3.12

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @frankie567@sterliakov

      Issue actions

        Function with generic class and generic value arguments are not inferred when generic is a TypedDict · Issue #19201 · python/mypy