Skip to content

Commit c3c2055

Browse files
authored
fix: support pydantic-version google pb (#568)
* feat: pydantic version of google pb * fix: patch pb Struct to support json, dict rountrip * fix: pydantic-version google pb, json, dict rntrip * chore: remove `@generated`, remove gen, code fmt * chore: test case for pydantic-version google pb
1 parent df1ba91 commit c3c2055

File tree

16 files changed

+5606
-1869
lines changed

16 files changed

+5606
-1869
lines changed

docs/migrating.rst

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,19 @@ wrappers used to provide optional zero value support. Each of these has a specia
8585
representation and is handled a little differently from normal messages. The Python
8686
mapping for these is as follows:
8787

88-
+-------------------------------+-----------------------------------------------+--------------------------+
89-
| ``Google Message`` | ``Python Type`` | ``Default`` |
90-
+===============================+===============================================+==========================+
91-
| ``google.protobuf.duration`` | :class:`datetime.timedelta` | ``0`` |
92-
+-------------------------------+-----------------------------------------------+--------------------------+
93-
| ``google.protobuf.timestamp`` | ``Timezone-aware`` :class:`datetime.datetime` | ``1970-01-01T00:00:00Z`` |
94-
+-------------------------------+-----------------------------------------------+--------------------------+
95-
| ``google.protobuf.*Value`` | ``Optional[...]``/``None`` | ``None`` |
96-
+-------------------------------+-----------------------------------------------+--------------------------+
97-
| ``google.protobuf.*`` | ``betterproto.lib.google.protobuf.*`` | ``None`` |
98-
+-------------------------------+-----------------------------------------------+--------------------------+
88+
+-------------------------------+-------------------------------------------------+--------------------------+
89+
| ``Google Message`` | ``Python Type`` | ``Default`` |
90+
+===============================+=================================================+==========================+
91+
| ``google.protobuf.duration`` | :class:`datetime.timedelta` | ``0`` |
92+
+-------------------------------+-------------------------------------------------+--------------------------+
93+
| ``google.protobuf.timestamp`` | ``Timezone-aware`` :class:`datetime.datetime` | ``1970-01-01T00:00:00Z`` |
94+
+-------------------------------+-------------------------------------------------+--------------------------+
95+
| ``google.protobuf.*Value`` | ``Optional[...]``/``None`` | ``None`` |
96+
+-------------------------------+-------------------------------------------------+--------------------------+
97+
| ``google.protobuf.*`` | ``betterproto.lib.std.google.protobuf.*`` | ``None`` |
98+
+-------------------------------+-------------------------------------------------+--------------------------+
99+
| ``google.protobuf.*`` | ``betterproto.lib.pydantic.google.protobuf.*`` | ``None`` |
100+
+-------------------------------+-------------------------------------------------+--------------------------+
99101

100102

101103
For the wrapper types, the Python type corresponds to the wrapped type, e.g.

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ cmd = """
9595
protoc
9696
--plugin=protoc-gen-custom=src/betterproto/plugin/main.py
9797
--custom_opt=INCLUDE_GOOGLE
98-
--custom_out=src/betterproto/lib
98+
--custom_out=src/betterproto/lib/std
9999
-I /usr/local/include/
100100
/usr/local/include/google/protobuf/**/*.proto
101101
"""
102-
help = "Regenerate the types in betterproto.lib.google"
102+
help = "Regenerate the types in betterproto.lib.std.google"
103103

104104
# CI tasks
105105

src/betterproto/compile/importing.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ def parse_source_type_name(field_type_name: str) -> Tuple[str, str]:
4343

4444

4545
def get_type_reference(
46-
*, package: str, imports: set, source_type: str, unwrap: bool = True
46+
*,
47+
package: str,
48+
imports: set,
49+
source_type: str,
50+
unwrap: bool = True,
51+
pydantic: bool = False,
4752
) -> str:
4853
"""
4954
Return a Python type name for a proto type reference. Adds the import if
@@ -69,7 +74,9 @@ def get_type_reference(
6974
compiling_google_protobuf = current_package == ["google", "protobuf"]
7075
importing_google_protobuf = py_package == ["google", "protobuf"]
7176
if importing_google_protobuf and not compiling_google_protobuf:
72-
py_package = ["betterproto", "lib"] + py_package
77+
py_package = (
78+
["betterproto", "lib"] + (["pydantic"] if pydantic else []) + py_package
79+
)
7380

7481
if py_package[:1] == ["betterproto"]:
7582
return reference_absolute(imports, py_package, py_type)

0 commit comments

Comments
 (0)