2
2
PynamoDB attributes
3
3
"""
4
4
import six
5
+ from six import add_metaclass
5
6
import json
6
7
from base64 import b64encode , b64decode
7
8
from datetime import datetime
@@ -118,11 +119,13 @@ def _serialize(self, value):
118
119
return {ATTR_TYPE_MAP [self .attribute .attr_type ]: self .attribute .serialize (value )}
119
120
120
121
121
- class AttributeContainer ( object ):
122
+ class AttributeContainerMeta ( type ):
122
123
123
- _dynamo_to_python_attrs = None
124
+ def __init__ (cls , name , bases , attrs ):
125
+ super (AttributeContainerMeta , cls ).__init__ (name , bases , attrs )
126
+ AttributeContainerMeta ._initialize_attributes (cls )
124
127
125
- @classmethod
128
+ @staticmethod
126
129
def _initialize_attributes (cls ):
127
130
"""
128
131
Initialize attributes on the class.
@@ -157,16 +160,17 @@ def _initialize_attributes(cls):
157
160
else :
158
161
instance .attr_name = item_name
159
162
163
+
164
+ @add_metaclass (AttributeContainerMeta )
165
+ class AttributeContainer (object ):
166
+
160
167
@classmethod
161
168
def _get_attributes (cls ):
162
169
"""
163
170
Returns the attributes of this class as a mapping from `python_attr_name` => `attribute`.
164
171
165
172
:rtype: dict[str, Attribute]
166
173
"""
167
- if '_attributes' not in cls .__dict__ :
168
- # Each subclass of AttributeContainer needs its own attributes map.
169
- cls ._initialize_attributes ()
170
174
return cls ._attributes
171
175
172
176
@classmethod
@@ -176,8 +180,6 @@ def _dynamo_to_python_attr(cls, dynamo_key):
176
180
177
181
This covers cases where an attribute name has been overridden via "attr_name".
178
182
"""
179
- if cls ._attributes is None :
180
- cls ._initialize_attributes ()
181
183
return cls ._dynamo_to_python_attrs .get (dynamo_key , dynamo_key )
182
184
183
185
def _set_defaults (self ):
@@ -473,6 +475,13 @@ def deserialize(self, value):
473
475
return None
474
476
475
477
478
+ class MapAttributeMeta (AttributeContainerMeta ):
479
+ """
480
+ This is only here for backwards compatibility: i.e. so type(MapAttribute) == MapAttributeMeta
481
+ """
482
+
483
+
484
+ @add_metaclass (MapAttributeMeta )
476
485
class MapAttribute (AttributeContainer , Attribute ):
477
486
attr_type = MAP
478
487
@@ -482,7 +491,6 @@ def __init__(self, hash_key=False, range_key=False, null=None, default=None, att
482
491
null = null ,
483
492
default = default ,
484
493
attr_name = attr_name )
485
- self ._get_attributes () # Ensure attributes are always inited
486
494
self .attribute_values = {}
487
495
self ._set_defaults ()
488
496
self ._set_attributes (** attrs )
0 commit comments