Skip to content

Commit

Permalink
Add typing test to MutableMap
Browse files Browse the repository at this point in the history
Summary: Add typing test to `MutableMap` that covers primitive and container values.

Reviewed By: ahilger

Differential Revision: D66309700

fbshipit-source-id: d7f45f96038881653e3d3b1bfc545e5e88d6723c
  • Loading branch information
yoney authored and facebook-github-bot committed Nov 22, 2024
1 parent bad9437 commit 70d9d75
Showing 1 changed file with 73 additions and 1 deletion.
74 changes: 73 additions & 1 deletion thrift/lib/python/test/mutable_map_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
)

from thrift.python.mutable_typeinfos import MutableListTypeInfo
from thrift.python.mutable_types import to_thrift_list
from thrift.python.mutable_types import to_thrift_list, to_thrift_map, to_thrift_set

from thrift.python.types import typeinfo_i32, typeinfo_string

Expand Down Expand Up @@ -93,6 +93,39 @@ def test_type_hints(self) -> None:
# pyre-ignore[6]: expected `str` but got `int` and expected `int` but got `str`
mutable_map[1] = "value"

###################################################################

### __contains__() ###
_ = 1 in mutable_map
_ = "key" in mutable_map
_ = b"key" in mutable_map

###################################################################

### __delitem() ###
del mutable_map["key"]

# pyre-ignore[6]: expected `str` but got `int`
del mutable_map[1]
# pyre-ignore[6]: expected `str` but got `bytes`
del mutable_map[b"key"]

###################################################################

### pop() ####
v6: int = mutable_map.pop("key")

# pyre-ignore[6]: expected `str` but got `bytes`
v7: int = mutable_map.pop(b"key")
# pyre-ignore[9]: v8 type `str` but is used as type `int`
v8: str = mutable_map.pop("key")

v9: int = mutable_map.pop("key", 42)
v10: int | float = mutable_map.pop("key", 42.0)

# to silence F841: not used variable
_ = (v6, v7, v8, v9, v10)

except Exception:
pass

Expand Down Expand Up @@ -141,6 +174,45 @@ def test_type_hints_with_container_value(self) -> None:
# pyre-ignore[6]: expected `str` but got `int` and expected `MutableList[int]` but got `MutableList[str]`
mutable_map[1] = mutable_list_str

###################################################################

### __contains__() ###
_ = [1, 2, 3] in mutable_map
_ = to_thrift_list([]) in mutable_map
_ = to_thrift_set(set()) in mutable_map
_ = to_thrift_map({}) in mutable_map

###################################################################

### __delitem() ###
del mutable_map["key"]

# pyre-ignore[6]: expected `str` but got `List[int]`
del mutable_map[[1, 2, 3]]
# pyre-ignore[6]: expected `str` but got `_ThriftListWrapper`
del mutable_map[to_thrift_list([])]

###################################################################

### pop() ####
v6: MutableList[int] = mutable_map.pop("key")

# pyre-ignore[6]: expected `str` but got `bytes`
v7: MutableList[int] = mutable_map.pop(b"key")
# pyre-ignore[9]: type `MutableList[str]` but is used as type `MutableList[int]`
v8: MutableList[str] = mutable_map.pop("key")

mutable_list_int = cast(MutableList[int], object())
v9: MutableList[int] = mutable_map.pop("key", mutable_list_int)

mutable_list_str = cast(MutableList[str], object())
v10: MutableList[int] | MutableList[str] = mutable_map.pop(
"key", mutable_list_str
)

# to silence F841: not used variable
_ = (v6, v7, v8, v9, v10)

except Exception:
pass

Expand Down

0 comments on commit 70d9d75

Please sign in to comment.