Skip to content

Fix default values for enum service args #298 #299

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

Merged
merged 1 commit into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 10 additions & 3 deletions src/betterproto/plugin/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@
import re
import textwrap
from dataclasses import dataclass, field
from typing import Dict, Iterable, Iterator, List, Optional, Set, Text, Type, Union
import sys
from typing import Dict, Iterable, Iterator, List, Optional, Set, Type, Union

from ..casing import sanitize_name
from ..compile.importing import get_type_reference, parse_source_type_name
Expand Down Expand Up @@ -460,7 +459,7 @@ def field_type(self) -> str:
)

@property
def default_value_string(self) -> Union[Text, None, float, int]:
def default_value_string(self) -> str:
"""Python representation of the default proto value."""
if self.repeated:
return "[]"
Expand All @@ -474,6 +473,14 @@ def default_value_string(self) -> Union[Text, None, float, int]:
return '""'
elif self.py_type == "bytes":
return 'b""'
elif self.field_type == "enum":
enum_proto_obj_name = self.proto_obj.type_name.split(".").pop()
enum = next(
e
for e in self.output_file.enums
if e.proto_obj.name == enum_proto_obj_name
)
return enum.default_value_string
else:
# Message type
return "None"
Expand Down
7 changes: 7 additions & 0 deletions tests/inputs/service/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ syntax = "proto3";

package service;

enum ThingType {
UNKNOWN = 0;
LIVING = 1;
DEAD = 2;
}

message DoThingRequest {
string name = 1;
repeated string comments = 2;
ThingType type = 3;
}

message DoThingResponse {
Expand Down
10 changes: 9 additions & 1 deletion tests/test_features.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import betterproto
from dataclasses import dataclass
from typing import Optional, List, Dict
from datetime import datetime, timedelta
from datetime import datetime
from inspect import signature


def test_has_field():
Expand Down Expand Up @@ -476,3 +477,10 @@ class Envelope(betterproto.Message):

msg.from_dict({"timestamps": iso_candidates})
assert all([isinstance(item, datetime) for item in msg.timestamps])


def test_enum_service_argument__expected_default_value():
from tests.output_betterproto.service.service import ThingType, TestStub

sig = signature(TestStub.do_thing)
assert sig.parameters["type"].default == ThingType.UNKNOWN