From a7320135da5829cacd1b4e6c4cb0d675f8acea85 Mon Sep 17 00:00:00 2001 From: oppnc <129483880+oppnc@users.noreply.github.com> Date: Tue, 26 May 2026 05:11:29 +0800 Subject: [PATCH] Fix TypedDict tuple context for Collection --- mypy/types.py | 1 + test-data/unit/check-typeddict.test | 17 +++++++++++++++++ test-data/unit/fixtures/typing-typeddict.pyi | 2 ++ 3 files changed, 20 insertions(+) diff --git a/mypy/types.py b/mypy/types.py index 5a05962dc4802..9781861edec18 100644 --- a/mypy/types.py +++ b/mypy/types.py @@ -163,6 +163,7 @@ "builtins.tuple", "typing.Iterable", "typing.Container", + "typing.Collection", "typing.Sequence", "typing.Reversible", ) diff --git a/test-data/unit/check-typeddict.test b/test-data/unit/check-typeddict.test index 17a1fea22ef04..25f7ea1065434 100644 --- a/test-data/unit/check-typeddict.test +++ b/test-data/unit/check-typeddict.test @@ -4601,6 +4601,23 @@ inputs: Sequence[Component] = [{ [builtins fixtures/dict.pyi] [typing fixtures/typing-typeddict.pyi] +[case testTypedDictTupleExpressionWithCollectionContext] +import collections.abc +from typing import Collection, TypedDict + +class Item(TypedDict): + x: int + +def take(items: Collection[Item]) -> None: ... +def take_abc(items: collections.abc.Collection[Item]) -> None: ... + +take(({"x": 1}, {"x": 2})) +take_abc(({"x": 1}, {"x": 2})) +items: Collection[Item] = ({"x": 1}, {"x": 2}) +abc_items: collections.abc.Collection[Item] = ({"x": 1}, {"x": 2}) +[builtins fixtures/dict.pyi] +[typing fixtures/typing-typeddict.pyi] + [case testTypedDictAssignableToWiderContext] from typing import TypedDict, Union diff --git a/test-data/unit/fixtures/typing-typeddict.pyi b/test-data/unit/fixtures/typing-typeddict.pyi index 0bc5637b32708..0f1439d21c5ba 100644 --- a/test-data/unit/fixtures/typing-typeddict.pyi +++ b/test-data/unit/fixtures/typing-typeddict.pyi @@ -47,6 +47,8 @@ class Iterable(Protocol[T_co]): class Iterator(Iterable[T_co], Protocol): def __next__(self) -> T_co: pass +class Collection(Iterable[T_co]): pass + class Sequence(Iterable[T_co]): def __getitem__(self, n: Any) -> T_co: pass # type: ignore[explicit-any]