Skip to content

Commit

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

Reviewed By: createdbysk

Differential Revision: D66439015

fbshipit-source-id: 2a06cf0ae8e63229311b9efd0e1f48774ddfc356
  • Loading branch information
yoney authored and facebook-github-bot committed Nov 25, 2024
1 parent 0edfd14 commit a64b3b1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
13 changes: 8 additions & 5 deletions third-party/thrift/src/thrift/lib/python/mutable_containers.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import typing
from collections.abc import (
Iterator,
MutableMapping as MutableMappingAbc,
KeysView,
MutableMapping,
MutableSequence,
MutableSet as MutableSetAbc,
)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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]: ...
Expand Down Expand Up @@ -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: ...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit a64b3b1

Please sign in to comment.