Skip to content

Commit 8fdcb38

Browse files
Prevent users from creating messages with wrong parameters when pydantic is used (#615)
1 parent efaef50 commit 8fdcb38

File tree

6 files changed

+799
-886
lines changed

6 files changed

+799
-886
lines changed

poetry.lock

Lines changed: 766 additions & 873 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,20 @@ packages = [
1212
]
1313

1414
[tool.poetry.dependencies]
15-
python = "^3.7"
15+
python = "^3.8"
1616
black = { version = ">=23.1.0", optional = true }
1717
grpclib = "^0.4.1"
18-
importlib-metadata = { version = ">=1.6.0", python = "<3.8" }
1918
jinja2 = { version = ">=3.0.3", optional = true }
2019
python-dateutil = "^2.8"
21-
isort = {version = "^5.11.5", optional = true}
20+
isort = { version = "^5.11.5", optional = true }
2221
typing-extensions = "^4.7.1"
2322
betterproto-rust-codec = { version = "0.1.1", optional = true }
2423

2524
[tool.poetry.group.dev.dependencies]
2625
asv = "^0.4.2"
2726
bpython = "^0.19"
2827
jinja2 = ">=3.0.3"
29-
mypy = "^0.930"
28+
mypy = "^1.11.2"
3029
sphinx = "3.1.2"
3130
sphinx-rtd-theme = "0.5.0"
3231
pre-commit = "^2.17.0"

src/betterproto/templates/header.py.j2

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,9 @@
55
{% for i in output_file.python_module_imports|sort %}
66
import {{ i }}
77
{% endfor %}
8-
{% set type_checking_imported = False %}
98

109
{% if output_file.pydantic_dataclasses %}
11-
from typing import TYPE_CHECKING
12-
{% set type_checking_imported = True %}
13-
14-
if TYPE_CHECKING:
15-
from dataclasses import dataclass
16-
else:
17-
from pydantic.dataclasses import dataclass
10+
from pydantic.dataclasses import dataclass
1811
from pydantic.dataclasses import rebuild_dataclass
1912
{%- else -%}
2013
from dataclasses import dataclass
@@ -46,7 +39,7 @@ import grpclib
4639
{{ i }}
4740
{% endfor %}
4841

49-
{% if output_file.imports_type_checking_only and not type_checking_imported %}
42+
{% if output_file.imports_type_checking_only %}
5043
from typing import TYPE_CHECKING
5144

5245
if TYPE_CHECKING:

src/betterproto/templates/template.py.j2

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ class {{ enum.py_name }}(betterproto.Enum):
2323
{% endfor %}
2424
{% endif %}
2525
{% for message in output_file.messages %}
26+
{% if output_file.pydantic_dataclasses %}
27+
@dataclass(eq=False, repr=False, config={"extra": "forbid"})
28+
{% else %}
2629
@dataclass(eq=False, repr=False)
30+
{% endif %}
2731
class {{ message.py_name }}(betterproto.Message):
2832
{% if message.comment %}
2933
{{ message.comment }}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
syntax = "proto3";
2+
3+
package invalid_field;
4+
5+
message Test {
6+
int32 x = 1;
7+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pytest
2+
3+
4+
def test_invalid_field():
5+
from tests.output_betterproto.invalid_field import Test
6+
7+
with pytest.raises(TypeError):
8+
Test(unknown_field=12)
9+
10+
11+
def test_invalid_field_pydantic():
12+
from pydantic import ValidationError
13+
14+
from tests.output_betterproto_pydantic.invalid_field import Test
15+
16+
with pytest.raises(ValidationError):
17+
Test(unknown_field=12)

0 commit comments

Comments
 (0)