diff --git a/third-party/thrift/src/thrift/lib/python/mutable_containers.pyi b/third-party/thrift/src/thrift/lib/python/mutable_containers.pyi index 5fe824e18bf6c..6cd9f997a08e6 100644 --- a/third-party/thrift/src/thrift/lib/python/mutable_containers.pyi +++ b/third-party/thrift/src/thrift/lib/python/mutable_containers.pyi @@ -16,6 +16,7 @@ import typing from collections.abc import ( + ItemsView, Iterator, KeysView, MutableMapping, @@ -257,7 +258,6 @@ class MutableMap(MutableMapping[K, V]): def popitem(self) -> Tuple[K, V]: ... def clear(self) -> None: ... def keys(self) -> MapKeysView[K]: ... - # pyre-ignore[15]: Inconsistent override def items(self) -> MapItemsView[K, V]: ... # pyre-ignore[15]: Inconsistent override def values(self) -> MapValuesView[V]: ... @@ -291,10 +291,13 @@ class MapKeysView(KeysView[K]): @overload def __iter__(self: MapKeysView[K]) -> Iterator[K]: ... -class MapItemsView(Generic[K, V]): +class MapItemsView(ItemsView[K, V]): def __len__(self) -> int: ... - def __contains__(self, key: K) -> bool: ... - def __iter__(self) -> MapItemIterator[tuple[K, V]]: ... + def __contains__(self, item: object) -> bool: ... + @overload + def __iter__(self) -> Iterator[tuple[K, V]]: ... + @overload + def __iter__(self: ItemsView[K, V]) -> Iterator[tuple[K, V]]: ... class MapItemIterator(Iterator[T]): def __next__(self) -> T: ... diff --git a/third-party/thrift/src/thrift/lib/python/test/mutable_map_test.py b/third-party/thrift/src/thrift/lib/python/test/mutable_map_test.py index 0ac5ae5fd5071..77e1bf8d3da1a 100644 --- a/third-party/thrift/src/thrift/lib/python/test/mutable_map_test.py +++ b/third-party/thrift/src/thrift/lib/python/test/mutable_map_test.py @@ -19,7 +19,7 @@ import string import unittest -from typing import cast, KeysView, Optional +from typing import cast, ItemsView, Iterator, KeysView, Optional from thrift.python.mutable_containers import ( MapItemsView, @@ -133,11 +133,38 @@ def test_type_hints(self) -> None: v11: MapKeysView[str] = mutable_map.keys() v12: KeysView[str] = mutable_map.keys() - # pyre-ignore[9]: v13 is type `MapKeysView[int]` but is used as type `MapKeysView[str]` + # pyre-ignore[9]: v13 is type `MapKeysView[int]` but is used as type + # `MapKeysView[str]` v13: MapKeysView[int] = mutable_map.keys() + keys_iter_1: Iterator[str] = iter(v11) + + # pyre-ignore[9]: keys_iter_2 is type `Iterator[int]` but is used as + # type `Iterator[str]` + keys_iter_2: Iterator[int] = iter(v11) + + # to silence F841: not used variable + _ = (v11, v12, v13, keys_iter_1, keys_iter_2) + + ################################################################### + + ### items() #### + + v14: MapItemsView[str, int] = mutable_map.items() + v15: ItemsView[str, int] = mutable_map.items() + + # pyre-ignore[9]: v16 is type `MapItemsView[str, str]` but is used + # as type `MapItemsView[str, int]` + v16: MapItemsView[str, str] = mutable_map.items() + + items_iter_1: Iterator[tuple[str, int]] = iter(v14) + + # pyre-ignore[9]: items_iter_2 is type `Iterator[Tuple[str, str]]` + # but is used as type `Iterator[Tuple[str, int]]` + items_iter_2: Iterator[tuple[str, str]] = iter(v14) + # to silence F841: not used variable - _ = (v11, v12, v13) + _ = (v14, v15, v16, items_iter_1, items_iter_2) except Exception: pass