Skip to content

Commit af4b50f

Browse files
authored
Export UnicodeProtobufEnumAttribute and make prefix optional (#53)
* Add `UnicodeProtobufEnumAttribute` back to module `__init__.py` * Make enum prefix optional (default to empty string) so db stored enum.Name as-is by default Test: * unit test * `pip install -e ./pynamodb-attributes` from another local repo to test import and save
1 parent 1a74dea commit af4b50f

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

pynamodb_attributes/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from .unicode_datetime import UnicodeDatetimeAttribute
1313
from .unicode_delimited_tuple import UnicodeDelimitedTupleAttribute
1414
from .unicode_enum import UnicodeEnumAttribute
15+
from .unicode_protobuf_enum import UnicodeProtobufEnumAttribute
1516
from .uuid import UUIDAttribute
1617

1718
__all__ = [
@@ -22,6 +23,7 @@
2223
"IntegerEnumAttribute",
2324
"UnicodeDelimitedTupleAttribute",
2425
"UnicodeEnumAttribute",
26+
"UnicodeProtobufEnumAttribute",
2527
"TimedeltaAttribute",
2628
"TimedeltaMsAttribute",
2729
"TimedeltaUsAttribute",

pynamodb_attributes/unicode_protobuf_enum.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def __init__(
5050
enum_type: Type[_TProtobufEnum],
5151
*,
5252
unknown_value: Optional[_TProtobufEnum] = _fail,
53-
prefix: str,
53+
prefix: str = "",
5454
lower: bool = True,
5555
**kwargs: Any,
5656
) -> None:

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ universal = 1
44
[metadata]
55
license_file = LICENSE
66
name = pynamodb-attributes
7-
version = 0.5.0
7+
version = 0.5.1
88
description = Common attributes for PynamoDB
99
long_description = file:README.md
1010
long_description_content_type = text/markdown

tests/unicode_protobuf_enum_attribute_test.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from pynamodb.models import Model
1111
from typing_extensions import assert_type
1212

13-
from pynamodb_attributes.unicode_protobuf_enum import UnicodeProtobufEnumAttribute
13+
from pynamodb_attributes import UnicodeProtobufEnumAttribute
1414
from tests.connection import _connection
1515
from tests.meta import dynamodb_table_meta
1616

@@ -85,6 +85,12 @@ class MyModel(Model):
8585
prefix="SHAKE_FLAVOR_",
8686
null=True,
8787
)
88+
value_with_prefix = UnicodeProtobufEnumAttribute(
89+
diner_pb2.ShakeFlavor,
90+
unknown_value=diner_pb2.SHAKE_FLAVOR_UNKNOWN,
91+
null=True,
92+
lower=False,
93+
)
8894
map_attr = MyMapAttr(null=True)
8995

9096

@@ -140,6 +146,7 @@ def test_serialization_unknown_value_success(uuid_key):
140146
"value": {"S": "vanilla"},
141147
"value_upper": {"S": "VANILLA"},
142148
"value_with_unknown": {"S": "vanilla"},
149+
"value_with_prefix": {"S": "SHAKE_FLAVOR_VANILLA"},
143150
},
144151
),
145152
(
@@ -148,6 +155,7 @@ def test_serialization_unknown_value_success(uuid_key):
148155
"value": {"S": "chocolate"},
149156
"value_upper": {"S": "CHOCOLATE"},
150157
"value_with_unknown": {"S": "chocolate"},
158+
"value_with_prefix": {"S": "SHAKE_FLAVOR_CHOCOLATE"},
151159
},
152160
),
153161
],
@@ -162,6 +170,7 @@ def test_serialization(
162170
model.value = value
163171
model.value_upper = value
164172
model.value_with_unknown = value
173+
model.value_with_prefix = value
165174
model.save()
166175

167176
# verify underlying storage
@@ -173,6 +182,7 @@ def test_serialization(
173182
assert model.value == value
174183
assert model.value_upper == value
175184
assert model.value_with_unknown == value
185+
assert model.value_with_prefix == value
176186

177187

178188
def test_map_attribute( # exercises the __deepcopy__ method

0 commit comments

Comments
 (0)