-
Notifications
You must be signed in to change notification settings - Fork 429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exposing control over return_values in Model.update() #1050
Open
chkothe
wants to merge
2
commits into
pynamodb:master
Choose a base branch
from
intheon:expose-readback-argument
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -46,7 +46,7 @@ | |||||
ATTR_DEFINITIONS, ATTR_NAME, ATTR_TYPE, KEY_SCHEMA, | ||||||
KEY_TYPE, ITEM, READ_CAPACITY_UNITS, WRITE_CAPACITY_UNITS, | ||||||
RANGE_KEY, ATTRIBUTES, PUT, DELETE, RESPONSES, | ||||||
INDEX_NAME, PROVISIONED_THROUGHPUT, PROJECTION, ALL_NEW, | ||||||
INDEX_NAME, PROVISIONED_THROUGHPUT, PROJECTION, ALL_NEW, NONE, | ||||||
GLOBAL_SECONDARY_INDEXES, LOCAL_SECONDARY_INDEXES, KEYS, | ||||||
PROJECTION_TYPE, NON_KEY_ATTRIBUTES, | ||||||
TABLE_STATUS, ACTIVE, RETURN_VALUES, BATCH_GET_PAGE_LIMIT, | ||||||
|
@@ -417,31 +417,43 @@ def delete(self, condition: Optional[Condition] = None, settings: OperationSetti | |||||
|
||||||
return self._get_connection().delete_item(hk_value, range_key=rk_value, condition=condition, settings=settings) | ||||||
|
||||||
def update(self, actions: List[Action], condition: Optional[Condition] = None, settings: OperationSettings = OperationSettings.default) -> Any: | ||||||
def update( | ||||||
self, | ||||||
actions: List[Action], | ||||||
condition: Optional[Condition] = None, | ||||||
settings: OperationSettings = OperationSettings.default, | ||||||
read_back: str = ALL_NEW, | ||||||
) -> Any: | ||||||
""" | ||||||
Updates an item using the UpdateItem operation. | ||||||
|
||||||
:param actions: a list of Action updates to apply | ||||||
:param condition: an optional Condition on which to update | ||||||
:param settings: per-operation settings | ||||||
:param read_back: what to read back from the DB after the update | ||||||
ALL_NEW: read back entire object after the update (default) | ||||||
NONE: read back nothing, local version is not updated | ||||||
:raises ModelInstance.DoesNotExist: if the object to be updated does not exist | ||||||
:raises pynamodb.exceptions.UpdateError: if the `condition` is not met | ||||||
""" | ||||||
if not isinstance(actions, list) or len(actions) == 0: | ||||||
raise TypeError("the value of `actions` is expected to be a non-empty list") | ||||||
if read_back not in (ALL_NEW, NONE): | ||||||
raise ValueError("expected `read_back` to be `ALL_NEW` or `NONE`, but was: {}".format(read_back)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
hk_value, rk_value = self._get_hash_range_key_serialized_values() | ||||||
version_condition = self._handle_version_attribute(actions=actions) | ||||||
if version_condition is not None: | ||||||
condition &= version_condition | ||||||
|
||||||
data = self._get_connection().update_item(hk_value, range_key=rk_value, return_values=ALL_NEW, condition=condition, actions=actions, settings=settings) | ||||||
item_data = data[ATTRIBUTES] | ||||||
stored_cls = self._get_discriminator_class(item_data) | ||||||
if stored_cls and stored_cls != type(self): | ||||||
raise ValueError("Cannot update this item from the returned class: {}".format(stored_cls.__name__)) | ||||||
self.deserialize(item_data) | ||||||
return data | ||||||
data = self._get_connection().update_item(hk_value, range_key=rk_value, return_values=read_back, condition=condition, actions=actions, settings=settings) | ||||||
if read_back == ALL_NEW: | ||||||
item_data = data[ATTRIBUTES] | ||||||
stored_cls = self._get_discriminator_class(item_data) | ||||||
if stored_cls and stored_cls != type(self): | ||||||
raise ValueError("Cannot update this item from the returned class: {}".format(stored_cls.__name__)) | ||||||
self.deserialize(item_data) | ||||||
return data | ||||||
|
||||||
def save(self, condition: Optional[Condition] = None, settings: OperationSettings = OperationSettings.default) -> Dict[str, Any]: | ||||||
""" | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -17,7 +17,7 @@ | |||||
from pynamodb.constants import ( | ||||||
ITEM, STRING, ALL, KEYS_ONLY, INCLUDE, REQUEST_ITEMS, UNPROCESSED_KEYS, CAMEL_COUNT, | ||||||
RESPONSES, KEYS, ITEMS, LAST_EVALUATED_KEY, EXCLUSIVE_START_KEY, ATTRIBUTES, BINARY, | ||||||
UNPROCESSED_ITEMS, DEFAULT_ENCODING, MAP, LIST, NUMBER, SCANNED_COUNT, | ||||||
UNPROCESSED_ITEMS, DEFAULT_ENCODING, MAP, LIST, NUMBER, SCANNED_COUNT, ALL_NEW, NONE | ||||||
) | ||||||
from pynamodb.models import Model | ||||||
from pynamodb.indexes import ( | ||||||
|
@@ -921,6 +921,28 @@ def test_update(self, mock_time): | |||||
assert item.views is None | ||||||
self.assertEqual({'bob'}, item.custom_aliases) | ||||||
|
||||||
def test_update_readback(self): | ||||||
self.init_table_meta(SimpleUserModel, SIMPLE_MODEL_TABLE_DATA) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
No longer needed 😅 |
||||||
item = SimpleUserModel(user_name='foo', is_active=True, email='[email protected]', signature='foo', views=100) | ||||||
|
||||||
with patch(PATCH_METHOD) as req: | ||||||
req.return_value = {} | ||||||
item.update( | ||||||
actions=[SimpleUserModel.email.set('[email protected]')], | ||||||
read_back=NONE) | ||||||
params = { | ||||||
'TableName': 'SimpleModel', | ||||||
'Key': {'user_name': {'S': 'foo'}}, | ||||||
'ReturnValues': 'NONE', | ||||||
'UpdateExpression': 'SET #0 = :0', | ||||||
'ExpressionAttributeNames': {'#0': 'email'}, | ||||||
'ExpressionAttributeValues': {':0': {'S': '[email protected]'}}, | ||||||
'ReturnConsumedCapacity': 'TOTAL' | ||||||
} | ||||||
args = req.call_args[0][1] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
deep_eq(args, params, _assert=True) | ||||||
assert item.email == '[email protected]' | ||||||
|
||||||
def test_update_doesnt_do_validation_on_null_attributes(self): | ||||||
self.init_table_meta(CarModel, CAR_MODEL_TABLE_DATA) | ||||||
item = CarModel(12345) | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should make it an enum. This library isn't awfully modern, but maybe there's an opportunity to change that (especially as we're establishing a new API here).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd say an enum is a better choice, too. Let's start to use strong typing whenever possible.