Skip to content

Commit bc218bb

Browse files
authored
Merge pull request #38 from digital-asset/python-enum-fix
python: Fix a bug that broke enum support.
2 parents 9a5035f + 3c51fe3 commit bc218bb

File tree

6 files changed

+26
-6
lines changed

6 files changed

+26
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
python/build
1414
python/dazl.egg-info
1515
python/dist
16+
python/pip-wheel-metadata
1617
app.log
1718
sandbox.log
1819
target

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6.2.1
1+
6.2.2

python/dazl/client/_writer_verify.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from ..model.core import ContractId
77
from ..model.types import Type, UnsupportedType, VariantType, RecordType, ListType, \
88
ContractIdType, TemplateChoice, TypeEvaluationContext, OptionalType, MapType, \
9-
UnresolvedTypeReference, TypeReference
9+
UnresolvedTypeReference, TypeReference, EnumType
1010
from ..model.writing import Command, CreateCommand, ExerciseCommand, ExerciseByKeyCommand, \
1111
CreateAndExerciseCommand, AbstractSerializer
1212
from ..util.prim_types import to_int, to_str, to_decimal, to_date, to_datetime, \
@@ -186,6 +186,13 @@ def serialize_variant(self, context: TypeEvaluationContext, tt: VariantType, obj
186186
error = 'variants must be encoded as dictionaries with a single key and a value'
187187
raise ValueError(f'{error} (got {obj!r} instead)', obj)
188188

189+
def serialize_enum(self, context: TypeEvaluationContext, tt: EnumType, obj: Any) -> Any:
190+
if obj in tt.constructors:
191+
return obj
192+
else:
193+
error = f'expected one of {tt.constructors}'
194+
raise ValueError(f'{error} (got {obj!r} instead')
195+
189196
def serialize_unsupported(self, context: TypeEvaluationContext, tt: UnsupportedType, obj: Any) \
190197
-> Any:
191198
return obj

python/dazl/protocols/v1/pb_parse_event.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,8 @@ def to_natural_type(context: TypeEvaluationContext, data_type: Type, obj: 'G.Val
403403
return to_list(context, data_type, obj.list)
404404
elif ctor == 'optional':
405405
return to_optional(context, data_type, obj.optional)
406+
elif ctor == 'enum':
407+
return to_enum(obj.enum)
406408
elif ctor == 'int64':
407409
return to_int64(obj.int64)
408410
elif ctor == 'decimal':
@@ -497,6 +499,10 @@ def process(child_context: TypeEvaluationContext, ot: OptionalType) -> list:
497499
return type_evaluate_dispatch_default_error(on_optional=process)(context, tt)
498500

499501

502+
def to_enum(enum: str) -> str:
503+
return enum
504+
505+
500506
def to_int64(int64: int) -> int:
501507
return int64
502508

python/dazl/protocols/v1/pb_ser_command.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from ...model.core import ContractId
1414
from ...model.types import VariantType, RecordType, ListType, ContractIdType, \
1515
UnsupportedType, TemplateChoice, TypeReference, TypeEvaluationContext, SCALAR_TYPE_UNIT, \
16-
MapType, OptionalType
16+
MapType, OptionalType, EnumType
1717
from ...model.writing import AbstractSerializer, CommandPayload
1818
from ...util.prim_types import to_boolean, to_date, to_datetime, to_decimal, to_int, to_str, \
1919
decode_variant_dict
@@ -244,6 +244,12 @@ def serialize_variant(self, context: TypeEvaluationContext, tt: VariantType, obj
244244
_set_value(variant_message.value, ctor, value)
245245
return 'variant', variant_message
246246

247+
def serialize_enum(self, context: TypeEvaluationContext, tt: EnumType, obj: Any) -> R:
248+
from ..._gen.com.digitalasset.ledger.api.v1.value_pb2 import Enum
249+
enum_message = Enum()
250+
enum_message.constructor = obj
251+
return 'enum', enum_message
252+
247253
def serialize_unsupported(
248254
self, context: TypeEvaluationContext, tt: UnsupportedType, obj: Any) -> R:
249255
raise Exception(f'UnsupportedType {tt} is not serializable in gRPC')
@@ -275,7 +281,7 @@ def _set_value(message: G.Value, ctor: 'Optional[str]', value) -> None:
275281
message.MergeFrom(value)
276282
elif ctor == 'unit':
277283
message.unit.SetInParent()
278-
elif ctor in ('record', 'variant', 'list', 'optional'):
284+
elif ctor in ('record', 'variant', 'list', 'optional', 'enum'):
279285
getattr(message, ctor).MergeFrom(value)
280286
else:
281287
setattr(message, ctor, value)

python/pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "dazl"
3-
version = "6.2.1"
3+
version = "6.2.2"
44
description = "high-level Ledger API client for DAML ledgers"
55
license = "Apache-2.0"
66
authors = ["Davin K. Tanabe <davin.tanabe@digitalasset.com>"]
@@ -13,7 +13,6 @@ keywords = ["daml", "blockchain", "dlt", "distributed ledger", "digital asset"]
1313
python = ">=3.6"
1414
aiohttp = "*"
1515
dataclasses = { version = "*", python = "~=3.6.0" }
16-
1716
google-auth = "*"
1817
grpcio = ">=1.20.1"
1918
oauthlib = "*"
@@ -24,6 +23,7 @@ pyyaml = "*"
2423
requests = "*"
2524
semver = "*"
2625
toposort = "*"
26+
typing_extensions = "*"
2727

2828
[tool.poetry.dev-dependencies]
2929
grpcio-tools = ">=1.20.1"

0 commit comments

Comments
 (0)