Skip to content

Commit

Permalink
Merge 453e009 into 3cdf4ce
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathantan committed Aug 3, 2020
2 parents 3cdf4ce + 453e009 commit 56a0417
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
9 changes: 9 additions & 0 deletions pynamodb/exceptions.py
Expand Up @@ -118,6 +118,15 @@ class InvalidStateError(PynamoDBException):
msg = "Operation in invalid state"


class AttributeDeserializationError(TypeError):
"""
Raised when attribute type is invalid
"""
def __init__(self, attr_name: Optional[str]):
msg = "Deserialization error on `{}`".format(attr_name)
super(AttributeDeserializationError, self).__init__(msg)


class VerboseClientError(botocore.exceptions.ClientError):
def __init__(self, error_response: Any, operation_name: str, verbose_properties: Optional[Any] = None):
""" Modify the message template to include the desired verbose properties """
Expand Down
7 changes: 5 additions & 2 deletions pynamodb/models.py
Expand Up @@ -11,7 +11,7 @@
Tuple, Union, cast

from pynamodb.expressions.update import Action
from pynamodb.exceptions import DoesNotExist, TableDoesNotExist, TableError, InvalidStateError, PutError
from pynamodb.exceptions import DoesNotExist, TableDoesNotExist, TableError, InvalidStateError, PutError, AttributeDeserializationError
from pynamodb.attributes import (
Attribute, AttributeContainer, AttributeContainerMeta, MapAttribute, TTLAttribute, VersionAttribute
)
Expand Down Expand Up @@ -551,7 +551,10 @@ def from_raw_data(cls: Type[_T], data: Dict[str, Any]) -> _T:
attr_name = cls._dynamo_to_python_attr(name)
attr = cls.get_attributes().get(attr_name, None) # type: ignore
if attr:
attributes[attr_name] = attr.deserialize(attr.get_value(value)) # type: ignore
try:
attributes[attr_name] = attr.deserialize(attr.get_value(value)) # type: ignore
except TypeError as e:
raise AttributeDeserializationError(attr_name=attr_name) from e
return cls(_user_instantiated=False, **attributes)

@classmethod
Expand Down
5 changes: 4 additions & 1 deletion tests/test_model.py
Expand Up @@ -14,7 +14,7 @@

from .deep_eq import deep_eq
from pynamodb.util import snake_to_camel_case
from pynamodb.exceptions import DoesNotExist, TableError, PutError
from pynamodb.exceptions import DoesNotExist, TableError, PutError, AttributeDeserializationError
from pynamodb.types import RANGE
from pynamodb.constants import (
ITEM, STRING_SHORT, ALL, KEYS_ONLY, INCLUDE, REQUEST_ITEMS, UNPROCESSED_KEYS, CAMEL_COUNT,
Expand Down Expand Up @@ -3302,6 +3302,9 @@ def test_deserialized_with_ttl(self):
m = TTLModel.from_raw_data({'user_name': {'S': 'mock'}, 'my_ttl': {'N': '1546300800'}})
assert m.my_ttl == datetime(2019, 1, 1, tzinfo=tzutc())

def test_deserialized_with_invalid_type(self):
self.assertRaises(AttributeDeserializationError, TTLModel.from_raw_data, {'my_ttl': {'S': '1546300800'}})

def test_multiple_version_attributes(self):
with self.assertRaises(ValueError):
class BadVersionedModel(Model):
Expand Down

0 comments on commit 56a0417

Please sign in to comment.