Prevent users from creating messages with wrong parameters when pydantic is used (#615)
This commit is contained in:
parent
efaef5095c
commit
8fdcb381b7
1635
poetry.lock
generated
1635
poetry.lock
generated
File diff suppressed because it is too large
Load Diff
@ -12,13 +12,12 @@ packages = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
[tool.poetry.dependencies]
|
[tool.poetry.dependencies]
|
||||||
python = "^3.7"
|
python = "^3.8"
|
||||||
black = { version = ">=23.1.0", optional = true }
|
black = { version = ">=23.1.0", optional = true }
|
||||||
grpclib = "^0.4.1"
|
grpclib = "^0.4.1"
|
||||||
importlib-metadata = { version = ">=1.6.0", python = "<3.8" }
|
|
||||||
jinja2 = { version = ">=3.0.3", optional = true }
|
jinja2 = { version = ">=3.0.3", optional = true }
|
||||||
python-dateutil = "^2.8"
|
python-dateutil = "^2.8"
|
||||||
isort = {version = "^5.11.5", optional = true}
|
isort = { version = "^5.11.5", optional = true }
|
||||||
typing-extensions = "^4.7.1"
|
typing-extensions = "^4.7.1"
|
||||||
betterproto-rust-codec = { version = "0.1.1", optional = true }
|
betterproto-rust-codec = { version = "0.1.1", optional = true }
|
||||||
|
|
||||||
@ -26,7 +25,7 @@ betterproto-rust-codec = { version = "0.1.1", optional = true }
|
|||||||
asv = "^0.4.2"
|
asv = "^0.4.2"
|
||||||
bpython = "^0.19"
|
bpython = "^0.19"
|
||||||
jinja2 = ">=3.0.3"
|
jinja2 = ">=3.0.3"
|
||||||
mypy = "^0.930"
|
mypy = "^1.11.2"
|
||||||
sphinx = "3.1.2"
|
sphinx = "3.1.2"
|
||||||
sphinx-rtd-theme = "0.5.0"
|
sphinx-rtd-theme = "0.5.0"
|
||||||
pre-commit = "^2.17.0"
|
pre-commit = "^2.17.0"
|
||||||
|
@ -5,16 +5,9 @@
|
|||||||
{% for i in output_file.python_module_imports|sort %}
|
{% for i in output_file.python_module_imports|sort %}
|
||||||
import {{ i }}
|
import {{ i }}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% set type_checking_imported = False %}
|
|
||||||
|
|
||||||
{% if output_file.pydantic_dataclasses %}
|
{% if output_file.pydantic_dataclasses %}
|
||||||
from typing import TYPE_CHECKING
|
from pydantic.dataclasses import dataclass
|
||||||
{% set type_checking_imported = True %}
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
|
||||||
from dataclasses import dataclass
|
|
||||||
else:
|
|
||||||
from pydantic.dataclasses import dataclass
|
|
||||||
from pydantic.dataclasses import rebuild_dataclass
|
from pydantic.dataclasses import rebuild_dataclass
|
||||||
{%- else -%}
|
{%- else -%}
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
@ -46,7 +39,7 @@ import grpclib
|
|||||||
{{ i }}
|
{{ i }}
|
||||||
{% endfor %}
|
{% 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
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
|
@ -23,7 +23,11 @@ class {{ enum.py_name }}(betterproto.Enum):
|
|||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% for message in output_file.messages %}
|
{% 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)
|
@dataclass(eq=False, repr=False)
|
||||||
|
{% endif %}
|
||||||
class {{ message.py_name }}(betterproto.Message):
|
class {{ message.py_name }}(betterproto.Message):
|
||||||
{% if message.comment %}
|
{% if message.comment %}
|
||||||
{{ message.comment }}
|
{{ message.comment }}
|
||||||
|
7
tests/inputs/invalid_field/invalid_field.proto
Normal file
7
tests/inputs/invalid_field/invalid_field.proto
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
package invalid_field;
|
||||||
|
|
||||||
|
message Test {
|
||||||
|
int32 x = 1;
|
||||||
|
}
|
17
tests/inputs/invalid_field/test_invalid_field.py
Normal file
17
tests/inputs/invalid_field/test_invalid_field.py
Normal file
@ -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)
|
Loading…
x
Reference in New Issue
Block a user