Skip to content

Commit b42c326

Browse files
authored
Remove snake_to_camel_case and just use constants. (#865)
1 parent 2e2a37c commit b42c326

File tree

6 files changed

+74
-89
lines changed

6 files changed

+74
-89
lines changed

docs/release_notes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Other changes in this release:
3838
* Remove ``ListAttribute.remove_indexes`` (added in v4.3.2) and document usage of remove for list elements (#838)
3939
* Add the attribute name to error messages when deserialization fails (#815)
4040
* Add the table name to error messages for transactional operations (#835)
41-
* Move ``pynamodb.connection.util.pythonic`` to ``pynamodb.util.snake_to_camel_case`` (#753)
41+
* Remove ``pynamodb.connection.util.pythonic`` (#753) and (#865)
4242
* Remove ``ModelContextManager`` class (#861)
4343

4444
Contributors to this release:

pynamodb/connection/base.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
from pynamodb.settings import get_settings_value
5555
from pynamodb.signals import pre_dynamodb_send, post_dynamodb_send
5656
from pynamodb.types import HASH, RANGE
57-
from pynamodb.util import snake_to_camel_case
5857

5958
BOTOCORE_EXCEPTIONS = (BotoCoreError, ClientError)
6059
RATE_LIMITING_ERROR_CODES = ['ProvisionedThroughputExceededException', 'ThrottlingException']
@@ -587,8 +586,8 @@ def create_table(
587586
raise ValueError("attribute_definitions argument is required")
588587
for attr in attribute_definitions:
589588
attrs_list.append({
590-
ATTR_NAME: attr.get(snake_to_camel_case(ATTR_NAME)),
591-
ATTR_TYPE: attr.get(snake_to_camel_case(ATTR_TYPE))
589+
ATTR_NAME: attr.get('attribute_name'),
590+
ATTR_TYPE: attr.get('attribute_type')
592591
})
593592
operation_kwargs[ATTR_DEFINITIONS] = attrs_list
594593

@@ -603,10 +602,10 @@ def create_table(
603602
global_secondary_indexes_list = []
604603
for index in global_secondary_indexes:
605604
index_kwargs = {
606-
INDEX_NAME: index.get(snake_to_camel_case(INDEX_NAME)),
607-
KEY_SCHEMA: sorted(index.get(snake_to_camel_case(KEY_SCHEMA)), key=lambda x: x.get(KEY_TYPE)),
608-
PROJECTION: index.get(snake_to_camel_case(PROJECTION)),
609-
PROVISIONED_THROUGHPUT: index.get(snake_to_camel_case(PROVISIONED_THROUGHPUT))
605+
INDEX_NAME: index.get('index_name'),
606+
KEY_SCHEMA: sorted(index.get('key_schema'), key=lambda x: x.get(KEY_TYPE)),
607+
PROJECTION: index.get('projection'),
608+
PROVISIONED_THROUGHPUT: index.get('provisioned_throughput')
610609
}
611610
if billing_mode == PAY_PER_REQUEST_BILLING_MODE:
612611
del index_kwargs[PROVISIONED_THROUGHPUT]
@@ -618,25 +617,25 @@ def create_table(
618617
key_schema_list = []
619618
for item in key_schema:
620619
key_schema_list.append({
621-
ATTR_NAME: item.get(snake_to_camel_case(ATTR_NAME)),
622-
KEY_TYPE: str(item.get(snake_to_camel_case(KEY_TYPE))).upper()
620+
ATTR_NAME: item.get('attribute_name'),
621+
KEY_TYPE: str(item.get('key_type')).upper()
623622
})
624623
operation_kwargs[KEY_SCHEMA] = sorted(key_schema_list, key=lambda x: x.get(KEY_TYPE))
625624

626625
local_secondary_indexes_list = []
627626
if local_secondary_indexes:
628627
for index in local_secondary_indexes:
629628
local_secondary_indexes_list.append({
630-
INDEX_NAME: index.get(snake_to_camel_case(INDEX_NAME)),
631-
KEY_SCHEMA: sorted(index.get(snake_to_camel_case(KEY_SCHEMA)), key=lambda x: x.get(KEY_TYPE)),
632-
PROJECTION: index.get(snake_to_camel_case(PROJECTION)),
629+
INDEX_NAME: index.get('index_name'),
630+
KEY_SCHEMA: sorted(index.get('key_schema'), key=lambda x: x.get(KEY_TYPE)),
631+
PROJECTION: index.get('projection'),
633632
})
634633
operation_kwargs[LOCAL_SECONDARY_INDEXES] = local_secondary_indexes_list
635634

636635
if stream_specification:
637636
operation_kwargs[STREAM_SPECIFICATION] = {
638-
STREAM_ENABLED: stream_specification[snake_to_camel_case(STREAM_ENABLED)],
639-
STREAM_VIEW_TYPE: stream_specification[snake_to_camel_case(STREAM_VIEW_TYPE)]
637+
STREAM_ENABLED: stream_specification['stream_enabled'],
638+
STREAM_VIEW_TYPE: stream_specification['stream_view_type']
640639
}
641640

642641
try:
@@ -699,10 +698,10 @@ def update_table(
699698
for index in global_secondary_index_updates:
700699
global_secondary_indexes_list.append({
701700
UPDATE: {
702-
INDEX_NAME: index.get(snake_to_camel_case(INDEX_NAME)),
701+
INDEX_NAME: index.get('index_name'),
703702
PROVISIONED_THROUGHPUT: {
704-
READ_CAPACITY_UNITS: index.get(snake_to_camel_case(READ_CAPACITY_UNITS)),
705-
WRITE_CAPACITY_UNITS: index.get(snake_to_camel_case(WRITE_CAPACITY_UNITS))
703+
READ_CAPACITY_UNITS: index.get('read_capacity_units'),
704+
WRITE_CAPACITY_UNITS: index.get('write_capacity_units')
706705
}
707706
}
708707
})

pynamodb/indexes.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from pynamodb.expressions.condition import Condition
1515
from pynamodb.pagination import ResultIterator
1616
from pynamodb.types import HASH, RANGE
17-
from pynamodb.util import snake_to_camel_case
1817

1918
if TYPE_CHECKING:
2019
from pynamodb.models import Model
@@ -156,8 +155,8 @@ def _get_schema(cls) -> Dict:
156155
schema = []
157156
for attr_name, attr_cls in cls._get_attributes().items():
158157
attr_definitions.append({
159-
snake_to_camel_case(ATTR_NAME): attr_cls.attr_name,
160-
snake_to_camel_case(ATTR_TYPE): attr_cls.attr_type
158+
'attribute_name': attr_cls.attr_name,
159+
'attribute_type': attr_cls.attr_type
161160
})
162161
if attr_cls.is_hash_key:
163162
schema.append({
@@ -170,8 +169,8 @@ def _get_schema(cls) -> Dict:
170169
KEY_TYPE: RANGE
171170
})
172171
return {
173-
snake_to_camel_case(KEY_SCHEMA): schema,
174-
snake_to_camel_case(ATTR_DEFINITIONS): attr_definitions
172+
'key_schema': schema,
173+
'attribute_definitions': attr_definitions
175174
}
176175

177176
@classmethod

pynamodb/models.py

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
COUNT, ITEM_COUNT, KEY, UNPROCESSED_ITEMS, STREAM_VIEW_TYPE,
5454
STREAM_SPECIFICATION, STREAM_ENABLED, BILLING_MODE, PAY_PER_REQUEST_BILLING_MODE
5555
)
56-
from pynamodb.util import snake_to_camel_case
5756

5857
_T = TypeVar('_T', bound='Model')
5958
_KeyType = Any
@@ -415,11 +414,11 @@ def update(self, actions: Sequence[Action], condition: Optional[Condition] = Non
415414
if version_condition is not None:
416415
condition &= version_condition
417416
kwargs: Dict[str, Any] = {
418-
snake_to_camel_case(RETURN_VALUES): ALL_NEW,
417+
'return_values': ALL_NEW,
419418
}
420419

421-
if snake_to_camel_case(RANGE_KEY) in save_kwargs:
422-
kwargs[snake_to_camel_case(RANGE_KEY)] = save_kwargs[snake_to_camel_case(RANGE_KEY)]
420+
if 'range_key' in save_kwargs:
421+
kwargs['range_key'] = save_kwargs['range_key']
423422

424423
kwargs.update(condition=condition)
425424
kwargs.update(actions=actions)
@@ -482,8 +481,8 @@ def get_operation_kwargs_from_instance(
482481
)
483482
if not is_update:
484483
kwargs.update(save_kwargs)
485-
elif snake_to_camel_case(RANGE_KEY) in save_kwargs:
486-
kwargs[snake_to_camel_case(RANGE_KEY)] = save_kwargs[snake_to_camel_case(RANGE_KEY)]
484+
elif 'range_key' in save_kwargs:
485+
kwargs['range_key'] = save_kwargs['range_key']
487486
return self._get_connection().get_operation_kwargs(*args, **kwargs)
488487

489488
@classmethod
@@ -771,32 +770,32 @@ def create_table(
771770
"""
772771
if not cls.exists():
773772
schema = cls._get_schema()
774-
if hasattr(cls.Meta, snake_to_camel_case(READ_CAPACITY_UNITS)):
775-
schema[snake_to_camel_case(READ_CAPACITY_UNITS)] = cls.Meta.read_capacity_units
776-
if hasattr(cls.Meta, snake_to_camel_case(WRITE_CAPACITY_UNITS)):
777-
schema[snake_to_camel_case(WRITE_CAPACITY_UNITS)] = cls.Meta.write_capacity_units
778-
if hasattr(cls.Meta, snake_to_camel_case(STREAM_VIEW_TYPE)):
779-
schema[snake_to_camel_case(STREAM_SPECIFICATION)] = {
780-
snake_to_camel_case(STREAM_ENABLED): True,
781-
snake_to_camel_case(STREAM_VIEW_TYPE): cls.Meta.stream_view_type
773+
if hasattr(cls.Meta, 'read_capacity_units'):
774+
schema['read_capacity_units'] = cls.Meta.read_capacity_units
775+
if hasattr(cls.Meta, 'write_capacity_units'):
776+
schema['write_capacity_units'] = cls.Meta.write_capacity_units
777+
if hasattr(cls.Meta, 'stream_view_type'):
778+
schema['stream_specification'] = {
779+
'stream_enabled': True,
780+
'stream_view_type': cls.Meta.stream_view_type
782781
}
783-
if hasattr(cls.Meta, snake_to_camel_case(BILLING_MODE)):
784-
schema[snake_to_camel_case(BILLING_MODE)] = cls.Meta.billing_mode
782+
if hasattr(cls.Meta, 'billing_mode'):
783+
schema['billing_mode'] = cls.Meta.billing_mode
785784
if read_capacity_units is not None:
786-
schema[snake_to_camel_case(READ_CAPACITY_UNITS)] = read_capacity_units
785+
schema['read_capacity_units'] = read_capacity_units
787786
if write_capacity_units is not None:
788-
schema[snake_to_camel_case(WRITE_CAPACITY_UNITS)] = write_capacity_units
787+
schema['write_capacity_units'] = write_capacity_units
789788
if billing_mode is not None:
790-
schema[snake_to_camel_case(BILLING_MODE)] = billing_mode
789+
schema['billing_mode'] = billing_mode
791790
index_data = cls._get_indexes()
792-
schema[snake_to_camel_case(GLOBAL_SECONDARY_INDEXES)] = index_data.get(snake_to_camel_case(GLOBAL_SECONDARY_INDEXES))
793-
schema[snake_to_camel_case(LOCAL_SECONDARY_INDEXES)] = index_data.get(snake_to_camel_case(LOCAL_SECONDARY_INDEXES))
794-
index_attrs = index_data.get(snake_to_camel_case(ATTR_DEFINITIONS))
795-
attr_keys = [attr.get(snake_to_camel_case(ATTR_NAME)) for attr in schema.get(snake_to_camel_case(ATTR_DEFINITIONS))]
791+
schema['global_secondary_indexes'] = index_data.get('global_secondary_indexes')
792+
schema['local_secondary_indexes'] = index_data.get('local_secondary_indexes')
793+
index_attrs = index_data.get('attribute_definitions')
794+
attr_keys = [attr.get('attribute_name') for attr in schema.get('attribute_definitions')]
796795
for attr in index_attrs:
797-
attr_name = attr.get(snake_to_camel_case(ATTR_NAME))
796+
attr_name = attr.get('attribute_name')
798797
if attr_name not in attr_keys:
799-
schema[snake_to_camel_case(ATTR_DEFINITIONS)].append(attr)
798+
schema['attribute_definitions'].append(attr)
800799
attr_keys.append(attr_name)
801800
cls._get_connection().create_table(
802801
**schema
@@ -841,24 +840,24 @@ def _get_schema(cls):
841840
Returns the schema for this table
842841
"""
843842
schema: Dict[str, List] = {
844-
snake_to_camel_case(ATTR_DEFINITIONS): [],
845-
snake_to_camel_case(KEY_SCHEMA): []
843+
'attribute_definitions': [],
844+
'key_schema': []
846845
}
847846
for attr_name, attr_cls in cls.get_attributes().items():
848847
if attr_cls.is_hash_key or attr_cls.is_range_key:
849-
schema[snake_to_camel_case(ATTR_DEFINITIONS)].append({
850-
snake_to_camel_case(ATTR_NAME): attr_cls.attr_name,
851-
snake_to_camel_case(ATTR_TYPE): attr_cls.attr_type
848+
schema['attribute_definitions'].append({
849+
'attribute_name': attr_cls.attr_name,
850+
'attribute_type': attr_cls.attr_type
852851
})
853852
if attr_cls.is_hash_key:
854-
schema[snake_to_camel_case(KEY_SCHEMA)].append({
855-
snake_to_camel_case(KEY_TYPE): HASH,
856-
snake_to_camel_case(ATTR_NAME): attr_cls.attr_name
853+
schema['key_schema'].append({
854+
'key_type': HASH,
855+
'attribute_name': attr_cls.attr_name
857856
})
858857
elif attr_cls.is_range_key:
859-
schema[snake_to_camel_case(KEY_SCHEMA)].append({
860-
snake_to_camel_case(KEY_TYPE): RANGE,
861-
snake_to_camel_case(ATTR_NAME): attr_cls.attr_name
858+
schema['key_schema'].append({
859+
'key_type': RANGE,
860+
'attribute_name': attr_cls.attr_name
862861
})
863862
return schema
864863

@@ -869,35 +868,35 @@ def _get_indexes(cls):
869868
"""
870869
if cls._indexes is None:
871870
cls._indexes = {
872-
snake_to_camel_case(GLOBAL_SECONDARY_INDEXES): [],
873-
snake_to_camel_case(LOCAL_SECONDARY_INDEXES): [],
874-
snake_to_camel_case(ATTR_DEFINITIONS): []
871+
'global_secondary_indexes': [],
872+
'local_secondary_indexes': [],
873+
'attribute_definitions': []
875874
}
876875
cls._index_classes = {}
877876
for name, index in getmembers(cls, lambda o: isinstance(o, Index)):
878877
cls._index_classes[index.Meta.index_name] = index
879878
schema = index._get_schema()
880879
idx = {
881-
snake_to_camel_case(INDEX_NAME): index.Meta.index_name,
882-
snake_to_camel_case(KEY_SCHEMA): schema.get(snake_to_camel_case(KEY_SCHEMA)),
883-
snake_to_camel_case(PROJECTION): {
880+
'index_name': index.Meta.index_name,
881+
'key_schema': schema.get('key_schema'),
882+
'projection': {
884883
PROJECTION_TYPE: index.Meta.projection.projection_type,
885884
},
886885

887886
}
888887
if isinstance(index, GlobalSecondaryIndex):
889888
if getattr(cls.Meta, 'billing_mode', None) != PAY_PER_REQUEST_BILLING_MODE:
890-
idx[snake_to_camel_case(PROVISIONED_THROUGHPUT)] = {
889+
idx['provisioned_throughput'] = {
891890
READ_CAPACITY_UNITS: index.Meta.read_capacity_units,
892891
WRITE_CAPACITY_UNITS: index.Meta.write_capacity_units
893892
}
894-
cls._indexes[snake_to_camel_case(ATTR_DEFINITIONS)].extend(schema.get(snake_to_camel_case(ATTR_DEFINITIONS)))
893+
cls._indexes['attribute_definitions'].extend(schema.get('attribute_definitions'))
895894
if index.Meta.projection.non_key_attributes:
896-
idx[snake_to_camel_case(PROJECTION)][NON_KEY_ATTRIBUTES] = index.Meta.projection.non_key_attributes
895+
idx['projection'][NON_KEY_ATTRIBUTES] = index.Meta.projection.non_key_attributes
897896
if isinstance(index, GlobalSecondaryIndex):
898-
cls._indexes[snake_to_camel_case(GLOBAL_SECONDARY_INDEXES)].append(idx)
897+
cls._indexes['global_secondary_indexes'].append(idx)
899898
else:
900-
cls._indexes[snake_to_camel_case(LOCAL_SECONDARY_INDEXES)].append(idx)
899+
cls._indexes['local_secondary_indexes'].append(idx)
901900
return cls._indexes
902901

903902
def _get_save_args(self, attributes=True, null_check=True):
@@ -919,9 +918,9 @@ def _get_save_args(self, attributes=True, null_check=True):
919918
range_key = attribute_values.pop(range_key_attribute.attr_name, {}).get(range_key_attribute.attr_type)
920919
args = (hash_key, )
921920
if range_key is not None:
922-
kwargs[snake_to_camel_case(RANGE_KEY)] = range_key
921+
kwargs['range_key'] = range_key
923922
if attributes:
924-
kwargs[snake_to_camel_case(ATTRIBUTES)] = attribute_values
923+
kwargs['attributes'] = attribute_values
925924
return args, kwargs
926925

927926
def _handle_version_attribute(self, serialized_attributes, actions=None):
@@ -941,16 +940,16 @@ def _handle_version_attribute(self, serialized_attributes, actions=None):
941940
version_condition = version_attribute == version_attribute_value
942941
if actions:
943942
actions.append(version_attribute.add(1))
944-
elif snake_to_camel_case(ATTRIBUTES) in serialized_attributes:
945-
serialized_attributes[snake_to_camel_case(ATTRIBUTES)][version_attribute.attr_name] = self._serialize_value(
943+
elif 'attributes' in serialized_attributes:
944+
serialized_attributes['attributes'][version_attribute.attr_name] = self._serialize_value(
946945
version_attribute, version_attribute_value + 1
947946
)
948947
else:
949948
version_condition = version_attribute.does_not_exist()
950949
if actions:
951950
actions.append(version_attribute.set(1))
952-
elif snake_to_camel_case(ATTRIBUTES) in serialized_attributes:
953-
serialized_attributes[snake_to_camel_case(ATTRIBUTES)][version_attribute.attr_name] = self._serialize_value(
951+
elif 'attributes' in serialized_attributes:
952+
serialized_attributes['attributes'][version_attribute.attr_name] = self._serialize_value(
954953
version_attribute, 1
955954
)
956955

pynamodb/util.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/test_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2684,7 +2684,7 @@ def test_model_with_maps_with_nulls_retrieve_from_db(self):
26842684
self.assertIsNone(item.person.age)
26852685
self.assertIsNone(item.person.is_male)
26862686

2687-
def test_model_with_maps_with_snake_to_camel_case_attributes(self):
2687+
def test_model_with_maps_with_snake_case_attributes(self):
26882688
fake_db = self.database_mocker(
26892689
OfficeEmployee,
26902690
OFFICE_EMPLOYEE_MODEL_TABLE_DATA,

0 commit comments

Comments
 (0)