Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,647 changes: 770 additions & 877 deletions poetry.lock

Large diffs are not rendered by default.

11 changes: 5 additions & 6 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"
black = { version = ">=23.1.0", optional = true }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not using optional here is not acceptable for libraries that bundle their protobufs as it will install black, isort etc

python = "^3.8"
black = ">=23.1.0"
grpclib = "^0.4.1"
importlib-metadata = { version = ">=1.6.0", python = "<3.8" }
jinja2 = { version = ">=3.0.3", optional = true }
jinja2 = ">=3.0.3"
python-dateutil = "^2.8"
isort = {version = "^5.11.5", optional = true}
isort = "^5.11.5"
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)
Loading