Skip to content

Commit

Permalink
update typeddict
Browse files Browse the repository at this point in the history
  • Loading branch information
panh99 committed Jun 21, 2024
1 parent ce77e20 commit c022267
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 85 deletions.
6 changes: 3 additions & 3 deletions src/py/flwr/common/record/recordset_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def test_set_parameters_with_incorrect_types(
}

with pytest.raises(TypeError):
p_record.update(array_dict)
p_record.update(array_dict) # type: ignore


@pytest.mark.parametrize(
Expand Down Expand Up @@ -250,7 +250,7 @@ def test_set_metrics_to_metricsrecord_with_incorrect_types(
)

with pytest.raises(TypeError):
m_record.update(my_metrics)
m_record.update(my_metrics) # type: ignore


@pytest.mark.parametrize(
Expand Down Expand Up @@ -360,7 +360,7 @@ def test_set_configs_to_configsrecord_with_incorrect_types(
)

with pytest.raises(TypeError):
c_record.update(my_configs)
c_record.update(my_configs) # type: ignore


def test_count_bytes_metricsrecord() -> None:
Expand Down
83 changes: 1 addition & 82 deletions src/py/flwr/common/record/typeddict.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,10 @@


from collections.abc import MutableMapping
from typing import (
Any,
Callable,
Dict,
Generic,
ItemsView,
Iterator,
KeysView,
Optional,
TypeVar,
Union,
ValuesView,
cast,
overload,
)
from typing import Callable, Dict, Generic, Iterator, TypeVar, cast

K = TypeVar("K") # Key type
V = TypeVar("V") # Value type
_T = TypeVar("_T")


class TypedDict(MutableMapping[K, V], Generic[K, V]):
Expand Down Expand Up @@ -89,69 +74,3 @@ def __eq__(self, other: object) -> bool:
if isinstance(other, dict):
return data == other
return NotImplemented

def items(self) -> ItemsView[K, V]:
"""R.items() -> a set-like object providing a view on R's items."""
return cast(Dict[K, V], self.__dict__["_data"]).items()

def keys(self) -> KeysView[K]:
"""R.keys() -> a set-like object providing a view on R's keys."""
return cast(Dict[K, V], self.__dict__["_data"]).keys()

def values(self) -> ValuesView[V]:
"""R.values() -> an object providing a view on R's values."""
return cast(Dict[K, V], self.__dict__["_data"]).values()

@overload
def update(self, m: SupportsKeysAndGetItem[K, V], /, **kwargs: V) -> None: ...


def update(self, m: Any = None, **kwargs: Any) -> None:
"""R.update([E, ]**F) -> None.
Update R from dict/iterable E and F.
"""
for key, value in dict(m, **kwargs).items():
self[key] = value

@overload
def pop(self, key: K, /) -> V: ...

@overload
def pop(self, key: K, /, default: V) -> V: ...

@overload
def pop(self, key: K, /, default: _T) -> Union[V, _T]: ...

def pop(self, key: K, /, default: Any = None) -> Any:
"""R.pop(k[,d]) -> v, remove specified key and return the corresponding value.
If key is not found, d is returned if given, otherwise KeyError is raised.
"""
if default is None:
return cast(Dict[K, V], self.__dict__["_data"]).pop(key)
else:
return cast(Dict[K, V], self.__dict__["_data"]).pop(key, default)

@overload
def get(self, key: K, /) -> Optional[V]: ...

@overload
def get(self, key: K, /, default: Union[V, _T]) -> Union[V, _T]: ...

def get(self, key: K, /, default: Any = None) -> Any:
"""R.get(k[,d]) -> R[k] if k in R, else d.
d defaults to None.
"""
if default is None:
return cast(Dict[K, V], self.__dict__["_data"]).get(key)
else:
return cast(Dict[K, V], self.__dict__["_data"]).get(key, default)

def clear(self) -> None:
"""R.clear() -> None.
Remove all items from R.
"""
cast(Dict[K, V], self.__dict__["_data"]).clear()

0 comments on commit c022267

Please sign in to comment.