betterproto2-compiler 0.2.3__py3-none-any.whl → 0.2.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,5 @@
1
+ import typing
2
+
1
3
  import betterproto2
2
4
 
3
5
  from betterproto2_compiler.lib.google.protobuf import Any as VanillaAny
@@ -18,19 +20,37 @@ class Any(VanillaAny):
18
20
  self.type_url = message_pool.type_to_url[type(message)]
19
21
  self.value = bytes(message)
20
22
 
21
- def unpack(self, message_pool: "betterproto2.MessagePool | None" = None) -> betterproto2.Message:
23
+ def unpack(self, message_pool: "betterproto2.MessagePool | None" = None) -> betterproto2.Message | None:
22
24
  """
23
25
  Return the message packed inside the `Any` object.
24
26
 
25
27
  The target message type must be registered in the message pool, which is done automatically when the module
26
28
  defining the message type is imported.
27
29
  """
30
+ if not self.type_url:
31
+ return None
32
+
28
33
  message_pool = message_pool or default_message_pool
29
34
 
30
- message_type = message_pool.url_to_type[self.type_url]
35
+ try:
36
+ message_type = message_pool.url_to_type[self.type_url]
37
+ except KeyError:
38
+ raise TypeError(f"Can't unpack unregistered type: {self.type_url}")
31
39
 
32
40
  return message_type().parse(self.value)
33
41
 
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()}
42
+ def to_dict(self, **kwargs) -> dict[str, typing.Any]:
43
+ # TODO allow passing a message pool to `to_dict`
44
+ output: dict[str, typing.Any] = {"@type": self.type_url}
45
+
46
+ value = self.unpack()
47
+
48
+ if value is None:
49
+ return output
50
+
51
+ if type(value).to_dict == betterproto2.Message.to_dict:
52
+ output.update(value.to_dict(**kwargs))
53
+ else:
54
+ output["value"] = value.to_dict(**kwargs)
55
+
56
+ return output
@@ -37,9 +37,9 @@ class Timestamp(VanillaTimestamp):
37
37
  return f"{result}Z"
38
38
  if (nanos % 1e6) == 0:
39
39
  # Serialize 3 fractional digits.
40
- return f"{result}.{int(nanos // 1e6) :03d}Z"
40
+ return f"{result}.{int(nanos // 1e6):03d}Z"
41
41
  if (nanos % 1e3) == 0:
42
42
  # Serialize 6 fractional digits.
43
- return f"{result}.{int(nanos // 1e3) :06d}Z"
43
+ return f"{result}.{int(nanos // 1e3):06d}Z"
44
44
  # Serialize 9 fractional digits.
45
45
  return f"{result}.{nanos:09d}"
@@ -724,35 +724,6 @@ class Any(betterproto2.Message):
724
724
  Must be a valid serialized protocol buffer of the above specified type.
725
725
  """
726
726
 
727
- def pack(self, message: betterproto2.Message, message_pool: "betterproto2.MessagePool | None" = None) -> None:
728
- """
729
- Pack the given message in the `Any` object.
730
-
731
- The message type must be registered in the message pool, which is done automatically when the module defining
732
- the message type is imported.
733
- """
734
- message_pool = message_pool or default_message_pool
735
-
736
- self.type_url = message_pool.type_to_url[type(message)]
737
- self.value = bytes(message)
738
-
739
- def unpack(self, message_pool: "betterproto2.MessagePool | None" = None) -> betterproto2.Message:
740
- """
741
- Return the message packed inside the `Any` object.
742
-
743
- The target message type must be registered in the message pool, which is done automatically when the module
744
- defining the message type is imported.
745
- """
746
- message_pool = message_pool or default_message_pool
747
-
748
- message_type = message_pool.url_to_type[self.type_url]
749
-
750
- return message_type().parse(self.value)
751
-
752
- def to_dict(self) -> dict: # pyright: ignore [reportIncompatibleMethodOverride]
753
- # TOOO improve when dict is updated
754
- return {"@type": self.type_url, "value": self.unpack().to_dict()}
755
-
756
727
 
757
728
  default_message_pool.register_message("google.protobuf", "Any", Any)
758
729
 
@@ -22,6 +22,7 @@ import builtins
22
22
  import datetime
23
23
  import warnings
24
24
  from collections.abc import AsyncIterable, AsyncIterator, Iterable
25
+ import typing
25
26
  from typing import TYPE_CHECKING
26
27
 
27
28
  {% if output_file.settings.pydantic_dataclasses %}
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: betterproto2_compiler
3
- Version: 0.2.3
3
+ Version: 0.2.4
4
4
  Summary: Compiler for betterproto2
5
5
  License: MIT
6
6
  Keywords: protobuf,gRPC,compiler
@@ -13,11 +13,12 @@ Classifier: Programming Language :: Python :: 3.10
13
13
  Classifier: Programming Language :: Python :: 3.11
14
14
  Classifier: Programming Language :: Python :: 3.12
15
15
  Classifier: Programming Language :: Python :: 3.13
16
- Requires-Dist: betterproto2 (>=0.2.1,<0.3.0)
16
+ Requires-Dist: betterproto2 (>=0.2.3,<0.3.0)
17
17
  Requires-Dist: grpclib (>=0.4.1,<0.5.0)
18
18
  Requires-Dist: jinja2 (>=3.0.3)
19
- Requires-Dist: ruff (>=0.7.4,<0.8.0)
19
+ Requires-Dist: ruff (>=0.9.3,<0.10.0)
20
20
  Requires-Dist: typing-extensions (>=4.7.1,<5.0.0)
21
+ Project-URL: Documentation, https://betterproto.github.io/python-betterproto2-compiler/
21
22
  Project-URL: Repository, https://github.com/betterproto/python-betterproto2-compiler
22
23
  Description-Content-Type: text/markdown
23
24
 
@@ -4,12 +4,12 @@ betterproto2_compiler/compile/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5N
4
4
  betterproto2_compiler/compile/importing.py,sha256=2DF9zpYhjX_H3PCyUHCXhho1J2QdGAtXitlNneZZfFs,7129
5
5
  betterproto2_compiler/compile/naming.py,sha256=zf0VOmNojzyv33upOGelGxjZTEDE8JULEEED5_3inHg,562
6
6
  betterproto2_compiler/known_types/__init__.py,sha256=Exqo-3ubDuik0TZDTw5ZPqf-dVb2uPJTZxMG7X58E6U,780
7
- betterproto2_compiler/known_types/any.py,sha256=QnfSKTXzazZwmsxaFu8_SYNmLww1D2mFdmi29_8Nzb4,1432
7
+ betterproto2_compiler/known_types/any.py,sha256=eRMenvvrn-1Wiss3YxhqRsjZ4XqiqPb1YQuinoA8wI4,1899
8
8
  betterproto2_compiler/known_types/duration.py,sha256=jy9GPnQTT9qhi12pQDG_ptdFAdm2gYkq3NH75zUTDOU,895
9
- betterproto2_compiler/known_types/timestamp.py,sha256=lw7kQPYJn76Q2wR4l3nCshj7VZ3s8h_xRcvrGIno4z0,2155
9
+ betterproto2_compiler/known_types/timestamp.py,sha256=dUfJmdrVg1NW-zkquAc8kxMH6nqdxQ2x_MKeP4N5ksY,2153
10
10
  betterproto2_compiler/lib/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
11
11
  betterproto2_compiler/lib/google/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
12
- betterproto2_compiler/lib/google/protobuf/__init__.py,sha256=ajfzhKfLLdrg1eESD9LR18chGNDI2EerRtjLTUnWxKY,109428
12
+ betterproto2_compiler/lib/google/protobuf/__init__.py,sha256=dGC8iW2zPQQxxNMGmS2IjY5bOhYUmNvcasB-GC2CtHQ,108191
13
13
  betterproto2_compiler/lib/google/protobuf/compiler/__init__.py,sha256=bWYhEcL4nz0_4H6FTCmN3F419FQWXkY49LGE2hJJ6jM,8906
14
14
  betterproto2_compiler/lib/message_pool.py,sha256=4-cRhhiM6bmfpUJZ8qxc8LEyqHBHpLCcotjbyZxl7JM,71
15
15
  betterproto2_compiler/plugin/__init__.py,sha256=L3pW0b4CvkM5x53x_sYt1kYiSFPO0_vaeH6EQPq9FAM,43
@@ -22,10 +22,10 @@ betterproto2_compiler/plugin/parser.py,sha256=MIA5-pAIJsng59wk3KYEKBARNCsQEQeetn
22
22
  betterproto2_compiler/plugin/plugin.bat,sha256=lfLT1WguAXqyerLLsRL6BfHA0RqUE6QG79v-1BYVSpI,48
23
23
  betterproto2_compiler/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
24
24
  betterproto2_compiler/settings.py,sha256=FQwco5j9ViBXtDYoFqog7SlXhX2YcbgEnFP77znYiwc,94
25
- betterproto2_compiler/templates/header.py.j2,sha256=fZZ10HP8ZKD2iaRXyx_x4WYp8ffMOlu-J3Q_1h-pi3Y,1595
25
+ betterproto2_compiler/templates/header.py.j2,sha256=HrISX0IKmUsVpIlGIT6XlD9e3LgWWPN7jYRpws5_-CY,1609
26
26
  betterproto2_compiler/templates/template.py.j2,sha256=Kwbw302GGzSEQ007eSHmHYGF0lHw01n_c0sGbtV9pWI,8419
27
- betterproto2_compiler-0.2.3.dist-info/LICENSE.md,sha256=Pgl2pReU-2yw2miGeQ55UFlyzqAZ_EpYVyZ2nWjwRv4,1121
28
- betterproto2_compiler-0.2.3.dist-info/METADATA,sha256=u6OTIRaAg9s2lIdSNif-FzAiSDYxATAHg6ktrj9LDj0,1099
29
- betterproto2_compiler-0.2.3.dist-info/WHEEL,sha256=IYZQI976HJqqOpQU6PHkJ8fb3tMNBFjg-Cn-pwAbaFM,88
30
- betterproto2_compiler-0.2.3.dist-info/entry_points.txt,sha256=re3Qg8lLljbVobeeKH2f1FVQZ114wfZkGv3zCZTD8Ok,84
31
- betterproto2_compiler-0.2.3.dist-info/RECORD,,
27
+ betterproto2_compiler-0.2.4.dist-info/LICENSE.md,sha256=Pgl2pReU-2yw2miGeQ55UFlyzqAZ_EpYVyZ2nWjwRv4,1121
28
+ betterproto2_compiler-0.2.4.dist-info/METADATA,sha256=bQ5WSjI-tm0jNx_WK_1fwlCd3qIP37SZF0EpyFBoI9k,1188
29
+ betterproto2_compiler-0.2.4.dist-info/WHEEL,sha256=IYZQI976HJqqOpQU6PHkJ8fb3tMNBFjg-Cn-pwAbaFM,88
30
+ betterproto2_compiler-0.2.4.dist-info/entry_points.txt,sha256=re3Qg8lLljbVobeeKH2f1FVQZ114wfZkGv3zCZTD8Ok,84
31
+ betterproto2_compiler-0.2.4.dist-info/RECORD,,