Skip to content
This repository was archived by the owner on Jun 9, 2025. It is now read-only.

Commit 87da841

Browse files
committed
Improve to_dict for any
1 parent 94a7999 commit 87da841

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/betterproto2_compiler/known_types/any.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import typing
2+
13
import betterproto2
24

35
from betterproto2_compiler.lib.google.protobuf import Any as VanillaAny
@@ -31,6 +33,14 @@ def unpack(self, message_pool: "betterproto2.MessagePool | None" = None) -> bett
3133

3234
return message_type().parse(self.value)
3335

34-
def to_dict(self) -> dict: # pyright: ignore [reportIncompatibleMethodOverride]
35-
# TOOO improve when dict is updated
36-
return {"@type": self.type_url, "value": self.unpack().to_dict()}
36+
def to_dict(self, **kwargs) -> dict[str, typing.Any]:
37+
output: dict[str, typing.Any] = {"@type": self.type_url}
38+
39+
value = self.unpack()
40+
41+
if type(value).to_dict == betterproto2.Message.to_dict:
42+
output.update(value.to_dict(**kwargs))
43+
else:
44+
output["value"] = value.to_dict(**kwargs)
45+
46+
return output

src/betterproto2_compiler/templates/header.py.j2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import builtins
2222
import datetime
2323
import warnings
2424
from collections.abc import AsyncIterable, AsyncIterator, Iterable
25+
import typing
2526
from typing import TYPE_CHECKING
2627

2728
{% if output_file.settings.pydantic_dataclasses %}

0 commit comments

Comments
 (0)