Prevent users from creating messages with wrong parameters when pydantic is used (#615)

This commit is contained in:
Adrien
2024-09-12 19:34:47 +02:00
committed by GitHub
parent efaef5095c
commit 8fdcb381b7
6 changed files with 797 additions and 884 deletions

View File

@@ -5,16 +5,9 @@
{% for i in output_file.python_module_imports|sort %}
import {{ i }}
{% endfor %}
{% set type_checking_imported = False %}
{% if output_file.pydantic_dataclasses %}
from typing import TYPE_CHECKING
{% set type_checking_imported = True %}
if TYPE_CHECKING:
from dataclasses import dataclass
else:
from pydantic.dataclasses import dataclass
from pydantic.dataclasses import dataclass
from pydantic.dataclasses import rebuild_dataclass
{%- else -%}
from dataclasses import dataclass
@@ -46,7 +39,7 @@ import grpclib
{{ i }}
{% endfor %}
{% if output_file.imports_type_checking_only and not type_checking_imported %}
{% if output_file.imports_type_checking_only %}
from typing import TYPE_CHECKING
if TYPE_CHECKING:

View File

@@ -23,7 +23,11 @@ class {{ enum.py_name }}(betterproto.Enum):
{% endfor %}
{% endif %}
{% for message in output_file.messages %}
{% if output_file.pydantic_dataclasses %}
@dataclass(eq=False, repr=False, config={"extra": "forbid"})
{% else %}
@dataclass(eq=False, repr=False)
{% endif %}
class {{ message.py_name }}(betterproto.Message):
{% if message.comment %}
{{ message.comment }}