Skip to content

Commit

Permalink
Prevent users from creating messages with wrong parameters when pydan…
Browse files Browse the repository at this point in the history
…tic is used (#615)
  • Loading branch information
AdrienVannson authored Sep 12, 2024
1 parent efaef50 commit 8fdcb38
Show file tree
Hide file tree
Showing 6 changed files with 799 additions and 886 deletions.
1,639 changes: 766 additions & 873 deletions poetry.lock

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,20 @@ packages = [
]

[tool.poetry.dependencies]
python = "^3.7"
python = "^3.8"
black = { version = ">=23.1.0", optional = true }
grpclib = "^0.4.1"
importlib-metadata = { version = ">=1.6.0", python = "<3.8" }
jinja2 = { version = ">=3.0.3", optional = true }
python-dateutil = "^2.8"
isort = {version = "^5.11.5", optional = true}
isort = { version = "^5.11.5", optional = true }
typing-extensions = "^4.7.1"
betterproto-rust-codec = { version = "0.1.1", optional = true }

[tool.poetry.group.dev.dependencies]
asv = "^0.4.2"
bpython = "^0.19"
jinja2 = ">=3.0.3"
mypy = "^0.930"
mypy = "^1.11.2"
sphinx = "3.1.2"
sphinx-rtd-theme = "0.5.0"
pre-commit = "^2.17.0"
Expand Down
11 changes: 2 additions & 9 deletions src/betterproto/templates/header.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions src/betterproto/templates/template.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
7 changes: 7 additions & 0 deletions tests/inputs/invalid_field/invalid_field.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
syntax = "proto3";

package invalid_field;

message Test {
int32 x = 1;
}
17 changes: 17 additions & 0 deletions tests/inputs/invalid_field/test_invalid_field.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest


def test_invalid_field():
from tests.output_betterproto.invalid_field import Test

with pytest.raises(TypeError):
Test(unknown_field=12)


def test_invalid_field_pydantic():
from pydantic import ValidationError

from tests.output_betterproto_pydantic.invalid_field import Test

with pytest.raises(ValidationError):
Test(unknown_field=12)

0 comments on commit 8fdcb38

Please sign in to comment.