Closed
Description
Hi,
It seems that the to_dict
method modifies the nested message.
In short, here's the code and the output (version from master, pulled 24-09-2020)
from dataclasses import dataclass
from typing import Dict
import betterproto
class Code(betterproto.Enum):
ZERO = 0
ONE = 1
@dataclass(eq=False, repr=False)
class Message(betterproto.Message):
codes: Dict[str, "Nested"] = betterproto.map_field(2, betterproto.TYPE_STRING, betterproto.TYPE_MESSAGE)
def __post_init__(self) -> None:
super().__post_init__()
@dataclass(eq=False, repr=False)
class Nested(betterproto.Message):
code: "Code" = betterproto.enum_field(1)
def __post_init__(self) -> None:
super().__post_init__()
msg = Message(codes={'test': Nested(code=Code.ONE)})
print(msg.codes['test'])
print(msg.to_dict())
print(msg.codes['test'])
Nested(code=<Code.ONE: 1>)
{'codes': {'test': {'code': 'ONE'}}}
{'code': 'ONE'}
Also, if you'll change ONE
to ZERO
, it won't print anything, as ZERO
is a default value (even though it was set manually)