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

Error on refresh with UTCAttribute in python 2.7 #3

Closed
wants to merge 1 commit into from

Conversation

JohnCEarls
Copy link

A model that has a UTCDatetime attribute, if you do a model.refresh() you get the following error.

The problem is the attribute is a unicode object after being read and the function expects a datetime object. I doubt this is the right way to fix this bug, but it should give a place to start.

My setup
Python 2.7.3
delorean and six I believe were installed when Pynamodb was.

You have any trouble reproducing this, let me know.
something like

class myModel(Model):
    table_name = 'sometabel'                                                                           
    name = UnicodeAttribute( hash_key=True )                            
    date_created = UTCDateTimeAttribute( range_key=True, default=datetime.utcnow() )
    avar = UnicodeAttribute()

myModel.create_table( read_capacity_units=2, write_capacity_units=1, wait=True)
m = myModel('bar')
m.save()
models = [m for m in myModel.scan(name__eq='bar')]
models[0].refresh()

oughta reproduce it.

self._model.refresh()
File "/usr/local/lib/python2.7/dist-packages/pynamodb/models.py", line 360, in refresh
args, kwargs = self._get_save_args(attributes=False)
File "/usr/local/lib/python2.7/dist-packages/pynamodb/models.py", line 346, in _get_save_args
serialized = self.serialize(null_check=null_check)
File "/usr/local/lib/python2.7/dist-packages/pynamodb/models.py", line 391, in serialize
serialized = attr.serialize(value)
File "/usr/local/lib/python2.7/dist-packages/pynamodb/attributes.py", line 258, in serialize
fmt = Delorean(value, timezone=UTC).datetime.strftime(DATETIME_FORMAT)
File "/usr/local/lib/python2.7/dist-packages/delorean/dates.py", line 175, in init
if not is_datetime_naive(datetime):
File "/usr/local/lib/python2.7/dist-packages/delorean/dates.py", line 30, in is_datetime_naive
if dt.tzinfo is None:
AttributeError: 'unicode' object has no attribute 'tzinfo'

BTW, thanks for writing this. Been looking for something like this for a while now.

    self._model.refresh()
  File "/usr/local/lib/python2.7/dist-packages/pynamodb/models.py", line 360, in refresh
    args, kwargs = self._get_save_args(attributes=False)
  File "/usr/local/lib/python2.7/dist-packages/pynamodb/models.py", line 346, in _get_save_args
    serialized = self.serialize(null_check=null_check)
  File "/usr/local/lib/python2.7/dist-packages/pynamodb/models.py", line 391, in serialize
    serialized = attr.serialize(value)
  File "/usr/local/lib/python2.7/dist-packages/pynamodb/attributes.py", line 258, in serialize
    fmt = Delorean(value, timezone=UTC).datetime.strftime(DATETIME_FORMAT)
  File "/usr/local/lib/python2.7/dist-packages/delorean/dates.py", line 175, in __init__
    if not is_datetime_naive(datetime):
  File "/usr/local/lib/python2.7/dist-packages/delorean/dates.py", line 30, in is_datetime_naive
    if dt.tzinfo is None:
AttributeError: 'unicode' object has no attribute 'tzinfo'
@jlafon jlafon closed this in a8fbe47 Feb 4, 2014
jlafon added a commit that referenced this pull request Feb 4, 2014
* Issue_3:
  Correctly deserialize complex key types. Fixes #3
  Tests for refreshing objects with complex key types
  Added test data for issue 3
@jlafon
Copy link
Contributor

jlafon commented Feb 4, 2014

The problem was that I wasn't deserializing object attributes that were also keys, so for unicode or string types everything worked, but failed with a key attribute that actually required serialization. Thanks for reporting this.

jlafon added a commit that referenced this pull request Feb 26, 2014
* devel: (24 commits)
  PEP8 compliance
  Updated docs to explain DoesNotExist being raise when an item is not found
  Disabled caching of the botocore session to avoid credentials expiring
  dev version
  0.1.10
  Fixes #4. Correctly construct keys for tables that don't have both a hash key and a range key in batch get operations.
  added link to google group
  botocore end points shouldn't be cached, because they don't update their IAM role credentials
  first pass at API docs. Fixed some docstring contents. Added links to functions from tutorial and indexes pages.
  import the actual version
  pep8 fixed
  link to the awscli docs for full credentials options
  make way for an api auto page
  bumped botocore requirement
  New exception behavior: Model.get and Model.refresh will now raise DoesNotExist if the item is not found in the table.
  Added docstring comments to models class
  Docstring
  typo in README
  dev version
  Correctly deserialize complex key types. Fixes #3
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants