Skip to content

Commit 12b1014

Browse files
hallieIlya Konstantinov
authored andcommitted
adding float attributes (#11)
1 parent b9a1fd9 commit 12b1014

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

pynamodb_attributes/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from .float import FloatAttribute
12
from .integer import IntegerAttribute
23
from .integer_date import IntegerDateAttribute
34
from .integer_enum import IntegerEnumAttribute
@@ -8,6 +9,7 @@
89
from .unicode_enum import UnicodeEnumAttribute
910

1011
__all__ = [
12+
'FloatAttribute',
1113
'IntegerAttribute',
1214
'IntegerDateAttribute',
1315
'IntegerEnumAttribute',

pynamodb_attributes/float.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from pynamodb.attributes import Attribute
2+
from pynamodb.attributes import NumberAttribute
3+
4+
5+
class FloatAttribute(Attribute):
6+
"""
7+
Unlike NumberAttribute, this attribute has its type hinted as 'float'.
8+
"""
9+
attr_type = NumberAttribute.attr_type
10+
serialize = NumberAttribute.serialize
11+
deserialize = NumberAttribute.deserialize

pynamodb_attributes/float.pyi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from ._typing import Attribute
2+
3+
4+
class FloatAttribute(Attribute[float]):
5+
...

tests/float_attribute_test.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import pytest
2+
from pynamodb.attributes import UnicodeAttribute
3+
from pynamodb.models import Model
4+
5+
from pynamodb_attributes import FloatAttribute
6+
from tests.meta import dynamodb_table_meta
7+
8+
9+
class MyModel(Model):
10+
Meta = dynamodb_table_meta(__name__)
11+
12+
key = UnicodeAttribute(hash_key=True)
13+
value = FloatAttribute(null=True)
14+
15+
16+
@pytest.fixture(scope='module', autouse=True)
17+
def create_table():
18+
MyModel.create_table()
19+
20+
21+
def test_serialization_non_null(uuid_key):
22+
model = MyModel()
23+
model.key = uuid_key
24+
model.value = 45.6
25+
model.save()
26+
27+
# verify underlying storage
28+
item = MyModel._get_connection().get_item(uuid_key)
29+
assert item['Item']['value'] == {'N': '45.6'}
30+
31+
# verify deserialization
32+
model = MyModel.get(uuid_key)
33+
assert model.value == 45.6
34+
35+
36+
def test_serialization_null(uuid_key):
37+
model = MyModel()
38+
model.key = uuid_key
39+
model.value = None
40+
model.save()
41+
42+
# verify underlying storage
43+
item = MyModel._get_connection().get_item(uuid_key)
44+
assert 'value' not in item['Item']
45+
46+
# verify deserialization
47+
model = MyModel.get(uuid_key)
48+
assert model.value is None

0 commit comments

Comments
 (0)