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 11c250bf3e0e49..5fe824e18bf6c7 100644 --- a/third-party/thrift/src/thrift/lib/python/mutable_containers.pyi +++ b/third-party/thrift/src/thrift/lib/python/mutable_containers.pyi @@ -17,7 +17,8 @@ import typing from collections.abc import ( Iterator, - MutableMapping as MutableMappingAbc, + KeysView, + MutableMapping, MutableSequence, MutableSet as MutableSetAbc, ) @@ -203,7 +204,7 @@ class SupportsKeysAndGetItem(Protocol[K, V]): def keys(self) -> Iterable[K]: ... def __getitem__(self, k: K) -> V: ... -class MutableMap(MutableMappingAbc[K, V]): +class MutableMap(MutableMapping[K, V]): def __init__( self, key_typeinfo: object, @@ -255,7 +256,6 @@ class MutableMap(MutableMappingAbc[K, V]): def pop(self, key: K, /, default: T) -> V | T: ... def popitem(self) -> Tuple[K, V]: ... def clear(self) -> None: ... - # pyre-ignore[15]: Inconsistent override def keys(self) -> MapKeysView[K]: ... # pyre-ignore[15]: Inconsistent override def items(self) -> MapItemsView[K, V]: ... @@ -283,10 +283,13 @@ class MutableMap(MutableMappingAbc[K, V]): /, ) -> V: ... -class MapKeysView(Generic[K]): +class MapKeysView(KeysView[K]): def __len__(self) -> int: ... def __contains__(self, key: object) -> bool: ... - def __iter__(self) -> ValueIterator[K]: ... + @overload + def __iter__(self) -> Iterator[K]: ... + @overload + def __iter__(self: MapKeysView[K]) -> Iterator[K]: ... class MapItemsView(Generic[K, V]): def __len__(self) -> int: ... 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 cfd09e4ea0aa87..249691f37eccd1 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, Optional +from typing import cast, KeysView, Optional from thrift.python.mutable_containers import ( MapItemsView, @@ -126,6 +126,19 @@ def test_type_hints(self) -> None: # to silence F841: not used variable _ = (v6, v7, v8, v9, v10) + ################################################################### + + ### keys() #### + + 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]` + v13: MapKeysView[int] = mutable_map.keys() + + # to silence F841: not used variable + _ = (v11, v12, v13) + except Exception: pass