├── tests ├── __init__.py ├── conftest.py ├── connection.py ├── meta.py ├── float_attribute_test.py ├── integer_attribute_test.py ├── integer_set_attribute_test.py ├── integer_date_attribute_test.py ├── uuid_attribute_test.py ├── timedelta_attribute_test.py ├── timestamp_attribute_test.py ├── integer_enum_attribute_test.py ├── unicode_enum_attribute_test.py ├── unicode_delimited_tuple_attribute_test.py ├── unicode_protobuf_enum_attribute_test.py └── unicode_datetime_attribute_test.py ├── pynamodb_attributes ├── py.typed ├── float.py ├── integer.py ├── integer_set.py ├── integer_date.py ├── uuid.py ├── __init__.py ├── timedelta.py ├── timestamp.py ├── integer_enum.py ├── unicode_enum.py ├── unicode_datetime.py ├── unicode_delimited_tuple.py └── unicode_protobuf_enum.py ├── setup.py ├── NOTICE ├── Makefile ├── requirements.txt ├── CODE_OF_CONDUCT.md ├── .gitignore ├── .github └── workflows │ ├── release.yaml │ └── test.yaml ├── .pre-commit-config.yaml ├── README.md ├── setup.cfg └── LICENSE /tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /pynamodb_attributes/py.typed: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | 3 | setup() 4 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | pynamodb-attributes 2 | Copyright 2017-2018 Lyft Inc. 3 | 4 | This product includes software developed at Lyft Inc. 5 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: test 2 | test: 3 | py.test tests/ 4 | 5 | .PHONY: lint 6 | lint: 7 | pre-commit run --all-files --show-diff-on-failure 8 | -------------------------------------------------------------------------------- /tests/conftest.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | 4 | @pytest.fixture 5 | def uuid_key(): 6 | from uuid import uuid4 7 | 8 | return str(uuid4()) 9 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | mypy>=0.961 2 | pre-commit>=1.17.0 3 | pytest>=4.6.3 4 | pytest-cov>=2.7.1 5 | pytest-env>=0.6.2 6 | pytest-mock>=1.10.4 7 | typing-extensions 8 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | This project is governed by [Lyft's code of 2 | conduct](https://github.com/lyft/code-of-conduct). All contributors 3 | and participants agree to abide by its terms. 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.pyc 3 | *.pyo 4 | *.pyt 5 | *.pytc 6 | *.egg-info 7 | .*.swp 8 | .DS_Store 9 | .venv/ 10 | venv/ 11 | venv3/ 12 | .cache/ 13 | build/ 14 | .idea/ 15 | .coverage 16 | .mypy_cache 17 | .pytest_cache 18 | .dmypy.json 19 | coverage.xml 20 | -------------------------------------------------------------------------------- /tests/connection.py: -------------------------------------------------------------------------------- 1 | from typing import Type 2 | 3 | from pynamodb.connection.table import TableConnection 4 | from pynamodb.models import Model 5 | 6 | 7 | def _connection(model: Type[Model]) -> TableConnection: 8 | # pynamodb typestubs don't have private attrs 9 | return model._get_connection() # type: ignore 10 | -------------------------------------------------------------------------------- /tests/meta.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | 4 | def dynamodb_table_meta(table_suffix): 5 | class Meta: 6 | region = os.getenv("DYNAMODB_REGION", "us-east-1") 7 | host = os.getenv("DYNAMODB_URL", "http://localhost:8000") 8 | table_name = f"pynamodb-attributes-{table_suffix}" 9 | read_capacity_units = 10 10 | write_capacity_units = 10 11 | 12 | return Meta 13 | -------------------------------------------------------------------------------- /pynamodb_attributes/float.py: -------------------------------------------------------------------------------- 1 | from pynamodb.attributes import Attribute 2 | from pynamodb.attributes import NumberAttribute 3 | 4 | 5 | class FloatAttribute(Attribute[float]): 6 | """ 7 | Unlike NumberAttribute, this attribute has its type hinted as 'float'. 8 | """ 9 | 10 | attr_type = NumberAttribute.attr_type 11 | serialize = NumberAttribute.serialize # type: ignore 12 | deserialize = NumberAttribute.deserialize # type: ignore 13 | -------------------------------------------------------------------------------- /pynamodb_attributes/integer.py: -------------------------------------------------------------------------------- 1 | from pynamodb.attributes import Attribute 2 | from pynamodb.attributes import NumberAttribute 3 | 4 | 5 | class IntegerAttribute(Attribute[int]): 6 | """ 7 | Unlike NumberAttribute, this attribute has its type hinted as 'int'. 8 | """ 9 | 10 | attr_type = NumberAttribute.attr_type 11 | serialize = NumberAttribute.serialize # type: ignore 12 | deserialize = NumberAttribute.deserialize # type: ignore 13 | -------------------------------------------------------------------------------- /pynamodb_attributes/integer_set.py: -------------------------------------------------------------------------------- 1 | from typing import Set 2 | 3 | from pynamodb.attributes import Attribute 4 | from pynamodb.attributes import NumberSetAttribute 5 | 6 | 7 | class IntegerSetAttribute(Attribute[Set[int]]): 8 | """ 9 | Unlike NumberSetAttribute, this attribute has its type hinted as 'Set[int]'. 10 | """ 11 | 12 | attr_type = NumberSetAttribute.attr_type 13 | null = NumberSetAttribute.null 14 | serialize = NumberSetAttribute.serialize # type: ignore 15 | deserialize = NumberSetAttribute.deserialize # type: ignore 16 | -------------------------------------------------------------------------------- /pynamodb_attributes/integer_date.py: -------------------------------------------------------------------------------- 1 | import json 2 | from datetime import date 3 | 4 | from pynamodb.attributes import Attribute 5 | 6 | from pynamodb_attributes import IntegerAttribute 7 | 8 | 9 | class IntegerDateAttribute(Attribute[date]): 10 | """Represents a date as an integer (e.g. 2015_12_31 for December 31st, 2015).""" 11 | 12 | attr_type = IntegerAttribute.attr_type 13 | 14 | def serialize(self, value: date) -> str: 15 | return json.dumps(value.year * 1_00_00 + value.month * 1_00 + value.day) 16 | 17 | def deserialize(self, value: str) -> date: 18 | n = json.loads(value) 19 | return date(n // 1_00_00, n // 1_00 % 1_00, n % 1_00) 20 | -------------------------------------------------------------------------------- /.github/workflows/release.yaml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: 4 | release: 5 | types: [published] 6 | 7 | jobs: 8 | deploy: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - uses: actions/checkout@v2 12 | 13 | - name: Set up Python 14 | uses: actions/setup-python@v2 15 | with: 16 | python-version: "3.x" 17 | 18 | - name: Install dependencies 19 | run: | 20 | python -m pip install --upgrade pip 21 | pip install setuptools wheel twine 22 | python -m pip install -e . -r requirements.txt 23 | 24 | - name: Build packages 25 | run: | 26 | python setup.py sdist bdist_wheel 27 | 28 | - name: Publish to PyPI 29 | uses: pypa/gh-action-pypi-publish@release/v1 30 | with: 31 | user: __token__ 32 | password: ${{ secrets.PYPI_API_TOKEN }} 33 | -------------------------------------------------------------------------------- /pynamodb_attributes/uuid.py: -------------------------------------------------------------------------------- 1 | from typing import Any 2 | from uuid import UUID 3 | 4 | import pynamodb.constants 5 | from pynamodb.attributes import Attribute 6 | 7 | 8 | class UUIDAttribute(Attribute[UUID]): 9 | """ 10 | PynamoDB attribute to for UUIDs. These are backed by DynamoDB unicode (`S`) types. 11 | """ 12 | 13 | attr_type = pynamodb.constants.STRING 14 | 15 | def __init__(self, remove_dashes: bool = False, **kwargs: Any) -> None: 16 | """ 17 | Initializes a UUIDAttribute object. 18 | 19 | :param remove_dashes: if set, the string serialization will be without dashes. 20 | Defaults to False. 21 | """ 22 | super().__init__(**kwargs) 23 | self._remove_dashes = remove_dashes 24 | 25 | def serialize(self, value: UUID) -> str: 26 | result = str(value) 27 | 28 | if self._remove_dashes: 29 | result = result.replace("-", "") 30 | 31 | return result 32 | 33 | def deserialize(self, value: str) -> UUID: 34 | return UUID(value) 35 | -------------------------------------------------------------------------------- /.github/workflows/test.yaml: -------------------------------------------------------------------------------- 1 | name: Tests 2 | on: 3 | push: 4 | branches: [master] 5 | 6 | pull_request: 7 | 8 | jobs: 9 | test: 10 | runs-on: ubuntu-latest 11 | strategy: 12 | matrix: 13 | python-version: 14 | - "3.8" 15 | - "3.9" 16 | 17 | services: 18 | dynamodb-local: 19 | image: "amazon/dynamodb-local:latest" 20 | ports: 21 | - "8000:8000" 22 | 23 | steps: 24 | - uses: actions/checkout@v2 25 | 26 | - uses: actions/setup-python@v2 27 | with: 28 | python-version: ${{ matrix.python-version }} 29 | 30 | - name: install deps 31 | run: | 32 | python -m pip install -e . -r requirements.txt 33 | 34 | - name: run tests 35 | run: make test 36 | 37 | lint: 38 | runs-on: ubuntu-latest 39 | steps: 40 | - uses: actions/checkout@v2 41 | 42 | - uses: actions/setup-python@v2 43 | with: 44 | python-version: 3.8 45 | 46 | - name: Install pre-commit 47 | run: pip install pre-commit 48 | 49 | - run: make lint 50 | -------------------------------------------------------------------------------- /pynamodb_attributes/__init__.py: -------------------------------------------------------------------------------- 1 | from .float import FloatAttribute 2 | from .integer import IntegerAttribute 3 | from .integer_date import IntegerDateAttribute 4 | from .integer_enum import IntegerEnumAttribute 5 | from .integer_set import IntegerSetAttribute 6 | from .timedelta import TimedeltaAttribute 7 | from .timedelta import TimedeltaMsAttribute 8 | from .timedelta import TimedeltaUsAttribute 9 | from .timestamp import TimestampAttribute 10 | from .timestamp import TimestampMsAttribute 11 | from .timestamp import TimestampUsAttribute 12 | from .unicode_datetime import UnicodeDatetimeAttribute 13 | from .unicode_delimited_tuple import UnicodeDelimitedTupleAttribute 14 | from .unicode_enum import UnicodeEnumAttribute 15 | from .unicode_protobuf_enum import UnicodeProtobufEnumAttribute 16 | from .uuid import UUIDAttribute 17 | 18 | __all__ = [ 19 | "FloatAttribute", 20 | "IntegerAttribute", 21 | "IntegerSetAttribute", 22 | "IntegerDateAttribute", 23 | "IntegerEnumAttribute", 24 | "UnicodeDelimitedTupleAttribute", 25 | "UnicodeEnumAttribute", 26 | "UnicodeProtobufEnumAttribute", 27 | "TimedeltaAttribute", 28 | "TimedeltaMsAttribute", 29 | "TimedeltaUsAttribute", 30 | "TimestampAttribute", 31 | "TimestampMsAttribute", 32 | "TimestampUsAttribute", 33 | "UUIDAttribute", 34 | "UnicodeDatetimeAttribute", 35 | ] 36 | -------------------------------------------------------------------------------- /tests/float_attribute_test.py: -------------------------------------------------------------------------------- 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.connection import _connection 7 | from tests.meta import dynamodb_table_meta 8 | 9 | 10 | class MyModel(Model): 11 | Meta = dynamodb_table_meta(__name__) 12 | 13 | key = UnicodeAttribute(hash_key=True) 14 | value = FloatAttribute(null=True) 15 | 16 | 17 | @pytest.fixture(scope="module", autouse=True) 18 | def create_table(): 19 | MyModel.create_table() 20 | 21 | 22 | def test_serialization_non_null(uuid_key): 23 | model = MyModel() 24 | model.key = uuid_key 25 | model.value = 45.6 26 | model.save() 27 | 28 | # verify underlying storage 29 | item = _connection(MyModel).get_item(uuid_key) 30 | assert item["Item"]["value"] == {"N": "45.6"} 31 | 32 | # verify deserialization 33 | model = MyModel.get(uuid_key) 34 | assert model.value == 45.6 35 | 36 | 37 | def test_serialization_null(uuid_key): 38 | model = MyModel() 39 | model.key = uuid_key 40 | model.value = None 41 | model.save() 42 | 43 | # verify underlying storage 44 | item = _connection(MyModel).get_item(uuid_key) 45 | assert "value" not in item["Item"] 46 | 47 | # verify deserialization 48 | model = MyModel.get(uuid_key) 49 | assert model.value is None 50 | -------------------------------------------------------------------------------- /tests/integer_attribute_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | from pynamodb.attributes import UnicodeAttribute 3 | from pynamodb.models import Model 4 | from typing_extensions import assert_type 5 | 6 | from pynamodb_attributes import IntegerAttribute 7 | from tests.connection import _connection 8 | from tests.meta import dynamodb_table_meta 9 | 10 | 11 | class MyModel(Model): 12 | Meta = dynamodb_table_meta(__name__) 13 | 14 | key = UnicodeAttribute(hash_key=True) 15 | value = IntegerAttribute(null=True) 16 | 17 | 18 | assert_type(MyModel.value, IntegerAttribute) 19 | assert_type(MyModel().value, int) 20 | 21 | 22 | @pytest.fixture(scope="module", autouse=True) 23 | def create_table(): 24 | MyModel.create_table() 25 | 26 | 27 | def test_serialization_non_null(uuid_key): 28 | model = MyModel() 29 | model.key = uuid_key 30 | model.value = 456 31 | model.save() 32 | 33 | # verify underlying storage 34 | item = _connection(MyModel).get_item(uuid_key) 35 | assert item["Item"]["value"] == {"N": "456"} 36 | 37 | # verify deserialization 38 | model = MyModel.get(uuid_key) 39 | assert model.value == 456 40 | 41 | 42 | def test_serialization_null(uuid_key): 43 | model = MyModel() 44 | model.key = uuid_key 45 | model.value = None 46 | model.save() 47 | 48 | # verify underlying storage 49 | item = _connection(MyModel).get_item(uuid_key) 50 | assert "value" not in item["Item"] 51 | 52 | # verify deserialization 53 | model = MyModel.get(uuid_key) 54 | assert model.value is None 55 | -------------------------------------------------------------------------------- /tests/integer_set_attribute_test.py: -------------------------------------------------------------------------------- 1 | from typing import Set 2 | 3 | import pytest 4 | from pynamodb.attributes import UnicodeAttribute 5 | from pynamodb.models import Model 6 | from typing_extensions import assert_type 7 | 8 | from pynamodb_attributes import IntegerSetAttribute 9 | from tests.connection import _connection 10 | from tests.meta import dynamodb_table_meta 11 | 12 | 13 | class MyModel(Model): 14 | Meta = dynamodb_table_meta(__name__) 15 | 16 | key = UnicodeAttribute(hash_key=True) 17 | value = IntegerSetAttribute(null=True) 18 | 19 | 20 | assert_type(MyModel.value, IntegerSetAttribute) 21 | assert_type(MyModel().value, Set[int]) 22 | 23 | 24 | @pytest.fixture(scope="module", autouse=True) 25 | def create_table(): 26 | MyModel.create_table() 27 | 28 | 29 | def test_serialization_non_null(uuid_key): 30 | model = MyModel() 31 | model.key = uuid_key 32 | model.value = {456, 789} 33 | model.save() 34 | 35 | # verify underlying storage 36 | item = _connection(MyModel).get_item(uuid_key) 37 | assert item["Item"]["value"] == {"NS": ["456", "789"]} 38 | 39 | # verify deserialization 40 | model = MyModel.get(uuid_key) 41 | assert model.value == {456, 789} 42 | 43 | 44 | def test_serialization_null(uuid_key): 45 | model = MyModel() 46 | model.key = uuid_key 47 | model.value = None 48 | model.save() 49 | 50 | # verify underlying storage 51 | item = _connection(MyModel).get_item(uuid_key) 52 | assert "value" not in item["Item"] 53 | 54 | # verify deserialization 55 | model = MyModel.get(uuid_key) 56 | assert model.value is None 57 | -------------------------------------------------------------------------------- /pynamodb_attributes/timedelta.py: -------------------------------------------------------------------------------- 1 | from datetime import timedelta 2 | from typing import Any 3 | from typing import Optional 4 | 5 | import pynamodb 6 | from pynamodb.attributes import Attribute 7 | 8 | 9 | class TimedeltaAttribute(Attribute[timedelta]): 10 | """ 11 | Stores a timedelta as a number of seconds (truncated). 12 | 13 | >>> class MyModel(Model): 14 | >>> delta = TimedeltaAttribute(default=lambda: timedelta(seconds=5)) 15 | >>> delta_ms = TimedeltaMsAttribute(default=lambda: timedelta(milliseconds=500)) 16 | """ 17 | 18 | attr_type = pynamodb.constants.NUMBER 19 | _multiplier = 1.0 20 | 21 | def deserialize(self, value: str) -> timedelta: 22 | return timedelta(microseconds=float(value) * (1000000.0 / self._multiplier)) 23 | 24 | def serialize(self, td: timedelta) -> str: 25 | return str(int(td.total_seconds() * self._multiplier)) 26 | 27 | def __set__(self, instance: Any, value: Optional[Any]) -> None: 28 | if value is not None and not isinstance(value, timedelta): 29 | raise TypeError( 30 | f"value has invalid type '{type(value)}'; Optional[timedelta] expected", 31 | ) 32 | return super().__set__(instance, value) 33 | 34 | 35 | class TimedeltaMsAttribute(TimedeltaAttribute): 36 | """ 37 | Stores a timedelta as a number of milliseconds AKA ms (truncated). 38 | """ 39 | 40 | _multiplier = 1000.0 41 | 42 | 43 | class TimedeltaUsAttribute(TimedeltaAttribute): 44 | """ 45 | Stores a timedelta as a number of microseconds AKA μs (truncated). 46 | """ 47 | 48 | _multiplier = 1000000.0 49 | -------------------------------------------------------------------------------- /tests/integer_date_attribute_test.py: -------------------------------------------------------------------------------- 1 | from datetime import date 2 | from unittest.mock import ANY 3 | 4 | import pytest 5 | from pynamodb.attributes import UnicodeAttribute 6 | from pynamodb.models import Model 7 | from typing_extensions import assert_type 8 | 9 | from pynamodb_attributes.integer_date import IntegerDateAttribute 10 | from tests.connection import _connection 11 | from tests.meta import dynamodb_table_meta 12 | 13 | 14 | class MyModel(Model): 15 | Meta = dynamodb_table_meta(__name__) 16 | 17 | key = UnicodeAttribute(hash_key=True) 18 | value = IntegerDateAttribute(null=True) 19 | 20 | 21 | assert_type(MyModel.value, IntegerDateAttribute) 22 | assert_type(MyModel().value, date) 23 | 24 | 25 | @pytest.fixture(scope="module", autouse=True) 26 | def create_table(): 27 | MyModel.create_table() 28 | 29 | 30 | def test_serialization_non_null(uuid_key): 31 | model = MyModel() 32 | model.key = uuid_key 33 | model.value = date(2015, 12, 31) 34 | model.save() 35 | 36 | # verify underlying storage 37 | item = _connection(MyModel).get_item(uuid_key) 38 | assert item["Item"] == {"key": ANY, "value": {"N": "20151231"}} 39 | 40 | # verify deserialization 41 | model = MyModel.get(uuid_key) 42 | assert model.value.year == 2015 43 | assert model.value.month == 12 44 | assert model.value.day == 31 45 | 46 | 47 | def test_serialization_null(uuid_key): 48 | model = MyModel() 49 | model.key = uuid_key 50 | model.value = None 51 | model.save() 52 | 53 | # verify underlying storage 54 | item = _connection(MyModel).get_item(uuid_key) 55 | assert "value" not in item["Item"] 56 | 57 | # verify deserialization 58 | model = MyModel.get(uuid_key) 59 | assert model.value is None 60 | -------------------------------------------------------------------------------- /.pre-commit-config.yaml: -------------------------------------------------------------------------------- 1 | repos: 2 | - repo: https://github.com/psf/black 3 | rev: 22.6.0 4 | hooks: 5 | - id: black 6 | - repo: https://github.com/asottile/reorder_python_imports 7 | rev: v3.3.0 8 | hooks: 9 | - id: reorder-python-imports 10 | - repo: https://github.com/asottile/add-trailing-comma 11 | rev: v2.2.3 12 | hooks: 13 | - id: add-trailing-comma 14 | - repo: https://github.com/pre-commit/pre-commit-hooks 15 | rev: v4.3.0 16 | hooks: 17 | - id: check-ast 18 | - id: check-docstring-first 19 | - id: check-executables-have-shebangs 20 | - id: check-json 21 | - id: check-merge-conflict 22 | - id: check-yaml 23 | - id: debug-statements 24 | - id: end-of-file-fixer 25 | - id: trailing-whitespace 26 | - repo: https://github.com/pycqa/flake8 27 | rev: 3.9.2 28 | hooks: 29 | - id: flake8 30 | additional_dependencies: 31 | - flake8-bugbear 32 | - flake8-builtins 33 | - flake8-comprehensions 34 | - repo: https://github.com/asottile/yesqa 35 | rev: v1.3.0 36 | hooks: 37 | - id: yesqa 38 | additional_dependencies: 39 | - flake8==3.8.3 40 | - flake8-bugbear 41 | - flake8-builtins 42 | - flake8-comprehensions 43 | - repo: https://github.com/asottile/pyupgrade 44 | rev: v2.34.0 45 | hooks: 46 | - id: pyupgrade 47 | - repo: https://github.com/pre-commit/mirrors-mypy 48 | rev: v0.961 49 | hooks: 50 | - id: mypy 51 | additional_dependencies: 52 | - pynamodb==5.0.0 53 | -------------------------------------------------------------------------------- /pynamodb_attributes/timestamp.py: -------------------------------------------------------------------------------- 1 | from datetime import datetime 2 | from datetime import timezone 3 | from typing import Any 4 | from typing import Optional 5 | 6 | import pynamodb.constants 7 | from pynamodb.attributes import Attribute 8 | 9 | 10 | class TimestampAttribute(Attribute[datetime]): 11 | """ 12 | Stores time as a Unix epoch timestamp (in seconds) in a DynamoDB number. 13 | 14 | >>> class MyModel(Model): 15 | >>> created_at_seconds = TimestampAttribute(default=lambda: datetime.now(tz=timezone.utc)) 16 | >>> created_at_ms = TimestampMsAttribute(default=lambda: datetime.now(tz=timezone.utc)) 17 | """ 18 | 19 | attr_type = pynamodb.constants.NUMBER 20 | _multiplier = 1.0 21 | 22 | def deserialize(self, value: str) -> datetime: 23 | return datetime.fromtimestamp(int(value) / self._multiplier, tz=timezone.utc) 24 | 25 | def serialize(self, value: datetime) -> str: 26 | return str(int(value.timestamp() * self._multiplier)) 27 | 28 | def __set__(self, instance: Any, value: Optional[Any]) -> None: 29 | if value is not None: 30 | if not isinstance(value, datetime): 31 | raise TypeError( 32 | f"value has invalid type '{type(value)}'; datetime expected", 33 | ) 34 | if value.tzinfo is None or value.tzinfo.utcoffset(value) is None: 35 | raise TypeError("aware datetime expected") 36 | return super().__set__(instance, value) 37 | 38 | 39 | class TimestampMsAttribute(TimestampAttribute): 40 | """ 41 | Stores time as a Unix epoch timestamp in milliseconds (ms) in a DynamoDB number. 42 | """ 43 | 44 | _multiplier = 1000.0 45 | 46 | 47 | class TimestampUsAttribute(TimestampAttribute): 48 | """ 49 | Stores times as a Unix epoch timestamp in microseconds (μs) in a DynamoDB number. 50 | """ 51 | 52 | _multiplier = 1000000.0 53 | -------------------------------------------------------------------------------- /pynamodb_attributes/integer_enum.py: -------------------------------------------------------------------------------- 1 | from enum import Enum 2 | from typing import Any 3 | from typing import Optional 4 | from typing import Type 5 | from typing import TypeVar 6 | 7 | import pynamodb.constants 8 | from pynamodb.attributes import Attribute 9 | 10 | T = TypeVar("T", bound=Enum) 11 | _fail: Any = object() 12 | 13 | 14 | class IntegerEnumAttribute(Attribute[T]): 15 | """ 16 | Stores integer enumerations (Enums whose values are integers) as DynamoDB numbers. 17 | 18 | >>> from enum import Enum 19 | >>> 20 | >>> from pynamodb.models import Model 21 | >>> 22 | >>> class ShakeFlavor(Enum): 23 | >>> VANILLA = 1 24 | >>> CHOCOLATE = 2 25 | >>> COOKIES = 3 26 | >>> MINT = 4 27 | >>> 28 | >>> class Shake(Model): 29 | >>> flavor = IntegerEnumAttribute(ShakeFlavor) 30 | """ 31 | 32 | attr_type = pynamodb.constants.NUMBER 33 | 34 | def __init__( 35 | self, enum_type: Type[T], unknown_value: Optional[T] = _fail, **kwargs: Any 36 | ) -> None: 37 | """ 38 | :param enum_type: The type of the enum 39 | """ 40 | super().__init__(**kwargs) 41 | self.enum_type = enum_type 42 | self.unknown_value = unknown_value 43 | if not all(isinstance(e.value, int) for e in self.enum_type): 44 | raise TypeError(f"Enumeration '{self.enum_type}' values must be all ints") 45 | 46 | def deserialize(self, value: str) -> Optional[T]: 47 | try: 48 | return self.enum_type(int(value)) 49 | except ValueError: 50 | if self.unknown_value is _fail: 51 | raise 52 | return self.unknown_value 53 | 54 | def serialize(self, value: T) -> str: 55 | if not isinstance(value, self.enum_type): 56 | raise TypeError( 57 | f"value has invalid type '{type(value)}'; expected '{self.enum_type}'", 58 | ) 59 | return str(value.value) 60 | -------------------------------------------------------------------------------- /pynamodb_attributes/unicode_enum.py: -------------------------------------------------------------------------------- 1 | from enum import Enum 2 | from typing import Any 3 | from typing import Optional 4 | from typing import Type 5 | from typing import TypeVar 6 | 7 | import pynamodb.constants 8 | from pynamodb.attributes import Attribute 9 | 10 | T = TypeVar("T", bound=Enum) 11 | _fail: Any = object() 12 | 13 | 14 | class UnicodeEnumAttribute(Attribute[T]): 15 | """ 16 | Stores string enumerations (Enums whose values are strings) as DynamoDB strings. 17 | 18 | >>> from enum import Enum 19 | >>> 20 | >>> from pynamodb.models import Model 21 | >>> 22 | >>> class ShakeFlavor(Enum): 23 | >>> VANILLA = 'vanilla' 24 | >>> CHOCOLATE = 'chocolate' 25 | >>> COOKIES = 'cookies' 26 | >>> MINT = 'mint' 27 | >>> 28 | >>> class Shake(Model): 29 | >>> flavor = UnicodeEnumAttribute(ShakeFlavor) 30 | """ 31 | 32 | attr_type = pynamodb.constants.STRING 33 | 34 | def __init__( 35 | self, enum_type: Type[T], unknown_value: Optional[T] = _fail, **kwargs: Any 36 | ) -> None: 37 | """ 38 | :param enum_type: The type of the enum 39 | """ 40 | super().__init__(**kwargs) 41 | self.enum_type = enum_type 42 | self.unknown_value = unknown_value 43 | if not all(isinstance(e.value, str) for e in self.enum_type): 44 | raise TypeError( 45 | f"Enumeration '{self.enum_type}' values must be all strings", 46 | ) 47 | 48 | def deserialize(self, value: str) -> Optional[T]: 49 | try: 50 | return self.enum_type(value) 51 | except ValueError: 52 | if self.unknown_value is _fail: 53 | raise 54 | return self.unknown_value 55 | 56 | def serialize(self, value: T) -> str: 57 | if not isinstance(value, self.enum_type): 58 | raise TypeError( 59 | f"value has invalid type '{type(value)}'; expected '{self.enum_type}'", 60 | ) 61 | return value.value 62 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This Python 3 library contains compound and high-level PynamoDB attributes: 2 | 3 | - `FloatAttribute` – same as `NumberAttribute` but whose value is typed as `float` 4 | - `IntegerAttribute` – same as `NumberAttribute` but whose value is typed as `int` (rather than `float`) 5 | - `IntegerSetAttribute` – same as `NumberSetAttribute` but whose value is typed as `int` (rather than `float`) 6 | - `UnicodeDelimitedTupleAttribute` - a delimiter-separated value, useful for storing composite keys 7 | - `UnicodeEnumAttribute` - serializes a string-valued `Enum` into a Unicode (`S`-typed) attribute 8 | - `UnicodeProtobufEnumAttribute` - serializes a Protobuf enum into a Unicode (`S`-typed) attribute 9 | - `IntegerEnumAttribute` - serializes a int-valued `Enum` into a number (`N`-typed) attribute 10 | - `TimedeltaAttribute`, `TimedeltaMsAttribute`, `TimedeltaUsAttribute` – serializes `timedelta`s as integer seconds, milliseconds (ms) or microseconds (µs) 11 | - `TimestampAttribute`, `TimestampMsAttribute`, `TimestampUsAttribute` – serializes `datetime`s as Unix epoch seconds, milliseconds (ms) or microseconds (µs) 12 | - `IntegerDateAttribute` - serializes `date` as an integer representing the Gregorian date (_e.g._ `20181231`) 13 | - `UUIDAttribute` - serializes a `UUID` Python object as a `S` type attribute (_e.g._ `'a8098c1a-f86e-11da-bd1a-00112444be1e'`) 14 | - `UnicodeDatetimeAttribute` - ISO8601 datetime strings with offset information 15 | 16 | ## Testing 17 | 18 | The tests in this repository use an in-memory implementation of [`dynamodb`](https://aws.amazon.com/dynamodb). To run the tests locally, make sure [DynamoDB Local](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html) is running. It is available as a standalone binary, through package managers (e.g. [Homebrew](https://formulae.brew.sh/cask/dynamodb-local)) or as a Docker container: 19 | ```shell 20 | docker run -d -p 8000:8000 amazon/dynamodb-local 21 | ``` 22 | 23 | Afterwards, run tests as usual: 24 | ```shell 25 | pytest tests 26 | ``` 27 | -------------------------------------------------------------------------------- /pynamodb_attributes/unicode_datetime.py: -------------------------------------------------------------------------------- 1 | from datetime import datetime 2 | from datetime import timezone 3 | from typing import Any 4 | from typing import Optional 5 | 6 | import pynamodb.attributes 7 | from pynamodb.attributes import Attribute 8 | 9 | 10 | class UnicodeDatetimeAttribute(Attribute[datetime]): 11 | """ 12 | Stores a 'datetime.datetime' object as an ISO8601 formatted string 13 | 14 | This is useful for wanting database readable datetime objects that also sort. 15 | 16 | >>> class MyModel(Model): 17 | >>> created_at = UnicodeDatetimeAttribute() 18 | """ 19 | 20 | attr_type = pynamodb.attributes.STRING 21 | 22 | def __init__( 23 | self, 24 | *, 25 | force_tz: bool = True, 26 | force_utc: bool = False, 27 | fmt: Optional[str] = None, 28 | **kwargs: Any, 29 | ) -> None: 30 | """ 31 | :param force_tz: If set it will add timezone info to the `datetime` value if no `tzinfo` is currently 32 | set before serializing, defaults to `True` 33 | :param force_utc: If set it will normalize the `datetime` to UTC before serializing the value 34 | :param fmt: If set this value will be used to format the `datetime` object for serialization 35 | and deserialization 36 | """ 37 | 38 | super().__init__(**kwargs) 39 | self._force_tz = force_tz 40 | self._force_utc = force_utc 41 | self._fmt = fmt 42 | 43 | def deserialize(self, value: str) -> datetime: 44 | return ( 45 | datetime.fromisoformat(value) 46 | if self._fmt is None 47 | else datetime.strptime(value, self._fmt) 48 | ) 49 | 50 | def serialize(self, value: datetime) -> str: 51 | if self._force_tz and value.tzinfo is None: 52 | value = value.replace(tzinfo=timezone.utc) 53 | if self._force_utc: 54 | value = value.astimezone(tz=timezone.utc) 55 | return value.isoformat() if self._fmt is None else value.strftime(self._fmt) 56 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [wheel] 2 | universal = 1 3 | 4 | [metadata] 5 | license_file = LICENSE 6 | name = pynamodb-attributes 7 | version = 0.5.1 8 | description = Common attributes for PynamoDB 9 | long_description = file:README.md 10 | long_description_content_type = text/markdown 11 | url = https://www.github.com/lyft/pynamodb-attributes 12 | maintainer = Lyft 13 | maintainer_email = ikonstantinov@lyft.com 14 | classifiers = 15 | Programming Language :: Python :: 3 16 | Programming Language :: Python :: 3 :: Only 17 | 18 | [options] 19 | packages = find: 20 | install_requires= 21 | pynamodb>=5.0.0 22 | python_requires = >=3 23 | 24 | [options.package_data] 25 | pynamodb_attributes = 26 | py.typed 27 | 28 | [flake8] 29 | format = pylint 30 | max-complexity = 10 31 | max-line-length = 120 32 | ignore = E203 33 | 34 | [tool:pytest] 35 | addopts = --cov=pynamodb_attributes --cov-report=term-missing:skip-covered --cov-report=xml --cov-report=html -vvv 36 | env = 37 | # We don't need real AWS access in unit tests 38 | D:AWS_ACCESS_KEY_ID=1 39 | D:AWS_SECRET_ACCESS_KEY=1 40 | 41 | [coverage:run] 42 | branch = True 43 | 44 | [coverage:report] 45 | fail_under = 100 46 | exclude_lines = 47 | # the 'DEFAULT_EXCLUDE' from coverage.config: 48 | \#\s*(pragma|PRAGMA)[:\s]?\s*(no|NO)\s*(cover|COVER) 49 | # Exclude type-checker-only "code" 50 | if TYPE_CHECKING: 51 | # Exclude ellipsis bodies of type-stub functions 52 | ^\s*\.\.\.\s*$ 53 | # Exclude pytest.fail calls 54 | pytest\.fail 55 | # Exclude intentionally unimplemented branches 56 | raise NotImplementedError 57 | 58 | [coverage:xml] 59 | output = build/coverage.xml 60 | 61 | [coverage:html] 62 | directory = build/coverage_html 63 | 64 | [mypy] 65 | check_untyped_defs = True 66 | disallow_any_generics = True 67 | disallow_incomplete_defs = True 68 | disallow_untyped_defs = True 69 | ignore_missing_imports = True 70 | strict_equality = True 71 | strict_optional = True 72 | warn_no_return = True 73 | warn_redundant_casts = True 74 | 75 | [mypy-tests.*] 76 | disallow_untyped_defs = False 77 | -------------------------------------------------------------------------------- /tests/uuid_attribute_test.py: -------------------------------------------------------------------------------- 1 | from uuid import UUID 2 | 3 | import pytest 4 | from pynamodb.attributes import UnicodeAttribute 5 | from pynamodb.models import Model 6 | from typing_extensions import assert_type 7 | 8 | from pynamodb_attributes import UUIDAttribute 9 | from tests.connection import _connection 10 | from tests.meta import dynamodb_table_meta 11 | 12 | 13 | class MyModel(Model): 14 | Meta = dynamodb_table_meta(__name__) 15 | 16 | key = UnicodeAttribute(hash_key=True) 17 | value = UUIDAttribute(null=True) 18 | 19 | 20 | assert_type(MyModel.value, UUIDAttribute) 21 | assert_type(MyModel().value, UUID) 22 | 23 | 24 | @pytest.fixture(scope="module", autouse=True) 25 | def create_table(): 26 | MyModel.create_table() 27 | 28 | 29 | def test_deserialization_no_dashes(): 30 | uuid_attribute = UUIDAttribute(remove_dashes=True) 31 | uuid_str_no_dashes = "19c4f2515e364cc0bfeb983dd5d2bacd" 32 | 33 | assert UUID("19c4f251-5e36-4cc0-bfeb-983dd5d2bacd") == uuid_attribute.deserialize( 34 | uuid_str_no_dashes, 35 | ) 36 | 37 | 38 | def test_serialization_no_dashes(): 39 | uuid_attribute = UUIDAttribute(remove_dashes=True) 40 | uuid_value = UUID("19c4f251-5e36-4cc0-bfeb-983dd5d2bacd") 41 | 42 | assert "19c4f2515e364cc0bfeb983dd5d2bacd" == uuid_attribute.serialize(uuid_value) 43 | 44 | 45 | def test_serialization_non_null(uuid_key): 46 | model = MyModel() 47 | model.key = uuid_key 48 | uuid_str = "19c4f251-5e36-4cc0-bfeb-983dd5d2bacd" 49 | uuid_value = UUID(uuid_str) 50 | model.value = uuid_value 51 | model.save() 52 | 53 | # verify underlying storage 54 | item = _connection(MyModel).get_item(uuid_key) 55 | assert item["Item"]["value"] == {"S": uuid_str} 56 | 57 | # verify deserialization 58 | model = MyModel.get(uuid_key) 59 | assert model.value == uuid_value 60 | 61 | 62 | def test_serialization_null(uuid_key): 63 | model = MyModel() 64 | model.key = uuid_key 65 | model.value = None 66 | model.save() 67 | 68 | # verify underlying storage 69 | item = _connection(MyModel).get_item(uuid_key) 70 | assert "value" not in item["Item"] 71 | 72 | # verify deserialization 73 | model = MyModel.get(uuid_key) 74 | assert model.value is None 75 | -------------------------------------------------------------------------------- /tests/timedelta_attribute_test.py: -------------------------------------------------------------------------------- 1 | from datetime import timedelta 2 | 3 | import pytest 4 | from pynamodb.attributes import UnicodeAttribute 5 | from pynamodb.models import Model 6 | from typing_extensions import assert_type 7 | 8 | from pynamodb_attributes import TimedeltaAttribute 9 | from pynamodb_attributes import TimedeltaMsAttribute 10 | from pynamodb_attributes import TimedeltaUsAttribute 11 | from tests.connection import _connection 12 | from tests.meta import dynamodb_table_meta 13 | 14 | 15 | class MyModel(Model): 16 | Meta = dynamodb_table_meta(__name__) 17 | 18 | key = UnicodeAttribute(hash_key=True) 19 | value = TimedeltaAttribute(null=True) 20 | value_ms = TimedeltaMsAttribute(null=True) 21 | value_us = TimedeltaUsAttribute(null=True) 22 | 23 | 24 | assert_type(MyModel().value, timedelta) 25 | assert_type(MyModel().value_ms, timedelta) 26 | assert_type(MyModel().value_us, timedelta) 27 | 28 | 29 | @pytest.fixture(scope="module", autouse=True) 30 | def create_table(): 31 | MyModel.create_table() 32 | 33 | 34 | def test_serialization_non_null(uuid_key): 35 | model = MyModel() 36 | model.key = uuid_key 37 | model.value = model.value_ms = model.value_us = timedelta( 38 | seconds=456, 39 | microseconds=123456, 40 | ) 41 | model.save() 42 | 43 | # verify underlying storage 44 | item = _connection(MyModel).get_item(uuid_key) 45 | assert item["Item"]["value"] == {"N": "456"} 46 | assert item["Item"]["value_ms"] == {"N": "456123"} 47 | assert item["Item"]["value_us"] == {"N": "456123456"} 48 | 49 | # verify deserialization 50 | model = MyModel.get(uuid_key) 51 | assert model.value == timedelta(seconds=456) 52 | assert model.value_ms == timedelta(seconds=456, microseconds=123000) 53 | assert model.value_us == timedelta(seconds=456, microseconds=123456) 54 | 55 | 56 | def test_serialization_null(uuid_key): 57 | model = MyModel() 58 | model.key = uuid_key 59 | model.value = None 60 | model.value_ms = None 61 | model.value_us = None 62 | model.save() 63 | 64 | # verify underlying storage 65 | item = _connection(MyModel).get_item(uuid_key) 66 | assert "value" not in item["Item"] 67 | assert "value_ms" not in item["Item"] 68 | assert "value_us" not in item["Item"] 69 | 70 | # verify deserialization 71 | model = MyModel.get(uuid_key) 72 | assert model.value is None 73 | assert model.value_ms is None 74 | assert model.value_us is None 75 | 76 | 77 | def test_set_invalid_type(): 78 | model = MyModel() 79 | with pytest.raises(TypeError, match="invalid type"): 80 | model.value = 42 81 | -------------------------------------------------------------------------------- /tests/timestamp_attribute_test.py: -------------------------------------------------------------------------------- 1 | from datetime import datetime 2 | from datetime import timezone 3 | 4 | import pytest 5 | from pynamodb.attributes import UnicodeAttribute 6 | from pynamodb.models import Model 7 | from typing_extensions import assert_type 8 | 9 | from pynamodb_attributes import TimestampAttribute 10 | from pynamodb_attributes import TimestampMsAttribute 11 | from pynamodb_attributes import TimestampUsAttribute 12 | from tests.meta import dynamodb_table_meta 13 | 14 | 15 | class MyModel(Model): 16 | Meta = dynamodb_table_meta(__name__) 17 | 18 | key = UnicodeAttribute(hash_key=True) 19 | value = TimestampAttribute() 20 | value_ms = TimestampMsAttribute() 21 | value_us = TimestampUsAttribute() 22 | 23 | null_value = TimestampAttribute(null=True) 24 | 25 | 26 | assert_type(MyModel().value, datetime) 27 | assert_type(MyModel().value_ms, datetime) 28 | assert_type(MyModel().value_us, datetime) 29 | 30 | 31 | @pytest.fixture(scope="module", autouse=True) 32 | def create_table(): 33 | MyModel.create_table() 34 | 35 | 36 | def test_serialization(uuid_key): 37 | now = datetime.now(tz=timezone.utc) 38 | model = MyModel() 39 | model.key = uuid_key 40 | model.value = now 41 | model.value_ms = now 42 | model.value_us = now 43 | model.save() 44 | 45 | # verify deserialization 46 | model = MyModel.get(uuid_key) 47 | assert model.value == now.replace(microsecond=0) 48 | assert model.value_ms == now.replace(microsecond=now.microsecond // 1_000 * 1_000) 49 | assert model.value_us == now 50 | assert model.null_value is None 51 | 52 | 53 | def test_set_invalid_type(): 54 | model = MyModel() 55 | with pytest.raises(TypeError, match="invalid type"): 56 | model.value = 42 57 | 58 | 59 | def test_set_naive_datetime(): 60 | model = MyModel() 61 | with pytest.raises(TypeError, match="aware datetime expected"): 62 | model.value = datetime.utcnow() 63 | 64 | 65 | def test_set_none_succeeds_on_nullable(): 66 | model = MyModel() 67 | model.null_value = None 68 | assert model.null_value is None 69 | 70 | 71 | def test_set_timestame_succeeds_on_nullable(): 72 | model = MyModel() 73 | now = datetime.now(tz=timezone.utc) 74 | model.null_value = now 75 | assert model.null_value == now 76 | 77 | 78 | def test_set_get(): 79 | model = MyModel() 80 | now = datetime.now(tz=timezone.utc) 81 | model.value = now 82 | assert model.value == now, "data lost before serialization" 83 | 84 | 85 | def test_set_get_nullable(): 86 | model = MyModel() 87 | now = datetime.now(tz=timezone.utc) 88 | model.null_value = now 89 | assert model.null_value == now, "data lost before serialization" 90 | -------------------------------------------------------------------------------- /pynamodb_attributes/unicode_delimited_tuple.py: -------------------------------------------------------------------------------- 1 | from typing import Any 2 | from typing import get_type_hints 3 | from typing import List 4 | from typing import Tuple 5 | from typing import Type 6 | from typing import TypeVar 7 | 8 | import pynamodb.constants 9 | from pynamodb.attributes import Attribute 10 | 11 | T = TypeVar("T", bound=Tuple[Any, ...]) 12 | _DEFAULT_FIELD_DELIMITER = "::" 13 | 14 | 15 | class UnicodeDelimitedTupleAttribute(Attribute[T]): 16 | """ 17 | Stores a tuple of strings as a string. The tuple's members will be joined with a delimiter. 18 | 19 | >>> from typing import NamedTuple 20 | >>> 21 | >>> from pynamodb.models import Model 22 | >>> 23 | >>> class LatLng(NamedTuple): 24 | >>> lat: int 25 | >>> lng: int 26 | >>> 27 | >>> class Employee(Model): 28 | >>> location = UnicodeDelimitedTupleAttribute(LatLng) 29 | """ 30 | 31 | attr_type = pynamodb.constants.STRING 32 | 33 | def __init__( 34 | self, 35 | tuple_type: Type[T], 36 | delimiter: str = _DEFAULT_FIELD_DELIMITER, 37 | **kwargs: Any, 38 | ) -> None: 39 | """ 40 | :param tuple_type: The type of the tuple -- may be a named or plain tuple 41 | :param delimiter: The delimiter to separate the tuple elements 42 | """ 43 | super().__init__(**kwargs) 44 | self.tuple_type: Type[T] = tuple_type 45 | self.delimiter = delimiter 46 | 47 | def deserialize(self, value: str) -> T: 48 | field_types = get_type_hints(self.tuple_type) 49 | 50 | if field_types: 51 | values = value.split(self.delimiter, maxsplit=len(field_types)) 52 | return self.tuple_type( 53 | **{ 54 | field_name: field_type(value) 55 | for (field_name, field_type), value in zip( 56 | field_types.items(), 57 | values, 58 | ) 59 | } 60 | ) 61 | else: 62 | return self.tuple_type(value.split(self.delimiter)) 63 | 64 | def serialize(self, value: T) -> str: 65 | if not isinstance(value, self.tuple_type): 66 | raise TypeError( 67 | f"value has invalid type '{type(value)}'; expected '{self.tuple_type}'", 68 | ) 69 | values: List[T] = list(value) 70 | while values and values[-1] is None: 71 | del values[-1] 72 | strings = [str(e) for e in values] 73 | if any(self.delimiter in s for s in strings): 74 | raise ValueError( 75 | f"Tuple elements may not contain delimiter '{self.delimiter}'", 76 | ) 77 | return self.delimiter.join(strings) 78 | -------------------------------------------------------------------------------- /pynamodb_attributes/unicode_protobuf_enum.py: -------------------------------------------------------------------------------- 1 | from typing import Any 2 | from typing import Optional 3 | from typing import Protocol 4 | from typing import Type 5 | from typing import TypeVar 6 | 7 | import pynamodb.constants 8 | from pynamodb.attributes import Attribute 9 | 10 | _T_co = TypeVar("_T_co", covariant=True) 11 | _TProtobufEnum = TypeVar("_TProtobufEnum", bound="_ProtobufEnum[Any]") 12 | 13 | 14 | # TODO: replace with built-in str.removeprefix once we're >=py3.9 15 | def _removeprefix(self: str, prefix: str) -> str: # pragma: no cover 16 | if self.startswith(prefix): 17 | return self[len(prefix) :] 18 | else: 19 | return self[:] 20 | 21 | 22 | # What we expect of mypy-protobuf's enum "classes" 23 | class _ProtobufEnum(Protocol[_T_co]): 24 | @classmethod 25 | def Name(cls, number: int) -> str: 26 | ... 27 | 28 | @classmethod 29 | def Value(cls: Type[_T_co], name: str) -> _T_co: 30 | ... 31 | 32 | 33 | _fail: Any = object() 34 | 35 | 36 | class UnicodeProtobufEnumAttribute(Attribute[_TProtobufEnum]): 37 | """ 38 | Stores Protobuf enumeration values as DynamoDB strings. 39 | 40 | >>> from diner_pb2 import ShakeFlavor 41 | >>> 42 | >>> class Shake(Model): 43 | >>> flavor = UnicodeProtobufEnumAttribute(ShakeFlavor, prefix='SHAKE_FLAVOR_ ') 44 | """ 45 | 46 | attr_type = pynamodb.constants.STRING 47 | 48 | def __init__( 49 | self, 50 | enum_type: Type[_TProtobufEnum], 51 | *, 52 | unknown_value: Optional[_TProtobufEnum] = _fail, 53 | prefix: str = "", 54 | lower: bool = True, 55 | **kwargs: Any, 56 | ) -> None: 57 | """ 58 | :param enum_type: the type of the enumeration 59 | :param unknown_value: the value to return if the persisted value is unknown 60 | :param prefix: prefix to strip from the persisted value 61 | :param lower: whether to persist as lowercase 62 | """ 63 | super().__init__(**kwargs) 64 | self.enum_type = enum_type 65 | self.unknown_value = unknown_value 66 | self.prefix = prefix 67 | self.lower = lower 68 | self._kwargs = kwargs 69 | 70 | # Attributes need to be copiable :| 71 | def __deepcopy__(self, memo: Any) -> Any: 72 | return self.__class__( 73 | self.enum_type, 74 | unknown_value=self.unknown_value, 75 | prefix=self.prefix, 76 | lower=self.lower, 77 | **self._kwargs, 78 | ) 79 | 80 | def deserialize(self, value: str) -> Optional[_TProtobufEnum]: 81 | try: 82 | return self.enum_type.Value(self.prefix + value.upper()) 83 | except ValueError: 84 | if self.unknown_value is _fail: 85 | raise 86 | return self.unknown_value 87 | 88 | def serialize(self, value: _TProtobufEnum) -> str: 89 | if not isinstance(value, int): 90 | raise TypeError( 91 | f"value has invalid type '{type(value)}'; expected an integer", 92 | ) 93 | name = self.enum_type.Name(value) 94 | name = _removeprefix(name, self.prefix) 95 | if self.lower: 96 | name = name.lower() 97 | return name 98 | -------------------------------------------------------------------------------- /tests/integer_enum_attribute_test.py: -------------------------------------------------------------------------------- 1 | from enum import Enum 2 | from unittest.mock import ANY 3 | 4 | import pytest 5 | from pynamodb.attributes import UnicodeAttribute 6 | from pynamodb.models import Model 7 | 8 | from pynamodb_attributes.integer_enum import IntegerEnumAttribute 9 | from tests.connection import _connection 10 | from tests.meta import dynamodb_table_meta 11 | 12 | 13 | class MyEnum(Enum): 14 | foo_key = 1 15 | bar_key = 2 16 | unknown_key = 0 17 | 18 | 19 | class MyEnumWithMissing(Enum): 20 | foo_key = 1 21 | bar_key = 2 22 | missing_key = 0 23 | 24 | @classmethod 25 | def _missing_(cls, key): 26 | return cls.missing_key 27 | 28 | 29 | class MyModel(Model): 30 | Meta = dynamodb_table_meta(__name__) 31 | 32 | key = UnicodeAttribute(hash_key=True) 33 | value = IntegerEnumAttribute(MyEnum, null=True) 34 | value_with_unknown = IntegerEnumAttribute( 35 | MyEnum, 36 | unknown_value=MyEnum.unknown_key, 37 | null=True, 38 | ) 39 | value_with_missing = IntegerEnumAttribute(MyEnumWithMissing, null=True) 40 | 41 | 42 | @pytest.fixture(scope="module", autouse=True) 43 | def create_table(): 44 | MyModel.create_table() 45 | 46 | 47 | def test_invalid_enum(): 48 | class StringEnum(Enum): 49 | foo_key = "foo_value" 50 | bar_key = 2 51 | 52 | with pytest.raises(TypeError, match="values must be all ints"): 53 | IntegerEnumAttribute(StringEnum) 54 | 55 | 56 | def test_serialization_invalid_type(uuid_key): 57 | model = MyModel() 58 | model.key = uuid_key 59 | model.value = 999 # type: ignore 60 | 61 | with pytest.raises(TypeError, match="value has invalid type"): 62 | model.save() 63 | 64 | 65 | def test_serialization_unknown_value_fail(uuid_key): 66 | _connection(MyModel).put_item( 67 | uuid_key, 68 | attributes={ 69 | "value": {"N": "9001"}, 70 | }, 71 | ) 72 | with pytest.raises(ValueError, match="9001 is not a valid MyEnum"): 73 | MyModel.get(uuid_key) 74 | 75 | 76 | def test_serialization_unknown_value_success(uuid_key): 77 | _connection(MyModel).put_item( 78 | uuid_key, 79 | attributes={ 80 | "value_with_unknown": {"N": "9001"}, 81 | }, 82 | ) 83 | model = MyModel.get(uuid_key) 84 | assert model.value_with_unknown == MyEnum.unknown_key 85 | 86 | 87 | def test_serialization_missing_value_success(uuid_key): 88 | _connection(MyModel).put_item( 89 | uuid_key, 90 | attributes={ 91 | "value_with_missing": {"N": "9001"}, 92 | }, 93 | ) 94 | model = MyModel.get(uuid_key) 95 | assert model.value_with_missing == MyEnumWithMissing.missing_key 96 | 97 | 98 | @pytest.mark.parametrize( 99 | ["value", "expected_attributes"], 100 | [ 101 | (None, {}), 102 | (MyEnum.foo_key, {"value": {"N": "1"}}), 103 | (MyEnum.bar_key, {"value": {"N": "2"}}), 104 | ], 105 | ) 106 | def test_serialization(value, expected_attributes, uuid_key): 107 | model = MyModel() 108 | model.key = uuid_key 109 | model.value = value 110 | model.save() 111 | 112 | # verify underlying storage 113 | item = _connection(MyModel).get_item(uuid_key) 114 | assert item["Item"] == {"key": ANY, **expected_attributes} 115 | 116 | # verify deserialization 117 | model = MyModel.get(uuid_key) 118 | assert model.value == value 119 | -------------------------------------------------------------------------------- /tests/unicode_enum_attribute_test.py: -------------------------------------------------------------------------------- 1 | from enum import Enum 2 | from unittest.mock import ANY 3 | 4 | import pytest 5 | from pynamodb.attributes import UnicodeAttribute 6 | from pynamodb.models import Model 7 | from typing_extensions import assert_type 8 | 9 | from pynamodb_attributes import UnicodeEnumAttribute 10 | from tests.connection import _connection 11 | from tests.meta import dynamodb_table_meta 12 | 13 | 14 | class MyEnum(Enum): 15 | foo_key = "foo_value" 16 | bar_key = "bar_value" 17 | unknown_key = "unknown_value" 18 | 19 | 20 | class MyEnumWithMissing(Enum): 21 | foo_key = "foo_value" 22 | bar_key = "bar_value" 23 | missing_key = "missing_value" 24 | 25 | @classmethod 26 | def _missing_(cls, key): 27 | return cls.missing_key 28 | 29 | 30 | class MyModel(Model): 31 | Meta = dynamodb_table_meta(__name__) 32 | 33 | key = UnicodeAttribute(hash_key=True) 34 | value = UnicodeEnumAttribute(MyEnum, null=True) 35 | value_with_unknown = UnicodeEnumAttribute( 36 | MyEnum, 37 | unknown_value=MyEnum.unknown_key, 38 | null=True, 39 | ) 40 | value_with_missing = UnicodeEnumAttribute(MyEnumWithMissing, null=True) 41 | 42 | 43 | assert_type(MyModel.value, UnicodeEnumAttribute[MyEnum]) 44 | assert_type(MyModel().value, MyEnum) 45 | 46 | 47 | @pytest.fixture(scope="module", autouse=True) 48 | def create_table(): 49 | MyModel.create_table() 50 | 51 | 52 | def test_invalid_enum(): 53 | class IntEnum(Enum): 54 | foo_key = "foo_value" 55 | bar_key = 2 56 | 57 | with pytest.raises(TypeError, match="values must be all strings"): 58 | UnicodeEnumAttribute(IntEnum) 59 | 60 | 61 | def test_serialization_invalid_type(uuid_key): 62 | model = MyModel() 63 | model.key = uuid_key 64 | model.value = "invalid" # type: ignore 65 | 66 | with pytest.raises(TypeError, match="value has invalid type"): 67 | model.save() 68 | 69 | 70 | def test_serialization_unknown_value_fail(uuid_key): 71 | _connection(MyModel).put_item( 72 | uuid_key, 73 | attributes={ 74 | "value": {"S": "nonexistent_value"}, 75 | }, 76 | ) 77 | with pytest.raises(ValueError, match="'nonexistent_value' is not a valid MyEnum"): 78 | MyModel.get(uuid_key) 79 | 80 | 81 | def test_serialization_unknown_value_success(uuid_key): 82 | _connection(MyModel).put_item( 83 | uuid_key, 84 | attributes={ 85 | "value_with_unknown": {"S": "nonexistent_value"}, 86 | }, 87 | ) 88 | model = MyModel.get(uuid_key) 89 | assert model.value_with_unknown == MyEnum.unknown_key 90 | 91 | 92 | def test_serialization_missing_value_success(uuid_key): 93 | _connection(MyModel).put_item( 94 | uuid_key, 95 | attributes={ 96 | "value_with_missing": {"S": "nonexistent_value"}, 97 | }, 98 | ) 99 | model = MyModel.get(uuid_key) 100 | assert model.value_with_missing == MyEnumWithMissing.missing_key 101 | 102 | 103 | @pytest.mark.parametrize( 104 | ["value", "expected_attributes"], 105 | [ 106 | (None, {}), 107 | (MyEnum.foo_key, {"value": {"S": "foo_value"}}), 108 | (MyEnum.bar_key, {"value": {"S": "bar_value"}}), 109 | ], 110 | ) 111 | def test_serialization(value, expected_attributes, uuid_key): 112 | model = MyModel() 113 | model.key = uuid_key 114 | model.value = value 115 | model.save() 116 | 117 | # verify underlying storage 118 | item = _connection(MyModel).get_item(uuid_key) 119 | assert item["Item"] == {"key": ANY, **expected_attributes} 120 | 121 | # verify deserialization 122 | model = MyModel.get(uuid_key) 123 | assert model.value == value 124 | -------------------------------------------------------------------------------- /tests/unicode_delimited_tuple_attribute_test.py: -------------------------------------------------------------------------------- 1 | from typing import Any 2 | from typing import NamedTuple 3 | from typing import Tuple 4 | from unittest.mock import ANY 5 | 6 | import pytest 7 | from pynamodb.attributes import UnicodeAttribute 8 | from pynamodb.models import Model 9 | from typing_extensions import assert_type 10 | 11 | from pynamodb_attributes import UnicodeDelimitedTupleAttribute 12 | from tests.connection import _connection 13 | from tests.meta import dynamodb_table_meta 14 | 15 | 16 | class MyTuple(NamedTuple): 17 | country: str 18 | city: str 19 | # should be Optional[int] but deserialization does not support it 20 | zip_code: int = None # type: ignore 21 | 22 | 23 | @pytest.fixture(scope="module", autouse=True) 24 | def create_table(): 25 | MyModel.create_table() 26 | 27 | 28 | class MyModel(Model): 29 | Meta = dynamodb_table_meta(__name__) 30 | 31 | key = UnicodeAttribute(hash_key=True) 32 | default_delimiter = UnicodeDelimitedTupleAttribute(MyTuple, null=True) 33 | custom_delimiter = UnicodeDelimitedTupleAttribute(MyTuple, delimiter=".", null=True) 34 | untyped = UnicodeDelimitedTupleAttribute(tuple, null=True) 35 | 36 | 37 | assert_type(MyModel.default_delimiter, UnicodeDelimitedTupleAttribute[MyTuple]) 38 | assert_type(MyModel.untyped, UnicodeDelimitedTupleAttribute[Tuple[Any, ...]]) 39 | 40 | 41 | def test_serialization_containing_delimiter(uuid_key): 42 | model = MyModel() 43 | model.key = uuid_key 44 | model.default_delimiter = MyTuple(country="U::S", city="San Francisco") 45 | 46 | assert_type(model.default_delimiter, MyTuple) 47 | assert_type(model.default_delimiter.country, str) 48 | 49 | with pytest.raises(ValueError): 50 | model.save() 51 | 52 | 53 | def test_serialization_containing_custom_delimiter(uuid_key): 54 | model = MyModel() 55 | model.key = uuid_key 56 | model.custom_delimiter = MyTuple(country="U.S.", city="San Francisco") 57 | 58 | with pytest.raises(ValueError): 59 | model.save() 60 | 61 | model.custom_delimiter = MyTuple(country="U::S", city="San Francisco") 62 | model.save() 63 | 64 | 65 | def test_serialization_invalid_type(uuid_key): 66 | model = MyModel() 67 | model.key = uuid_key 68 | model.default_delimiter = (1, 2, 3) # type: ignore 69 | 70 | with pytest.raises(TypeError): 71 | model.save() 72 | 73 | 74 | def test_serialization_typing(uuid_key): 75 | model = MyModel() 76 | model.key = uuid_key 77 | model.default_delimiter = MyTuple("US", "San Francisco", 94107) 78 | model.save() 79 | 80 | model = MyModel.get(uuid_key) 81 | assert model.default_delimiter.country == "US" 82 | assert model.default_delimiter.city == "San Francisco" 83 | assert model.default_delimiter.zip_code == 94107 # note the type 84 | 85 | 86 | @pytest.mark.parametrize( 87 | ["value", "expected_attributes"], 88 | [ 89 | (None, {}), 90 | ( 91 | MyTuple(country="US", city="San Francisco", zip_code=94107), 92 | { 93 | "default_delimiter": {"S": "US::San Francisco::94107"}, 94 | "custom_delimiter": {"S": "US.San Francisco.94107"}, 95 | }, 96 | ), 97 | ( 98 | MyTuple(country="US", city="San Francisco"), 99 | { 100 | "default_delimiter": {"S": "US::San Francisco"}, 101 | "custom_delimiter": {"S": "US.San Francisco"}, 102 | }, 103 | ), 104 | ], 105 | ) 106 | def test_serialization(expected_attributes, value, uuid_key): 107 | model = MyModel() 108 | model.key = uuid_key 109 | model.default_delimiter = value 110 | model.custom_delimiter = value 111 | model.save() 112 | 113 | # verify underlying storage 114 | item = _connection(MyModel).get_item(uuid_key) 115 | assert item["Item"] == {"key": ANY, **expected_attributes} 116 | 117 | # verify deserialization 118 | model = MyModel.get(uuid_key) 119 | assert model.default_delimiter == value 120 | assert model.custom_delimiter == value 121 | 122 | 123 | @pytest.mark.parametrize( 124 | ["value", "expected_attributes"], 125 | [ 126 | (None, {}), 127 | ( 128 | ("US", "San Francisco", "94107"), 129 | { 130 | "untyped": {"S": "US::San Francisco::94107"}, 131 | }, 132 | ), 133 | ( 134 | ("US", "San Francisco"), 135 | { 136 | "untyped": {"S": "US::San Francisco"}, 137 | }, 138 | ), 139 | ], 140 | ) 141 | def test_serialization_untyped(expected_attributes, value, uuid_key): 142 | model = MyModel() 143 | model.key = uuid_key 144 | model.untyped = value 145 | model.save() 146 | 147 | # verify underlying storage 148 | item = _connection(MyModel).get_item(uuid_key) 149 | assert item["Item"] == {"key": ANY, **expected_attributes} 150 | 151 | # verify deserialization 152 | model = MyModel.get(uuid_key) 153 | assert model.untyped == value 154 | 155 | assert_type(MyModel.untyped, UnicodeDelimitedTupleAttribute[Tuple[Any, ...]]) 156 | -------------------------------------------------------------------------------- /tests/unicode_protobuf_enum_attribute_test.py: -------------------------------------------------------------------------------- 1 | from typing import Any 2 | from typing import cast 3 | from typing import Dict 4 | from typing import Optional 5 | from unittest.mock import ANY 6 | 7 | import pytest 8 | from pynamodb.attributes import MapAttribute 9 | from pynamodb.attributes import UnicodeAttribute 10 | from pynamodb.models import Model 11 | from typing_extensions import assert_type 12 | 13 | from pynamodb_attributes import UnicodeProtobufEnumAttribute 14 | from tests.connection import _connection 15 | from tests.meta import dynamodb_table_meta 16 | 17 | 18 | # Something that mypy-protobuf would generate... 19 | class diner_pb2: 20 | class ShakeFlavor(int): 21 | @classmethod 22 | def Name(cls, number: int) -> str: 23 | mapping = { 24 | v: k 25 | for k, v in vars(diner_pb2).items() 26 | if k.startswith("SHAKE_FLAVOR_") 27 | } 28 | if name := mapping.get(number): 29 | return name 30 | else: 31 | if not isinstance(number, int): 32 | raise TypeError( 33 | f"Enum value for {cls.__name__} must be an int, but got {type(number)} {number!r}.", 34 | ) 35 | else: 36 | raise ValueError( 37 | f"Enum {cls.__name__} has no name defined for value {number!r}", 38 | ) 39 | 40 | @classmethod 41 | def Value(cls, name: str) -> "diner_pb2.ShakeFlavor": 42 | mapping = { 43 | k: v 44 | for k, v in vars(diner_pb2).items() 45 | if k.startswith("SHAKE_FLAVOR_") 46 | } 47 | if (value := mapping.get(name)) is not None: 48 | return cast("diner_pb2.ShakeFlavor", value) 49 | else: 50 | raise ValueError( 51 | f"Enum {cls.__name__} has no value defined for name {name!r}", 52 | ) 53 | 54 | SHAKE_FLAVOR_UNKNOWN = cast(ShakeFlavor, 0) 55 | SHAKE_FLAVOR_VANILLA = cast(ShakeFlavor, 1) 56 | SHAKE_FLAVOR_CHOCOLATE = cast(ShakeFlavor, 2) 57 | 58 | 59 | class MyMapAttr(MapAttribute[Any, Any]): 60 | value = UnicodeProtobufEnumAttribute( 61 | diner_pb2.ShakeFlavor, 62 | prefix="SHAKE_FLAVOR_", 63 | null=True, 64 | ) 65 | 66 | 67 | class MyModel(Model): 68 | Meta = dynamodb_table_meta(__name__) 69 | 70 | key = UnicodeAttribute(hash_key=True) 71 | value = UnicodeProtobufEnumAttribute( 72 | diner_pb2.ShakeFlavor, 73 | prefix="SHAKE_FLAVOR_", 74 | null=True, 75 | ) 76 | value_upper = UnicodeProtobufEnumAttribute( 77 | diner_pb2.ShakeFlavor, 78 | prefix="SHAKE_FLAVOR_", 79 | null=True, 80 | lower=False, 81 | ) 82 | value_with_unknown = UnicodeProtobufEnumAttribute( 83 | diner_pb2.ShakeFlavor, 84 | unknown_value=diner_pb2.SHAKE_FLAVOR_UNKNOWN, 85 | prefix="SHAKE_FLAVOR_", 86 | null=True, 87 | ) 88 | value_with_prefix = UnicodeProtobufEnumAttribute( 89 | diner_pb2.ShakeFlavor, 90 | unknown_value=diner_pb2.SHAKE_FLAVOR_UNKNOWN, 91 | null=True, 92 | lower=False, 93 | ) 94 | map_attr = MyMapAttr(null=True) 95 | 96 | 97 | assert_type(MyModel().value, diner_pb2.ShakeFlavor) 98 | 99 | 100 | @pytest.fixture(scope="module", autouse=True) 101 | def create_table(): 102 | MyModel.create_table() 103 | 104 | 105 | def test_serialization_invalid_type(uuid_key): 106 | model = MyModel() 107 | model.key = uuid_key 108 | model.value = "invalid" # type: ignore 109 | 110 | with pytest.raises(TypeError, match="value has invalid type"): 111 | model.save() 112 | 113 | 114 | def test_serialization_unknown_value_fail(uuid_key): 115 | _connection(MyModel).put_item( 116 | uuid_key, 117 | attributes={ 118 | "value": {"S": "nonexistent_value"}, 119 | }, 120 | ) 121 | with pytest.raises( 122 | ValueError, 123 | match="no value defined for name 'SHAKE_FLAVOR_NONEXISTENT_VALUE'", 124 | ): 125 | MyModel.get(uuid_key) 126 | 127 | 128 | def test_serialization_unknown_value_success(uuid_key): 129 | _connection(MyModel).put_item( 130 | uuid_key, 131 | attributes={ 132 | "value_with_unknown": {"S": "nonexistent_value"}, 133 | }, 134 | ) 135 | model = MyModel.get(uuid_key) 136 | assert model.value_with_unknown == diner_pb2.SHAKE_FLAVOR_UNKNOWN 137 | 138 | 139 | @pytest.mark.parametrize( 140 | ["value", "expected_attributes"], 141 | [ 142 | (None, {}), 143 | ( 144 | diner_pb2.SHAKE_FLAVOR_VANILLA, 145 | { 146 | "value": {"S": "vanilla"}, 147 | "value_upper": {"S": "VANILLA"}, 148 | "value_with_unknown": {"S": "vanilla"}, 149 | "value_with_prefix": {"S": "SHAKE_FLAVOR_VANILLA"}, 150 | }, 151 | ), 152 | ( 153 | diner_pb2.SHAKE_FLAVOR_CHOCOLATE, 154 | { 155 | "value": {"S": "chocolate"}, 156 | "value_upper": {"S": "CHOCOLATE"}, 157 | "value_with_unknown": {"S": "chocolate"}, 158 | "value_with_prefix": {"S": "SHAKE_FLAVOR_CHOCOLATE"}, 159 | }, 160 | ), 161 | ], 162 | ) 163 | def test_serialization( 164 | value: Optional[diner_pb2.ShakeFlavor], 165 | expected_attributes: Dict[str, Any], 166 | uuid_key: str, 167 | ) -> None: 168 | model = MyModel() 169 | model.key = uuid_key 170 | model.value = value 171 | model.value_upper = value 172 | model.value_with_unknown = value 173 | model.value_with_prefix = value 174 | model.save() 175 | 176 | # verify underlying storage 177 | item = _connection(MyModel).get_item(uuid_key) 178 | assert item["Item"] == {"key": ANY, **expected_attributes} 179 | 180 | # verify deserialization 181 | model = MyModel.get(uuid_key) 182 | assert model.value == value 183 | assert model.value_upper == value 184 | assert model.value_with_unknown == value 185 | assert model.value_with_prefix == value 186 | 187 | 188 | def test_map_attribute( # exercises the __deepcopy__ method 189 | uuid_key: str, 190 | ) -> None: 191 | model = MyModel() 192 | model.key = uuid_key 193 | model.map_attr = MyMapAttr(value=diner_pb2.SHAKE_FLAVOR_VANILLA) 194 | model.save() 195 | 196 | # verify deserialization 197 | model = MyModel.get(uuid_key) 198 | assert model.map_attr.value == diner_pb2.SHAKE_FLAVOR_VANILLA 199 | -------------------------------------------------------------------------------- /tests/unicode_datetime_attribute_test.py: -------------------------------------------------------------------------------- 1 | from datetime import datetime 2 | from unittest.mock import ANY 3 | 4 | import pytest 5 | from pynamodb.attributes import UnicodeAttribute 6 | from pynamodb.models import Model 7 | from typing_extensions import assert_type 8 | 9 | from pynamodb_attributes import UnicodeDatetimeAttribute 10 | from tests.connection import _connection 11 | from tests.meta import dynamodb_table_meta 12 | 13 | 14 | CUSTOM_FORMAT = "%m/%d/%Y, %H:%M:%S" 15 | CUSTOM_FORMAT_DATE = "11/22/2020, 11:22:33" 16 | TEST_ISO_DATE_NO_OFFSET = "2020-11-22T11:22:33.444444" 17 | TEST_ISO_DATE_UTC = "2020-11-22T11:22:33.444444+00:00" 18 | TEST_ISO_DATE_PST = "2020-11-22T03:22:33.444444-08:00" 19 | 20 | 21 | class MyModel(Model): 22 | Meta = dynamodb_table_meta(__name__) 23 | 24 | key = UnicodeAttribute(hash_key=True) 25 | default = UnicodeDatetimeAttribute(null=True) 26 | no_force_tz = UnicodeDatetimeAttribute(force_tz=False, null=True) 27 | force_utc = UnicodeDatetimeAttribute(force_utc=True, null=True) 28 | force_utc_no_force_tz = UnicodeDatetimeAttribute( 29 | force_utc=True, 30 | force_tz=False, 31 | null=True, 32 | ) 33 | custom_format = UnicodeDatetimeAttribute(fmt=CUSTOM_FORMAT, null=True) 34 | 35 | 36 | assert_type(MyModel.default, UnicodeDatetimeAttribute) 37 | assert_type(MyModel().default, datetime) 38 | 39 | 40 | @pytest.fixture(scope="module", autouse=True) 41 | def create_table(): 42 | MyModel.create_table() 43 | 44 | 45 | @pytest.mark.parametrize( 46 | ["value", "expected_str", "expected_value"], 47 | [ 48 | ( 49 | datetime.fromisoformat(TEST_ISO_DATE_NO_OFFSET), 50 | TEST_ISO_DATE_UTC, 51 | datetime.fromisoformat(TEST_ISO_DATE_UTC), 52 | ), 53 | ( 54 | datetime.fromisoformat(TEST_ISO_DATE_UTC), 55 | TEST_ISO_DATE_UTC, 56 | datetime.fromisoformat(TEST_ISO_DATE_UTC), 57 | ), 58 | ( 59 | datetime.fromisoformat(TEST_ISO_DATE_PST), 60 | TEST_ISO_DATE_PST, 61 | datetime.fromisoformat(TEST_ISO_DATE_PST), 62 | ), 63 | ], 64 | ) 65 | def test_default_serialization(value, expected_str, expected_value, uuid_key): 66 | model = MyModel() 67 | model.key = uuid_key 68 | model.default = value 69 | 70 | model.save() 71 | 72 | actual = MyModel.get(hash_key=uuid_key) 73 | assert actual.default == expected_value 74 | 75 | item = _connection(MyModel).get_item(uuid_key) 76 | assert item["Item"] == {"key": ANY, "default": {"S": expected_str}} 77 | 78 | 79 | @pytest.mark.parametrize( 80 | ["value", "expected_str", "expected_value"], 81 | [ 82 | ( 83 | datetime.fromisoformat(TEST_ISO_DATE_NO_OFFSET), 84 | TEST_ISO_DATE_NO_OFFSET, 85 | datetime.fromisoformat(TEST_ISO_DATE_NO_OFFSET), 86 | ), 87 | ( 88 | datetime.fromisoformat(TEST_ISO_DATE_UTC), 89 | TEST_ISO_DATE_UTC, 90 | datetime.fromisoformat(TEST_ISO_DATE_UTC), 91 | ), 92 | ( 93 | datetime.fromisoformat(TEST_ISO_DATE_PST), 94 | TEST_ISO_DATE_PST, 95 | datetime.fromisoformat(TEST_ISO_DATE_PST), 96 | ), 97 | ], 98 | ) 99 | def test_no_force_tz_serialization(value, expected_str, expected_value, uuid_key): 100 | model = MyModel() 101 | model.key = uuid_key 102 | model.no_force_tz = value 103 | 104 | model.save() 105 | 106 | actual = MyModel.get(hash_key=uuid_key) 107 | item = _connection(MyModel).get_item(uuid_key) 108 | 109 | assert item["Item"] == {"key": ANY, "no_force_tz": {"S": expected_str}} 110 | 111 | assert actual.no_force_tz == expected_value 112 | 113 | 114 | @pytest.mark.parametrize( 115 | ["value", "expected_str", "expected_value"], 116 | [ 117 | ( 118 | datetime.fromisoformat(TEST_ISO_DATE_NO_OFFSET), 119 | TEST_ISO_DATE_UTC, 120 | datetime.fromisoformat(TEST_ISO_DATE_UTC), 121 | ), 122 | ( 123 | datetime.fromisoformat(TEST_ISO_DATE_UTC), 124 | TEST_ISO_DATE_UTC, 125 | datetime.fromisoformat(TEST_ISO_DATE_UTC), 126 | ), 127 | ( 128 | datetime.fromisoformat(TEST_ISO_DATE_PST), 129 | TEST_ISO_DATE_UTC, 130 | datetime.fromisoformat(TEST_ISO_DATE_UTC), 131 | ), 132 | ], 133 | ) 134 | def test_force_utc_serialization(value, expected_str, expected_value, uuid_key): 135 | model = MyModel() 136 | model.key = uuid_key 137 | model.force_utc = value 138 | 139 | model.save() 140 | 141 | actual = MyModel.get(hash_key=uuid_key) 142 | item = _connection(MyModel).get_item(uuid_key) 143 | 144 | assert item["Item"] == {"key": ANY, "force_utc": {"S": expected_str}} 145 | 146 | assert actual.force_utc == expected_value 147 | 148 | 149 | @pytest.mark.parametrize( 150 | ["value", "expected_str", "expected_value"], 151 | [ 152 | ( 153 | datetime.fromisoformat(TEST_ISO_DATE_UTC), 154 | TEST_ISO_DATE_UTC, 155 | datetime.fromisoformat(TEST_ISO_DATE_UTC), 156 | ), 157 | ( 158 | datetime.fromisoformat(TEST_ISO_DATE_PST), 159 | TEST_ISO_DATE_UTC, 160 | datetime.fromisoformat(TEST_ISO_DATE_UTC), 161 | ), 162 | ], 163 | ) 164 | def test_force_utc_no_force_tz_serialization( 165 | value, 166 | expected_str, 167 | expected_value, 168 | uuid_key, 169 | ): 170 | model = MyModel() 171 | model.key = uuid_key 172 | model.force_utc_no_force_tz = value 173 | 174 | model.save() 175 | 176 | actual = MyModel.get(hash_key=uuid_key) 177 | item = _connection(MyModel).get_item(uuid_key) 178 | 179 | assert item["Item"] == {"key": ANY, "force_utc_no_force_tz": {"S": expected_str}} 180 | 181 | assert actual.force_utc_no_force_tz == expected_value 182 | 183 | 184 | @pytest.mark.parametrize( 185 | ["value", "expected_str", "expected_value"], 186 | [ 187 | ( 188 | datetime.fromisoformat(TEST_ISO_DATE_UTC), 189 | CUSTOM_FORMAT_DATE, 190 | datetime(2020, 11, 22, 11, 22, 33), 191 | ), 192 | ], 193 | ) 194 | def test_custom_format_force_tz_serialization( 195 | value, 196 | expected_str, 197 | expected_value, 198 | uuid_key, 199 | ): 200 | model = MyModel() 201 | model.key = uuid_key 202 | model.custom_format = value 203 | 204 | model.save() 205 | 206 | actual = MyModel.get(hash_key=uuid_key) 207 | item = _connection(MyModel).get_item(uuid_key) 208 | 209 | assert item["Item"] == {"key": ANY, "custom_format": {"S": expected_str}} 210 | 211 | assert actual.custom_format == expected_value 212 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "{}" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright 2017 Lyft, Inc. 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | --------------------------------------------------------------------------------