Fix for #459 (pydantic code gen only) (#460)

This commit is contained in:
Samuel Yvon 2023-02-21 14:41:32 -05:00 committed by GitHub
parent 13d656587c
commit 2fa0be2141
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 3 deletions

View File

@ -1471,12 +1471,24 @@ class Message(ABC):
@classmethod
def _validate_field_groups(cls, values):
meta = cls._betterproto_meta.oneof_field_by_group # type: ignore
group_to_one_ofs = cls._betterproto_meta.oneof_field_by_group # type: ignore
field_name_to_meta = cls._betterproto_meta.meta_by_field_name # type: ignore
for group, field_set in group_to_one_ofs.items():
if len(field_set) == 1:
(field,) = field_set
field_name = field.name
meta = field_name_to_meta[field_name]
# This is a synthetic oneof; we should ignore it's presence and not consider it as a oneof.
if meta.optional:
continue
for group, field_set in meta.items():
set_fields = [
field.name for field in field_set if values[field.name] is not None
]
if not set_fields:
raise ValueError(f"Group {group} has no value; all fields are None")
elif len(set_fields) > 1:

View File

@ -7,6 +7,10 @@ import {{ i }}
{% endfor %}
{% if output_file.pydantic_dataclasses %}
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from dataclasses import dataclass
else:
from pydantic.dataclasses import dataclass
{%- else -%}
from dataclasses import dataclass