Skip to content

Commit

Permalink
Fix a bug in serde._record_value_to_proto (#2859)
Browse files Browse the repository at this point in the history
  • Loading branch information
panh99 authored Jan 26, 2024
1 parent 0866770 commit 3cd2f48
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/py/flwr/common/serde.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,12 +601,15 @@ def named_values_from_proto(
def _record_value_to_proto(
value: Any, allowed_types: List[type], proto_class: Type[T]
) -> T:
"""Serialize `*RecordValue` to ProtoBuf."""
"""Serialize `*RecordValue` to ProtoBuf.
Note: `bool` MUST be put in the front of allowd_types if it exists.
"""
arg = {}
for t in allowed_types:
# Single element
# Note: `isinstance(False, int) == True`.
if type(value) == t: # pylint: disable=C0123
if isinstance(value, t):
arg[_type_to_field[t]] = value
return proto_class(**arg)
# List
Expand Down Expand Up @@ -634,7 +637,14 @@ def _record_value_from_proto(value_proto: GrpcMessage) -> Any:
def _record_value_dict_to_proto(
value_dict: Dict[str, Any], allowed_types: List[type], value_proto_class: Type[T]
) -> Dict[str, T]:
"""Serialize the record value dict to ProtoBuf."""
"""Serialize the record value dict to ProtoBuf.
Note: `bool` MUST be put in the front of allowd_types if it exists.
"""
# Move bool to the front
if bool in allowed_types and allowed_types[0] != bool:
allowed_types.remove(bool)
allowed_types.insert(0, bool)

def proto(_v: Any) -> T:
return _record_value_to_proto(_v, allowed_types, value_proto_class)
Expand Down Expand Up @@ -708,7 +718,7 @@ def configs_record_to_proto(record: ConfigsRecord) -> ProtoConfigsRecord:
"""Serialize ConfigsRecord to ProtoBuf."""
return ProtoConfigsRecord(
data=_record_value_dict_to_proto(
record.data, [int, float, bool, str, bytes], ProtoConfigsRecordValue
record.data, [bool, int, float, str, bytes], ProtoConfigsRecordValue
)
)

Expand Down

0 comments on commit 3cd2f48

Please sign in to comment.