Skip to content
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

Make resource identifiers immutable #246

Merged
merged 3 commits into from
Sep 11, 2015

Conversation

kyleknap
Copy link
Contributor

@kyleknap kyleknap commented Sep 4, 2015

Before you could switch out the identifiers on a resource, which can have unintended affects if you switch the identifier and then make subsequent calls with the resource. Now identifiers are read-only, much like how attributes are read-only:

In [1]: import boto3

In [2]: s3 = boto3.resource('s3')

In [3]: bucket = s3.Bucket('mybucketfoo')

In [4]: bucket.name = 'foo'
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-5d85c15550cd> in <module>()
----> 1 bucket.name = 'foo'

AttributeError: can't set attribute

cc @jamesls @mtdowling @rayluo

@rayluo rayluo added the enhancement This issue requests an improvement to a current feature. label Sep 8, 2015
@rayluo
Copy link
Contributor

rayluo commented Sep 8, 2015

LGTM

Creates a read-only property for identifier attributes.
"""
def identifier(self):
return getattr(self, '_' + name, None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be returning None if the internal attr doesn't exist? Seems like that would only happen if we had a bug, in which case we'd want it to fail loudly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I am fine with that. I do not think that it will happen unless there is a bug. Furthermore, since this is an identifier in general, its value cannot be None so it make sense not to default to None.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scratch what I said, I remembered why I set it a default to None. It is because when the resource is instantiated there is check to make sure that all of the identifiers are non-None values: https://github.com/boto/boto3/blob/develop/boto3/resources/base.py#L117. So if I did not use a default, I would get a bad error message like: 'test.Queue' object has no attribute '_url' instead of the current message of: ValueError: Required parameter url not set because the identifier getter is called before the ValueError is thrown. I would prefer to keep it as is unless you have any other suggestions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's fine, but let's add a comment here that explains that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do that.

@jamesls
Copy link
Member

jamesls commented Sep 11, 2015

Just had a question about a failure case, otherwise looks good.

@jamesls
Copy link
Member

jamesls commented Sep 11, 2015

:shipit:

kyleknap added a commit that referenced this pull request Sep 11, 2015
Make resource identifiers immutable
@kyleknap kyleknap merged commit c1551fd into boto:develop Sep 11, 2015
@kyleknap kyleknap deleted the immutable-identifier branch September 11, 2015 22:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue requests an improvement to a current feature.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants