Skip to content

Commit

Permalink
Fix typing for MapItemViews
Browse files Browse the repository at this point in the history
Summary: Addressing the pyre-ignore for `.items()`.

Reviewed By: ahilger

Differential Revision: D66482177

fbshipit-source-id: e9ae1410497e52fa85e1b5f3051ae9620fd2bca3
  • Loading branch information
yoney authored and facebook-github-bot committed Nov 26, 2024
1 parent c1376c0 commit ff7516f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
11 changes: 7 additions & 4 deletions third-party/thrift/src/thrift/lib/python/mutable_containers.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import typing
from collections.abc import (
ItemsView,
Iterator,
KeysView,
MutableMapping,
Expand Down Expand Up @@ -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]: ...
Expand Down Expand Up @@ -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: ...
Expand Down
33 changes: 30 additions & 3 deletions third-party/thrift/src/thrift/lib/python/test/mutable_map_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ff7516f

Please sign in to comment.